An Example: Using Object-Oriented Modules

Perl's sense of OOP is that much of it is optional; you can use only a little OOP, or go whole hog and OOP everything in sight if you want. It all depends on whatever's easiest, and how much of an OOP fanatic you are.

In many cases, the easiest way to use Perl's OOP in your scripts is to make use of the various CPAN modules in an object-oriented way, but not necessarily to structure your own scripts as a set of objects. Let's re-examine the work we did on Day 16, “Using Perl for CGI Scripting,” with CGI scripts and the CGI module. This module is written so that its subroutines can be used as ordinary subroutines or as object-oriented methods. Let's take an exercise we did at the end of that lesson—Exercise #1, a CGI script that does nothing but print the keys and values that were submitted to it—and use the CGI module in an object-oriented way.

Listing 20.1 shows the original script.

Listing 20.1. pairs1.pl
 1: #!/usr/bin/perl -w
 2: use strict;
 3: use CGI qw(:standard);
 4:
 5: my @keys = param();
 6:
 7: print header;
 8: print start_html('Hello!'),
 9: print "<H1>Key/Value Pairs</H1>
";
10: print "<UL
";
11:
12: foreach my $name (@keys) {
13:     print "<LI>$name = ", param($name), "
";
14: }
15: print "</UL>
";
16:
17: print end_html;
					

We use four of the CGI module's subroutines in this script: param (lines 5 and 13), which gives us both the full list of available keys and the values of specific keys; header (line 7), which prints a CGI header; start_html (line 8), which prints the top part of an HTML file; and end_html (line 17), which prints the tail end of an HTML file. In this previous example, we used these subroutines as regular subroutines.

But the CGI module can also be used as an object-oriented class. Create an instance of that class, and you can use those subroutines as if they were methods (because, well, they are). All this involves is one extra line of code and some slightly different syntax for the subroutines. I also removed the “standard” flag when I imported the CGI module because it's not necessary to import the standard methods when using it in object-oriented mode.) Listing 20.2 shows the result.

Listing 20.2. An Object-Oriented pairs1.pl
 1: #!/usr/bin/perl -w
 2: use strict;
 3: use CGI;
 4:
 5: my $obj = CGI->new();
 6: my @keys = $obj->param();
 7:
 8: print $obj->header();
 9: print $obj->start_html('Hello!'),
10: print "<H1>Key/Value Pairs</H1>
";
11: print "<UL
";
12:
13: foreach my $name (@keys) {
14:     print "<LI>$name = ", $obj->param($name), "
";
15: }
16: print "</UL>
";
17:
18: print $obj->end_html();
					

See the differences? There are effectively only two:

  • Line 5 is where we instantiate our CGI object and store a reference to it in the $obj variable. Following Perl conventions, the object constructor for the CGI class is called new.

  • All our subroutines—param, start_html, header, and end_html—are called as object methods by using dereferencing syntax (the object, ->, and the name of the method).

The end result? Exactly the same thing as the non-OOP version. The CGI module is written to work equally well as a regular collection of subroutines and as an object-oriented class.

Note that this example isn't a pure OOP program; we haven't created any of our own classes here. A “true” OOP script would put the bulk of the code into its own class, using the CGI module there, and then do little in the main package other than instantiate our class and call a method or two to get it running. This is one of the neat things about OOP in Perl—you can use only as much object-oriented programming as you need to. Unlike more strict OOP languages, you don't have to objectify the things that don't seem to fit into objects.

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

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