home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


7.2.41 lib - Manipulate @INC at Compile-Time

use lib 

LIST

;
no lib 

LIST

;

This module simplifies the manipulation of Perl's special @INC variable at compile-time. It is used to add extra directories to Perl's search path so that later use or require statements will find modules not located along Perl's default search path.

7.2.41.1 Adding directories

Directories itemized in LIST are added to the start of the Perl search path. Saying:

use lib 

LIST

;

is almost the same as saying:

BEGIN { unshift(@INC, 

LIST

) }

The difference is that, for each directory in LIST (called $dir here), the lib module also checks to see whether a directory called $dir / $archname / auto exists, where $archname is derived from Perl's configuration information:

use Config;
$archname = $Config{'archname'};

If so, the $dir/$archname directory is assumed to be an architecture-specific directory and is added to @INC in front of $dir .

If LIST includes both $dir and $dir / $archname , then $dir / $archname will be added to @INC twice (assuming $dir / $archname / auto exists).

7.2.41.2 Deleting directories

You should normally only add directories to @INC . If you need to delete directories from @INC , take care to delete only those you yourself added. Otherwise, be certain that the directories you delete are not needed by other modules directly or indirectly invoked by your script. Other modules may have added directories they need for correct operation.

By default the statement:

no lib 

LIST

deletes the first instance of each named directory from @INC . To delete multiple instances of the same name from @INC you can specify the name multiple times.

To delete all instances of all the specified names from @INC you can specify :ALL as the first parameter of LIST . For example:

no lib qw(:ALL .);

For each directory in LIST (called $dir here) the lib module also checks to see whether a directory called $dir / $archname / auto exists. If so, the $dir / $archname directory is assumed to be a corresponding architecture-specific directory and is also deleted from @INC .

If LIST includes both $dir and $dir / $archname then $dir / $archname will be deleted from @INC twice (assuming $dir / $archname / auto exists).

7.2.41.3 Restoring the original directory list

When the lib module is first loaded, it records the current value of @INC in an array @lib::ORIG_INC . To restore @INC to that value you can say:

@INC = @lib::ORIG_INC;

7.2.41.4 See also

The AddINC module (not in the standard Perl library, but available from CPAN) deals with paths relative to the source file.


Previous: 7.2.40 IPC::Open3 - Open a Process for Reading, Writing, and Error Handling Programming Perl Next: 7.2.42 Math::BigFloat - Arbitrary-Length, Floating-Point Math Package
7.2.40 IPC::Open3 - Open a Process for Reading, Writing, and Error Handling Book Index 7.2.42 Math::BigFloat - Arbitrary-Length, Floating-Point Math Package