up
Perl Lecture Notes - November 9
- Questions
- stfw assignment - Jim's version
- code - stfw source
- sample output - stfw.out
- Note that differences between absolute and relative URLs.
- Making all the pieces of this work is not
a trivial exercise - but playing with it can teach you a lot.
- Perl packages
- Basic ideas -
packageSample.pl
- a package is a namespace
for "our" variables and subroutine names.
- the "package" declaration lets you enter a package
at any time, in any file.
- There are some special package-specific "subroutines"
which get called at different phases of a packages
existance. For example, a block like this
BEGIN { # your code here }
is called before anything else. See "perldoc perlmod"
and the camel for the details.
- There is NO explicit relationship between
"package Jim::Mahoney" and "package Jim" - these
are just two different package names.
- NOT the same as Java package.
A Java package spans multiple files, while
typically a perl package is one file. Think of
a Perl package as being like a "class" in Java.
- Perl Modules - "package Math::Big" starts the file Math/Big.pm
- Typical use of a package is to put one per file,
with a specific correspondance between file name
and package name. These are called "perl modules",
and always end in .pm
- Another file can then "load" that module with the
command "use Math::Big" which searches for Math/Big.pm
in the list of @INC directories.
- An "export" mechanism allows variables and subroutine
names from Math::Big to be defined in the file that
"uses" Math::Big. You can define which variables
are exported automatically, and which may be exported
explicitly by the caller.
- Example :
moduleSample.pm and
testModuleSample.pl
- Other Notes:
- There are several other ways to include
the code from one perl file into another. See "require",
"do", and "eval". The most common, though, is "use".
- All perl modules must evaluate to true.
Typically one puts a line "1;" at the end.
(The historical reason for this was to allow packages
to signal that they hadn't been loaded properly.)
- The global special variable __PACKAGE__
(that's two _ chars on each end) has the name
of the current package.
- Perl Objects
- Basic idea
- an "object" is something that has both data and methods
associated with it. Perl accomplishes this with
references and packages.
- pg 310 of the camel:
An object is simply a reference ... er a referent.
A class is simply a package.
A method is simply a subroutine.
- extra pieces added to the language :
(1) "bless" associates a reference (well, its value, really,
but that distinction is usually unimportant) with
a package. This is an additional property that perl
values possess.
(2) additional syntax for invoking a subroutine from
a package associated with a given reference. There
are two forms, an "arrow" form and an "indirect" form.
- an Example: basicObjectSample.pl
- Is this clean? Is it an after-the-fact-hack thrown
on top of the language? Hmmm...
- A few of the details :
- Inheritance :
@ISA = (Parent, OtherParent); # defines a method search path
- Many special methods : DESTROY, AUTOLOAD, TIE, ...
- You can "overload" operators on these objects, so that
"$a + $b" calls a method you specify for add($a, $b).
(Assuming $a or $b is a blessed references.)
- There are "use Class::Struct" and other handy templates
on CPAN.
- Check out the readings (as described below) for all
the nitty-gritty specifics.
- Other notes :
- object methods need not be EXPORT-ed,
so using this approach avoids namespace pollution.
use Some::Module;
my $a = new Some::Module;
$a->doThis( how=>'fast' );
- subroutines may be either, in Java terms,
"static" or "instance" depending on how you treat
the arguments passed to them.
- data may likewise be (in Java terms), "static" or
"private" or "public" depending on where you put it
(in package file vs in object hash)
and how you advertise the interface.
- issues of privacy tend to be more by convention
than by fiat - Perl takes that attitude that
closing the door without locking it is suffienct.
- More Examples
- Node.pm - an element in a recursive data tree
- inheritance example - coloredNode
- Java Objects with corresponding Perl code :
perlVSjava
- Assignment
- Read about objects and packages in perl.
I've used
bold for what I consider to be the most important ones,
but there are a variety of places to read about this stuff.
- online , at Perl 5.6 docs at /~msie/2002/ipl/perl/docs :
- camel :
- browse chapter 10, on Packages, and 11, on Modules.
- read chapter 12 on Objects
- library (or buy another book) :
- Do as many of the exercises in the "very very short
tutorial" (see above) as seem worthwhile.
- Pick a perl module .pm of your choice from the standard
modules. (The directories on your machine are those output
from perl -e "print join(' ',@INC)") Examine how it
creates and uses perl objects.
On bob,
/usr/lib/perl5/site_perl/5.6.1/WWW/RobotRules.pm
might be a reasonable
choice. I'm not suggesting you read every detail; just
search for "sub new" and see how it creates a "ping" object,
and notice how, say, "sub allowed" takes $self as the
first argument, and how the POD documentation is part of the file.
(This is an open-ended
invitation to dive into some of the source code - that's
what open source is all about.)
- Choose one of the object-oriented Java thingies you've
written earlier this trimester, or a simple example from the
Java book. Convert that functionality and interface into a
Perl object. (This needn't be more than one or two classes
and a handful of methods and data; the idea is just to get a
feel for the syntax and style of Perl objects.)
So there you are.