[plug] Dynamic Perl Library Pathing...

Matthew Robinson matt at zensunni.org
Mon Oct 14 15:17:06 WST 2002



Trevor Phillips wrote:
> I'm trying to distribute a few perl scripts with dependent custom libraries 
> which are installed relative to the script, and NOT within the main Perl lib 
> heirarchy. Normally, I'd hard-code the path into a "use lib" call before 
> use-ing the other libraries, but if I'm going to be putting this on several 
> systems, and letting others at it, I'd rather not have to manually edit the 
> scripts all the time.
> 
> I've tried this:
>    $_=$0; s/[^\/]*$//;
>    use lib $_;
> ... but it needs the value at compile time.
> 
> I've tried this:
>    $_=$0; s/[^\/]*$//;
>    eval "use lib $_" if ($_);
> ... but then none of the modules can be found. 
> 
> I could possibly get it working by eval-ing all use's, but that seems to be 
> getting messier and messier.
> 
> Does anyone know of any other clean-ish techniques of doing what I want?
> 

How about:

#!/usr/bin/perl

BEGIN {
    $0 =~ s#^(.*)/##;
    push @INC, $1;
}

use CustomLibrary;



BEGIN blocks are executed during compile time, in fact use Module LIST 
is equivalent to the following:

BEGIN { require Module; import Module LIST }

except that Module must be a bareword.[0]


@INC is a list containing all the directories to be checked for included 
  modules.  use lib is simply a clean interface to modify this list


Hope this helps,


Matt


[0] perldoc -f use

-- 
print map{s&^(.)&\U\1&&&$_}split$,=$",(`$^Xdoc$,-qj`=~m&"(.*)"&)[0].$/



More information about the plug mailing list