Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

use lib connundrum

by Angel (Friar)
on Dec 02, 2002 at 14:39 UTC ( [id://216930]=perlquestion: print w/replies, xml ) Need Help??

Angel has asked for the wisdom of the Perl Monks concerning the following question:

I learned objects and now I want to store all of them in one place so that when I do updates I do not forget to change any files.
so I did :
use lib ( "~my_account/public_html/PERL_TEST/" );
Now the modules worked when I had the code and the X::Y subirectories in the same directory but now I get this error:
[Mon Dec 2 09:32:44 2002] User.pm: Can't locate Module/User.pm in @IN +C (@INC contains: ~my_account/public_html/PERL_TEST/ /pkg/perl-5.005_ +02/lib/perl5.005_02/i386-netbsd /pkg/perl-5.005_02/lib/perl5.005_02 / +usr/local/lib/site_perl/5.005 /usr/local/lib/site_perl .) at registra +tion.cgi line 18. BEGIN failed--compilation aborted at registration.cgi line 18.
so it sees the lib function any ideas?

update (broquaint): cleaned up formatting

Replies are listed 'Best First'.
Re: use lib connundrum
by tadman (Prior) on Dec 02, 2002 at 14:59 UTC
    Tilde is a feature of the shell which Perl is probably not able to expand for you. You can substitute it with something like this:
    use lib "$ENV{HOME}/public_html/PERL_TEST";
    There's also no guarantee that you're going to have a HOME environment variable either.

    A more reliable method might be something like this:
    BEGIN { my $home = (getpwuid($<))[7]; # Current user use lib "$home/public_html/PERL_TEST"; }
    Update:
    The above was merely given as an example of how you can replace tilde for the current user, figuring it was fairly obvious how to retool it to be more generic. What I probably should've put was something like this:
    BEGIN { my $user = 'someuser'; my ($home) = (getpwnam($user))[7]; # Specific user die "Can't find user $user home directory\n" unless($home); use lib "$home/bin"; print $home; }
    While having a hard-coded path is one way to ensure that the application will work, having an application use libraries from the home directory of a particular user isn't all that unusual, although it can be confusing if not documented clearly.
      There's also no guarantee that you're going to have a HOME environment variable either.
      More importantly, it'll definitely be the wrong HOME if I run your script rather than you.

      Even your second solution is wrong in this respect.

      The only solution is to fully hard-code the path. That's the point of "use lib", anyway!

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

(z) Re: use lib connundrum
by zigdon (Deacon) on Dec 02, 2002 at 14:43 UTC
    Just a guess - perhaps 'lib' doesn't understand ~user? try entering the full path:
    use lib ( "/path/to/home/public_html/PERL_TEST/" );

    -- Dan

Re: use lib connundrum
by broquaint (Abbot) on Dec 02, 2002 at 14:57 UTC
    The ~username convention is a shell shortcut (much like ~- and ~/) so isn't interpreted by perl. So you'll just have to hardcode the path I'm afraid, although I'm sure a tilde expansion module would be most welcome on CPAN :)
    HTH

    _________
    broquaint

Re: use lib connundrum
by rir (Vicar) on Dec 02, 2002 at 20:36 UTC
    use lib ( "~my_account/public_html/PERL_TEST/" ); This is what the environment variable PERL5LIB is made for. Set it in the appropriate profile like file.

    Using the environment may be a security issue, which is the argument for hard coded paths.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://216930]
Approved by valdez
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found