Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Defining a Class

September 28, 1998

In Perl 5, a class is simply a package with subroutines that function as methods. Let's take a look at a Cat class:

     #!/usr/sbin/perl
     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:

     #!/usr/sbin/perl
     package Cat;

Not too frightening right? We name the perl interpreter to use and 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. However, 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.

     sub meow {
        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 develope 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!

Object Oriented Programming in Perl
Introduction to Perl 5 | Table of Contents
Perl Objects



Up to => Home / Authoring / Languages / Perl / 5




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers