eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

introduction to Perl 5 for web developers
Defining a Class  
In Perl 5, a class is simply a package with subroutines that function as methods. Let's take a look at a Cat class:

     use strict;
     package Cat;
     sub new {
        my $class = shift;
        my $self  = {};
        bless $self;
        if (defined $_[0]) {
                $self->{'name'} = shift;
                }
        if (defined $_[0]) {
                $self->{'color'} = shift;
                }
        return $self;
     }

     sub meow {
        print "meow\n";
     }

     sub printDetails {
        my $self = shift;
        print "$self->{name}\n";
        print "$self->{color}\n";
     }

Okay, don't let the fact that we are involved with Object Oriented Design ruffle your feathers, the implementation of OOD is affected using the syntax we should already be very familiar with. There are only a few even remotely unusual things here. Let's take a look at the code:

     package Cat;

Not too frightening right? We simply specify a package name that we belong to. If you are not sure, a package is simply a group of related files. Packages are used primarily to define name space.

Next comes the definition of the "new" subroutine.

     sub new {
                        # Shift off first argument
                        # passed to this subroutine
                        # and assign it to the
                        # variable named $class

        my $class = shift;

                        # Create an anonymous hash
                        # and reference it using
                        # $self.

        my $self = {};

                        # bless $self so that the
                        # reference can be
                        # associated with its class.
        bless $self;

                        # If there is a second
                        # parameter passed to
                        # this subroutine, we can
                        # assign the value as the
                        # value to the key "name"
                        # in the anonymous hash
                        # referenced by $self.

        if (defined $_[0]) {
                $self->{'name'} = shift;
                }

                        # Third parameters should
                        # be handled the same
                        # way but for "color".

        if (defined $_[0]) {
                $self->{'color'} = shift;
                }

                        # return the reference.
        return $self;
        }

There are a few things to point out here. For one, Java developers can think of the new() subroutine as analogous to the constructor of a Java class. The new() method is used to define a new instance (or object) of the class.

Note also that this subroutine can be called with three parameters. In the case of the first one, which is assigned to the $class variable, Perl will pass this for us automatically. This is actually a Perl trick. Because new is called via a reference to the package, the first arguement that is based is always the name of the package itself as seen below:

# You can use the following to 
# call the new() method.

my $cat = new Cat();

# or

my $cat = Cat->new();

Likewise, you will see later that methods of the object are called using a reference to the object which Perl always passes as the first argument to a method. Note that the new() subroutine may also be called via a reference to the object, however, in this case, the above new() would not work because we only programmed it to expect a package name passed as a string.

The reasons why you might want to enable new() to be called from an object reference go beyond the scope of this document, but you can read more about it in perldoc perltoot or Damian Conway's book on Object-Oriented Perl Programming.

So back to discussing the new() subroutine.

In the case of $self->{'name'} and $self->{'color'}, the user may pass optional values. The constructor may be overloaded simply by passing it different arguments.

Okay, let's move on. The last two subroutines in the class are nothing new at all. They are just subroutine definitions. However, notice that these routines accept a reference to the current instance of the cat ($self) as the first argument.

     sub meow {
        my $self = shift;
        print "meow\n";
     }

     sub printDetails {
        my $self = shift;
        print "$self->{name}\n";
        print "$self->{color}\n";
     }

So essentially, this class defines the blueprint for the cat object. The new() subroutine is used to define the characteristics (fields, properties) that all cats share. In this case, all cats have a color and a name. We might imagine complexifying the new() subroutine to define eyeColor, weight, breed, etc.

Likewise, the subroutines besides new(), namely meow() and printDetails(), define the actions that any cat can perform. We might also add "ripCouch() and catchMouse() subroutines as we developed our object.

Nonetheless, the important thing to realize is that we have defined both properties and methods that are necessary to define a class in object oriented design. So how do we use this class? How do we instantiate an actual object? I want to see a cat named "Fred" who is white!

Previous | Next | Table of Contents