Last Modified: Fri, 20 Mar 2015 21:04:07 +0000 ; Created: Fri, 24 Apr 2009 17:06:42 +0000
| I have some useful code that I use for scripts that need to load Perl library modules from a sub-folder in the directory where the script resides.
This works well when for example you need to run a script on a machine where you can't install Perl modules into the system Perl. It's also nice in that you can bundle your script and libraries into a single archive for distribution and only have to decompress the archive to install everything. A common method available since perl 5.8: FindBin An alternative method:
# This will dyanmically load the script directory as a perl library path
# This allows us to not require installing external libaries into perl
BEGIN {
my $myScriptLoc = "";
use File::Spec::Functions qw(rel2abs);
use File::Basename qw(dirname);
# Perl always makes $0 work if it is actually a file (and not perl -e code)
$myScriptLoc = dirname(rel2abs($0));
push(@INC, "$myScriptLoc/lib");
}
Note that if you try this in a CGI script you may get an error if taint checking is enforced. You have to use use lib qw(.) instead. |
|