Hack #29. Manage Module Paths
Keep your code where it makes sense to you, not just to Perl.
Perl's a flexible language and it tries to make few assumptions about your environment. Perhaps you're a system administrator with root access and a compiler and can install modules anywhere you want. Perhaps you only have shell access on a shared box and have to submit a change request to have something installed. Perhaps you want to test one set of modules against one program but not another.
Whatever the case, Perl gives you options to manage where it looks for modules. Suppose you have a program in your ~/work directory that uses a module named
Site::User. By default, Perl will search all of the directories in the special @INC variable for a file named Site/User.pm. That may not always include the directory you want (especially if, in this case, you want ~/work/lib). What can you do?
Within Your Program
The simplest and most self-contained way to change Perl's search path is within your program by using the lib pragma. This happens at compile-time [Hack #70], as soon as perl encounters the statement, so put it before any use line for the module you want to load. For example:
use lib 'lib'; use Site::User;
adds the lib/ directory—relative to the current directory—to the front of Perl's search path list. Similarly:
no lib 'badlib'; use Site::User;
removes the badlib/ directory from Perl's list of search paths. If you have two versions of Site::User installed and want to make sure that Perl ...