First, let's examine the contents of
@INC on our system:
panic% perl -le 'print join "\n", @INC'
/usr/lib/perl5/5.6.1/i386-linux
/usr/lib/perl5/5.6.1
/usr/lib/perl5/site_perl/5.6.1/i386-linux
/usr/lib/perl5/site_perl/5.6.1
/usr/lib/perl5/site_perl
.
Notice . (the current directory) as the last directory in the list.
Let's load the module strict.pm
and see the contents of %INC:
panic% perl -le 'use strict; print map {"$_ => $INC{$_}"} keys %INC'
strict.pm => /usr/lib/perl5/5.6.1/strict.pm
Since strict.pm was found in the
/usr/lib/perl5/5.6.1/ directory and
/usr/lib/perl5/5.6.1/ is a part of
@INC, %INC includes the full
path as the value for the key strict.pm.
Let's create the simplest possible module in
/tmp/test.pm:
1;
This does absolutely nothing, but it returns a true value when
loaded, which is enough to satisfy Perl that it loaded correctly.
Let's load it in different ways:
panic% cd /tmp
panic% perl -e 'use test; \
print map { "$_ => $INC{$_}\n" } keys %INC'
test.pm => test.pm
Since the file was found in . (the directory the code was executed
from), the relative path is used as the value. Now
let's alter @INC by appending
/tmp:
panic% cd /tmp
panic% perl -e 'BEGIN { push @INC, "/tmp" } use test; \
print map { "$_ => $INC{$_}\n" } keys %INC'
test.pm => test.pm
Here we still get the relative path, since the module was found first
relative to
".". The
directory /tmp was placed after . in the list.
If we execute the same code from a different directory, the
"." directory
won't match:
panic% cd /
panic% perl -e 'BEGIN { push @INC, "/tmp" } use test; \
print map { "$_ => $INC{$_}\n" } keys %INC'
test.pm => /tmp/test.pm
so we get the full path. We can also prepend the path with
unshift( ), so that it will be used for matching
before ".". We
will get the full path here as well:
panic% cd /tmp
panic% perl -e 'BEGIN { unshift @INC, "/tmp" } use test; \
print map { "$_ => $INC{$_}\n" } keys %INC'
test.pm => /tmp/test.pm
The code:
BEGIN { unshift @INC, "/tmp" }
can be replaced with the more elegant:
use lib "/tmp";
This is almost equivalent to our BEGIN block and
is the recommended approach.
These approaches to modifying @INC can be labor
intensive: moving the script around in the filesystem might require
modifying the path.