Chapter 7. Packages, Modules, and Objects

Over the years, Perl has evolved from a utilitarian scripting tool into a sophisticated object-oriented programming language. Many people continue to use Perl just for simple scripts, and Perl will continue to make simple tasks easy. However, Perl can also make difficult tasks possible by writing reusable code and using object-oriented programming techniques.

This chapter explains what Perl modules are and how to use them in your programs. Modules are written to accomplish tasks that either aren’t implemented by Perl’s built-in functions, or that could be done better. We say modules are “reusable” because anyone who needs to accomplish the same task can use that module instead of writing the code from scratch. As you write more and more Perl code, you’ll undoubtedly find yourself using many of the modules other Perl programmers have provided. You may also find yourself writing modules and making them available for others to use.

The remainder of this book describes a significant portion of the functionality that’s present in publicly available Perl modules. You’ll find that a number of standard or core modules are distributed with Perl; many of these modules are discussed in Chapter 8. Scores of other modules are available on CPAN, and virtually any task you’d like to accomplish in Perl is implemented in a module found there. For unbundled modules, you’ll need to install the module on your system and integrate it into your program with the use function.

The use function is often the key to working with modules. For example, to bring the functionality of the popular CGI module into your program, you need to install the CGI.pm module (the .pm stands for Perl module) and put this line near the top of your program:

use CGI;

Now your program can use the many functions and variables made available by the CGI module.

Packages (from which modules are built) are also a mechanism by which Perl’s object-oriented features are implemented. But object-oriented programming isn’t for everyone, and there’s nothing in packages that forces the programmer to work with the object-oriented paradigm.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.181.144