Modules

A module is a package defined in a file with a name that is the same as the package’s. Perl locates modules by searching the @INC array, which contains a list of library directories. Perl’s use of @INC is roughly comparable to the Unix shell’s use of the PATH environment variable to locate executable programs. @INC is defined when Perl is built and can be supplemented with the -I command-line option to Perl or with use lib within a program.

When you refer to ModuleName in your program, Perl searches in the directories listed in @INC for the module file ModuleName.pm and uses the first one it finds. When you refer to a module embedded in another package, such as ParentPackage::ModuleName, Perl looks for a ParentPackage/ subdirectory in the @INC path, and for a ModuleName.pm file in that subdirectory.

Every Perl installation includes a central lib directory. The actual pathname of this directory varies from system to system, but it’s commonly /usr/lib/perl or /usr/local/lib/perl. Looking at the central lib directory for your Perl distribution, you’ll see something like this:

% ls -aF /usr/local/lib/perl ./ I18N/ bigfloat.pl less.pm ../ IO/ bigint.pl lib.pm AnyDBM_File.pm IPC/ bigrat.pl locale.pm AutoLoader.pm Math/ blib.pm look.pl AutoSplit.pm Net/ cacheout.pl man/ Benchmark.pm Pod/ chat2.pl newgetopt.pl Bundle/ Search/ complete.pl open2.pl CGI/ SelectSaver.pm constant.pm open3.pl CGI.pm SelfLoader.pm ctime.pl perl5db.pl CPAN/ Shell.pm diagnostics.pm pod/ CPAN.pm Symbol.pm dotsh.pl ...

Get Perl in a Nutshell, 2nd Edition 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.