There is no way to avoid it ”we have to talk about object-oriented programming (OOP) using Perl. Object-oriented programming is not a big deal though; it is just a way to give an object (a thing) actions (behaviors, also known as methods ). Let's say we have an object named $car . We can drive the car by executing that object's drive() method, using the nifty arrow notation: $car->drive() . Easy, eh? Well, of course it is not so easy, so we suggest that if you want an excellent discussion of all things OOP in Perl, see Objected-Oriented Perl [Conway 99]. Most of the time, when we do OOP in Perl, we will use objects that others have defined. In Chapter 7, when we learn how to do CGI programming with Perl, we will use a module, or object class definition, named CGI.pm . We will use this module with a pragma named (believe it or not) use , as in: use CGI; A Perl pragma is a message to the compiler to behave in a way that is not the default ”in the use pragma, we are telling the compiler to use a specific Perl module that it does not normally include, telling Perl to locate the module file CGI.pm and do some OOP magic, making all the methods, or object functions, available to us. For instance, in the CGI module, a method named header() is defined, and because we told Perl we want to use the CGI module, we can call this method. Once we have use d a module, we can then usually create an object of that type by executing new , which is a method that is defined to construct the object: $obj = new CGI; Once an object is created, methods can be invoked by using the arrow notation: $obj->header(); $obj->pre(); $obj->b(); This syntax means "execute the header() (or pre() or b() ) method using the object named $obj , which is an object of class CGI ." Hundreds of modules are available for Perl. You can find them all for free at the Comprehensive Perl Archive Network (CPAN) ”www.cpan.org/. We suggest that you check out what is there. You might find that the problem you are trying to solve, or will need to solve in the future, has a module written for it that could make your life easier. Installing a module is a snap. First, find the module you want at www.cpan.org. Let's say we want the module Nifty . We find the latest version of Nifty , let's say version 1.03. We download the tarball from CPAN named Nifty-1.03.tar.gz and save it somewhere convenient , let's say /tmp . Then, all we do is this:
# cd /tmp # tar xzvf Nifty-1.03.tar.gz # cd Nifty-1.03 # perl Makefile.PL # make # make test # make install Now, Nifty.pm is installed. To use it, we add this to our Perl scripts: use Nifty; That is all you need to know to be able to use the modules in this book. If you are interested in the workings of objects or are going to be creating your own classes, read Object-Oriented Perl [Conway 99] ”you will be happy you did. |