CGI and Object Oriented Perl: Background
May 10, 1999
By a wide margin, the most popular trend in modern programming
(straight from the catwalks of Paris) is object-oriented
programming. Perl has jumped onto the "OOP" bandwagon,
as it is known, beginning with Perl 5. Some programmers
swear by object-oriented programming while others loathe it;
Perl allows for either, in that OOP is there for those
who want to use it but not necessary for those who don't.
Taken in the right spirit, though, object-oriented programming
can make life easier.
As a web developer primarily interested in using a Perl as
a tool, you'll mostly deal with OOP as a user of
objects, rather than a creator of objects. That's good
-- using objects in Perl is no more complicated than anything
else we've seen in Part 1 of this introduction.
First, what is an object? Very simply and vaguely
put: an object is "a thing which does stuff".
This isn't as meaningless as it sounds. Consider your
kitchen stove. It is an object. The stove has a number of
characteristics and a number of actions that it is capable
of. Its characteristics, which we could call properties,
include its size, its color, how many burners it has, the
temperature ranges it can reach, and so on. The stove
also can perform various actions -- it can heat one or
more burners to varying degrees, it can heat the inner cavity
and maintain a certain temperature, etc.
When you use a stove, though, you interact with it through
a simple interface; the interface translates your
requests into actions that the stove takes care of on
its own. Thus, you simply turn a knob to 350 and the stove
knows what to do from there. This is how Perl objects
behave. Objects in Perl typically return a result from their
actions (one could say that the stove also returns a result:
heat).
The most commonly used object by web developers coding in Perl
is undoubtedly the CGI object. So, we can tackle
both the topic of
OOP
and a starter on
CGI at once with a
look at the CGI object in Perl.
Perl objects are contained within modules, and modules
are hunks of Perl which contain one or more miniature
tools that you can use. You can literally think of modules
as toolboxes within Perl. The standard Perl distribution
includes many of the most popular modules, but you must
specify at the start of your program which modules you
would like to use.
#!/usr/bin/perl
use CGI;
In the above embryonic Perl program, the use statement
names the module we would like to access from
this program. We happen to know that the CGI module contains
the CGI object (the resource list at the end of this
article contains links to information on other modules).
Perl Pattern Matching: Regular Expressions
The Perl You Need to Know
Notes on Running CGI
|