O'Reilly logo

Perl Cookbook by Nathan Torkington, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Keeping Your Own Module Directory

Problem

You don’t want to install your own personal modules in the standard per-system extension library.

Solution

You have several choices: use Perl’s -I command line switch; set your PERL5LIB environment variable; or employ the use lib pragma, possibly in conjunction with the FindBin module.

Discussion

The @INC array contains a list of directories that are consulted every time a do, require, or use compiles code from another file, library, or module. You can print these out easily from the command line:

% perl -e 'for (@INC) { printf "%d %s\n", $i++, $_ }'

                  0 /usr/local/perl/lib/i686-linux/5.004
               
                  1 /usr/local/perl/lib
               
                  2 /usr/local/perl/lib/site_perl/i686-linux
               
                  3 /usr/local/perl/lib/site_perl
               
                  4 .

The first two directories, elements and 1 of @INC, are the standard architecture-dependent and architecture-independent directories, which all standard libraries, modules, and pragmas will go into. You have two of them because some modules contain information or formatting that makes sense only on that particular architecture. For example, the Config module contains information that cannot be shared across several architectures, so it goes in the 0th array element. Modules that include compiled C components, such as Socket.so, are also placed there. Most modules, however, go in the platform-independent directory in the 1st element.

The next pair, elements 2 and 3 above, fulfills roles analogous to elements and 1, but on a site-specific basis. Suppose you have ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required