squeak!
Syllabus Homepage
Course Overview
Course resources
Day 1
Day 2
Day 3
Day 4
Common errors
Internet Glossary
About Your Instructor
Credits: This site powered by the vi text editor, apache webserver, perl scripting, and Debian linux.
squeak!

Intermediate Perl - Day 3

Goals

In this session you will:
  • discuss OOP terminology
  • build and use class methods
  • use inheritance (is-a)
  • use overrides
  • use extensions
  • describe objects in classes
  • create instances (objects) with constructors
  • modify instances with instance methods
  • work with multiple objects

Some preliminary thoughts

  1. Use OOP where it makes sense. Don't use it where it doesn't make sense.
  2. "...perl isn't obsessed with enforced privacy... a Perl module would prefer that you stayed out of its living room because you weren't invited, not because it has a shotgun."
  3. "...a module [shouldn't] pollute any namespace it wasn't asked to pollute."
  4. "...generally you should treat the object as a black box. All access to the object should be obtained through the published interface via the provided methods."
  5. it is critical that you document the interface to your module

some definitions

In the perl context:
  • a module is the basic unit of perl re-usability. It usually contains a package/class, is named after that entity, and ends in .pm
  • a package is a namespace, usually found in a stand-alone module
  • a class is a definition of an object, its properties and behaviors.
  • an object is an instance (particular occurrence) of a class
  • an instance method is a class-based subroutine/function that defines behaviors for an object.
  • a class/static method is a class-based subroutine/function that does not apply to specific objects (constructors, aggregate functions, etc).

The difference between the class and the object (instance) may be reminiscent of the relationship between Plato's Ideal and Real.

Perl classes and OOP

  • class methods class->method(); syntax (note that this differs from &function(); syntax.
  • inheritance and multiple inheritance from base/parent classes
  • overriding (not overloading, which is different and is handled by module)
  • extension

Perl objects

  • starting simply: blessing a reference into a class
    $dogname = 'Fido';
    $dogObj = \$dogname;
    bless($dogObj, Pets);
    
  • Constructors
    The construction of a particular object is usually handled in an automated manner.
    package Pen;
    
    sub new {
    	#constructor
    	#USAGE: Pen->new;
    
    	#	the class is the first arg passed to a class function
    	my $class = shift; #@_ is assumed.
    
    	#	this will be cleaner when working with anonymous hashes and arrays
    	my $penType = 'not specified';
    	my $penRef = \$penType;
    
    	#	creates the (object) instance reference and return it.
    	bless $penRef, $class;
    
    }#end, constructor
    
    
    #thank you, drive through
    1;
    
    
    
    
  • passing an argument to the constructor
    
    
    package Pen;
    
    sub new {
    	#constructor
    	#USAGE: Pen->new("type of pen");
    
    	#	the class is the first arg passed to a class function
    	my $class 	= shift; #@_ is assumed. remember that shift is destructive
    	my $penType	= shift; #now the passed arg
    
    	#	this will be cleaner when working with anonymous hashes and arrays
    	my $penRef = \$penType;
    
    	#	created the object reference and return it.
    	bless $penRef, $class;
    
    }#end, constructor
    
    
    #thank you, drive through
    1;
    
    
  • Holding real object data: hashes instead of scalars
    
    package Pen;
    
    sub newValues {
    	#constructor
    	#USAGE $ref = Pen->newValues('pen type', 'pen color', 'ink color');
    
    	#	just doing this to keep your head straight 
    	my $class 		= $_[0];
    	my $penType		= $_[1];
    	my $penColor	= $_[2];
    	my $inkColor	= $_[3];
    
    	#	anonymous hash!
    	my $penRef = { 	penType => $penType, 
    					penColor => $penColor,
    					inkColor => $inkColor,
    					};
    
    
    	#	created the instance reference and return it.
    	bless $penRef, $class;
    
    }#end, constructor
    
    
    #thank you, drive through
    1;
    

Class methods v. Instance Methods

Class methods pass the class as the first arg; Instance/Object methods pass the instance reference as the first arg. You may want to test for this using ref on the first (ie, $_[0]) argument. It will return true (actually the name of the class) if it is an reference and false (ie, undefined) if it is a string (class name).

Homework

  • destructors
  • serialization and persistence of objects
  • regex debugging, profiling


http://www.mousetrap.net/syllabus/interperl/day3.html
$Id: day3.orb,v 1.6 2002/08/08 00:31:26 mouse Exp $

Remember, your login is based on your machine's hostname, not on any other number.
~/[initials] refers to the subdirectory under your homedir, named after your initials. Everything except for .dotfiles will be stored in your ~/[initials] directory.


© 1995-2001 jason carr
Distributed under the terms of the GNU Free Documentation License.