| www.rodneybeede.com | "I would love to change the world, but they won't give me the source code" - unknown |
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.
# 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 will get an error if taint checking is enforced. You have to use use lib qw(.) instead.