ACCC Home Page ACADEMIC COMPUTING and COMMUNICATIONS CENTER
Accounts / Passwords Email Labs / Classrooms Telecom Software Computing and Network Services Education / Teaching Getting Help
 
Seminar - Perl II
0. Contents 1. Intro 2. Sources 3. References 4. Packages 5. Classes
6. Ties & DBM 7. Typeglobs 8. CGI 9. Security 10. Example  

Classes

   
 
     
Object Oriented Programming
 

I won't even pretend to be a tutorial or introduction to object oriented programming. I presume you already know what a class is, how an instance differs, about class methods and instance methods as well as static data and instance data, and the notions of inheritance and polymorphism. And, of course, good design and technique.

If you're not an expert, though, don't worry. I mostly want you to understand perl's notation and implementation, so that you can recognize it in other people's code.

Perl is considerably more relaxed than java or C++. The additional constructs needed for OO programming are surprisingly small in perl, yet they do a lot. The main additions are:

  • A new syntax for calling methods.
  • bless() function
  • @ISA arrays to keep track of inheritance trees
  • Optional DESTRUCTOR methods
  • UNIVERSAL class with 2 or 3 builtin methods
If you understand the new syntax for methods, that's all you need to use other people's classes.
 
     
Classes
 

A class normally provides the definitions of static and instance data, of constructors and destructors, and of class and instance methods. In perl, a class is just a package. Perl uses the notion of package to group class-related things together. A class package is no different than a normal package. No new syntax is needed.

 
     
Instances
 

An instance is a blessed reference. That is, it is a normal reference. But when you use the bless function, the thing that is referenced records what class it is in. The point of the blessing is so appropriate packages (i.e. classes) can be searched when methods are called.

The reference can be to anything. The thing referred to normally holds the instance data. People normally use hashrefs, and use the hash as a mini-namespace to hold instance data. This is particularly simple and natural in perl. But your instances can be quite complicated if you want them to.

 $a = {};
 print ref $a;         # hash
 bless $a, "MyClass";  # now $a is an instance
 print ref $a;         # MyClass
 $b = $a;
 print ref $b;         # MyClass
Note that the thing being referenced gets the blessing, not the reference itself.
 
     
Methods
 

A method is a subroutine. Just an ordinary subroutine, with a convention about the first argument passed. No new restrictions or syntax to writing the subroutine, however there is some new syntax when calling subroutines in an OO way.

 
     
Class (Static) Methods
 

Class methods are normal subroutines that expect the first agument to be the name of the class. In particular the constructor new is no different from any subroutine, and you don't even have to use the name "new"

When calling a class method, you have 2 options:

 $large   = Pizza->new;           # preferred
 $stuffed = new Pizza;            # possible
 $pan     = Pizza::new("Pizza");  # non-OO
 $basic   = Pizza->new("cheese"); # with args
 $pep     = new Pizza "pepperoni";# more args

 package Pizza;
 sub new { my $class=shift; bless {}, $class}
 
     
Instance Methods
 

Instance methods are normal subroutines that expect the first argument to be an instance (i.e. a blessed reference).

 $large->add("mushroom");          # preferred
 add $stuffed "spinach";           # possible
 print $pep->has("indigestion");

 package Pizza;
 sub add {my $self=shift; $self->{shift()}=1}
 sub has {my $self=shift; exists($self->{shift()}) ? 1:0 }
A method can be both Class and Instance, if it tests the first argument and acts accordingly.
 
     
Inheritance
 

Perl does method inheritance; in fact it does multiple inheritance. Each class knows who its parents are. If a method is not found, the parents are searched depth-first.

Each class contains an array, @ISA. Simply list the parent classes in this array, and you have determined the inheritance path.

Perl does not do data inheritance. This is normally not a problem, particularly when objects are implemented as hashes. In fact, multiple inheritance is normally more of a problem with data than with methods, so perl provides the best of both.

 
 

Perl II Previous: 4. Packages Next: 6. Ties & DBM


1999-3-3  BobG
UIC Home Page Search UIC Pages Contact UIC