Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl Monk, Perl Meditation
 
PerlMonks  

Re: problem with paths to library files

by themage (Friar)
on Jan 17, 2007 at 09:52 UTC ( #595055=note: print w/ replies, xml ) Need Help??


in reply to problem with paths to library files

hi j_c,

In your case you have to options (that I know), depending on your environment .

If your script is cgi your can use:

BEGIN { my $path=$0; $path=~s{[^\/]+$}{libs/}; } use lib $path;
On the other hand, if your site is using ModPerl, you can configure your libpath in the httpd.conf (or similar) using:
<virtualhost *:80> # ... PerlSetVar libpath "/path/to/your/libs/"; </virtualhost>
And then read that config with:
BEGIN { my $path=$r->dir_config("libpath"); } use lib $path;
Hope this help.


Comment on Re: problem with paths to library files
Select or Download Code
Re^2: problem with paths to library files
by almut (Canon) on Jan 17, 2007 at 11:04 UTC
    BEGIN { my $path=$0; $path=~s{[^\/]+$}{libs/}; } use lib $path;

    The "minor" problem with this is that the lexical $path will already be out of scope in the use lib $path; statement (so the (empty) global $path is being used instead...)

    Moving the my $path; before the BEGIN block does work, though.

    (Honestly, I'm somewhat surprised it does (you might argue the BEGIN block is being executed before the lexical is brought into existence - but apparently this is not the case...). Would any of the more knowledgable Monks care to comment on whether this is a feature you can rely on?)

      I tried moving the
      my $path
      above the begin but it didn't work...
      however, we have succeeded with the following:
      use vars qw/$RealDir/; BEGIN{ $0 =~ m/(.*\/)/;#location of this file $RealDir = $1 . "libs"; }; use lib "$RealDir";

      which works a treat.
      thanks guys, excellent advice :)
      all the best
      johnc

        I tried moving the

        my $path

        above the begin but it didn't work...

        Interesting... that's exactly the kind of thing I was worried about.

        I had tried:

        #!/usr/bin/perl use strict; use warnings; my $path; BEGIN { $path = $0; $path =~ s{[^\/]+$}{libs/}; } use lib $path; use MyLib;

        With the module ./libs/MyLib.pm being

        package MyLib; printf "Module %s loaded.\n", __PACKAGE__;

        running the script prints

        Module MyLib loaded.
        while commenting out the use lib $path gives the expected Can't locate MyLib.pm in @INC ...   IOW, it works for me.

        $ perl -v | head -2 This is perl, v5.8.8 built for x86_64-linux-thread-multi

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://595055]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-04-21 06:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (492 votes), past polls