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 ...

Get Perl Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.