Chapter 2
Of course, this code will only work if your home directory is called /home/sam .
You can use the following to pull the home directory out of the environment:
use lib "$ENV{HOME}/modules";
But this won't work:
$home_dir = $ENV{HOME};
use lib "$home_dir/modules";
If you do something like this you'll receive the following error:
Empty compile time value given to use lib
The problem is that Perl processes use statements at compile time but the
variable assignment to $home_dir happens at runtime. Perl needs to know where to
look for modules at compile time so that it can find the modules to compile
runtime is much too late. One way to solve this problem is to ask Perl for a little
runtime before compile time is over with BEGIN:
BEGIN { $home_dir = $ENV{HOME}; }
use lib $home_dir;
Of course, you can also modify @INC directly, which also needs to be in a BEGIN
block to be useful:
BEGIN { unshift(@INC, "/home/sam/modules"); }
The preceding line is equivalent to use lib
"
/home/sam/modules
"
. In general
use lib is the preferred method of adding a custom library path to your programs.
Once Perl has loaded a module, it creates an entry in the global hash %INC.
The keys of this hash are module filenames (that is, File/Find.pm), and the
values are the full path to the files loaded for the module (that is,
/usr/local/lib/perl5/5.6.1/File/Find.pm). You can use this hash to get a
list of loaded modules and where they were loaded from:
print map { "$_ => $INC{$_}\n" } keys %INC;
This can be very useful as a debugging aid when you're not sure Perl is picking up
the right version of a module. Perl uses %INC to avoid loading a module file more
than once.
30
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved