Loading Perl Modules from same directory as script

Loading Perl Modules from same directory as script

am 13.02.2006 23:27:55 von Gillies

Well, after only 6 hours of hacking around trying to do something that
should be obvious in perl - but isn't - i seem to have found two
solutions :

PROBLEM : Your team is developing a tree of perl scripts. Any
developer can check out a private and executable copy of the tree.
You want each developer to reference their own private checked-out
library modules without hacking on the checkout code or hack on the
developer's environment variables, etc.

For the sake of simplicity, you want the library modules and the perl
scripts to live in the same directory when they are checked out. I am
using Perl v5.8.0.

SOLUTION #1 : "PORTABLE SOLUTION"

BEGIN {
$my_exe = `which $0`;
while (substr($my_exe, length($my_exe) - 1, 1) ne "/") { chop($my_exe); }
push @INC,$my_exe;
}
require MyModule;

SOLUTION #2 "IF ITS MORE THAN ONE LINE IN PERL THEN ITS PROBABLY WRONG"

BEGIN { # load libraries from THIS SCRIPT'S directory
push @INC, `csh -c "set a=\\\`which $0\\\` ; echo -n \\\${a:h}"`;
}
require "getSlots.pm";

========================================

Now that I have two solutions, is there a third and "Right" way to
solve this problem ?? I cannot believe that PERL module search paths
are relative to directory where you run the command vs. relative to
the directory where the executable files live - this makes no sense at
all, maybe its job security for taintperl to exist or for some other
pointless and nonsensical reason ...

- Don Gillies
San Diego, CA

Re: Loading Perl Modules from same directory as script

am 14.02.2006 17:59:32 von unknown

Donald Gillies wrote:
> Well, after only 6 hours of hacking around trying to do something that
> should be obvious in perl - but isn't - i seem to have found two
> solutions :
>
> PROBLEM : Your team is developing a tree of perl scripts. Any
> developer can check out a private and executable copy of the tree.
> You want each developer to reference their own private checked-out
> library modules without hacking on the checkout code or hack on the
> developer's environment variables, etc.
>
> For the sake of simplicity, you want the library modules and the perl
> scripts to live in the same directory when they are checked out. I am
> using Perl v5.8.0.
>
> SOLUTION #1 : "PORTABLE SOLUTION"
>
> BEGIN {
> $my_exe = `which $0`;
> while (substr($my_exe, length($my_exe) - 1, 1) ne "/") { chop($my_exe); }
> push @INC,$my_exe;
> }
> require MyModule;
>
> SOLUTION #2 "IF ITS MORE THAN ONE LINE IN PERL THEN ITS PROBABLY WRONG"
>
> BEGIN { # load libraries from THIS SCRIPT'S directory
> push @INC, `csh -c "set a=\\\`which $0\\\` ; echo -n \\\${a:h}"`;
> }
> require "getSlots.pm";
>
> ========================================

Solution #3 : Portable, and pure perl.

use File::Basename;
use lib basename ($0);

This is cold-coded, but should work. I know I have done 'use lib' with a
computed argument before.

Tom Wyant