Perl Module Basics
This is similar to the system used by Java where class names are preceded by the
reversed domain name of their creators. For example, Java classes written by Sun
have names beginning with com.sun . The intent is the same that module
names never accidentally conflict, but the Perl system is considerably simpler
and results in shorter names. Of course, if you'd like to create a module called
Com::Acme::AutomaticDogCatcher module, you can.
...........................................................................................................................................................................................................................
How Perl Finds Modules
Let's take a brief detour into Perl mechanics. You need to know how Perl finds
modules before you can start writing your own. When Perl encounters a use statement
during compilation, it turns the module name into a filename as described earlier in
this chapter. For example, Scalar::List::Utils becomes Scalar/List/Utils.pm. Next,
Perl uses the global array @INC
8
to find a list of candidate directories to look for
Scalar/List/Utils.pm. When Perl finds a module file, it is immediately compiled.
You can find out what your Perl's default @INC is with this command:
perl e print join("\n", @INC) . "\n";
One way to use modules is to put your modules into one of the listed direc
tories usually one with site_perl in the name. This is what happens when you
install a module from CPAN. Another way to use modules is to modify @INC before
Perl starts looking for modules to include a different directory where you store
your modules. An easy way to do that is through the use lib statement shown
earlier. A use lib statement prepends a directory onto @INC at compile time.
For example, if you have a private modules directory in your home directory
9
called modules, you could start your programs with the following:
use lib /home/sam/modules ;
You can see the effect of this command on @INC by printing out after a use lib:
use lib /home/sam/modules ;
print join("\n", @INC) . "\n";
8. The name @INC refers to its use as an include path, although using a module is rarely
referred to as including the module.
9. This is a UNIX specific example since Windows (and other single user operating systems)
don't provide a home directory. However, use lib works just as well on Windows as it does
on UNIX, so the techniques should be easily adaptable.
29
29
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved