Chapter 2
This chapter will explain Perl's support for modular programming and delve into
modular programming's funny looking cousin, object oriented programming. You
may want to skip this chapter if you have experience programming modules in Perl.
Using Modules
Using modules in Perl is extremely easy. Simply place a use statement at the top of
your program specifying the name of the module. For example, here's a program
that lists all the files over 1 megabyte in size below the current directory, using the
File::Find module that comes with Perl:
#!/usr/bin/perl
use File::Find;
find(sub { print "$_\n" if s $_ > 1_024_000; }, ".");
The File::Find module provides the find() function that traverses directories.
For every file it finds, it calls the subroutine you pass as the first argument. The
name of the current file is made available in the $_ variable. In the preceding
example the subroutine examines the size of the file using s and prints its name if
the size is over a megabyte.
You could, of course, write this program without using File::Find. However, it
would certainly be much longer than the two lines required to do the job with
File::Find. File::Find, like many modules, makes your life easier by providing you
with functionality that you can use without having to write it yourself. Like most
modules, File::Find provides documentation in POD format (covered in detail in
Chapter 3). You can read this documentation using perldoc:
1
$ perldoc File::Find
File::Find provides its functionality through the find() function. This function
is exported. Exporting means that the module provides access to a symbol,
2
in this
case the find() subroutine, in the namespace where the module is used. I'll cover
exporting in more depth later in this chapter.
1. UNIX users may also be able to use man to read module documentation. This is generally
faster than using perldoc.
2. Symbol is a fancy word for named things in Perl. Variables, subroutines, and file handles
are all symbols.
22
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