Function based programming
• Multiple methods in a shared namespace
• Methods fulfill a generic action and require
all variables to be passed (or global /
otherwise in scope)
• No repetition of method names in a single
namespace
Objects
• Allow us to add somewhat more
abstraction to code
• An “object” represents a chunk of code
that’s logically connected
• Each object has a specific purpose –
multiple objects are placed together like
building blocks
• Objects are built by defining “class”es
which contain all the logic and expose
the user interface
Classes
• Namespace containing all of the private
information that makes the object work
• Interface is the way users manipulate the
object
• Implementation is the private set of
functions and variables that happens
inside
Interface
• Class name (eg, “Ball”)
• Methods (eg, “throw”, “catch”, “bounce”)
• Properties (eg, “color”, “type”)
• Constructors
Perl Considerations
• Widely held opinion is that Perl5 object
capabilities suck
• An object is simply a special reference,
“blessed” into a “class” (read: package)
• Any reference can be used, though
hashrefs tend to be the popular choice
• Perl6 is supposed to improve this
Simple Ball Class - Constructor
package My::Ball;
sub new {
my $proto=shift;
my $class=ref($proto) || $proto;
my $self={};
bless $self,$class;
print “I blessed $self into $class
n”;
return $self;
}
Simple Ball Class - Property
sub color {
my $self=shift;
if (@_) {$self->{COLOR}=shift;}
print “COLOR is “, $self
->{COLOR},”n”;
return $self->{COLOR};
}
Simple Ball Class – Method
sub bounce {
my $self=shift;
print “I bounced the ball.n”;
}
sub throw {
my $self=shift;
print “I threw the ”,$self->color,
“ ball.n”;
}
Simple Ball Class – Usage
my $ball=new My::Ball;
$ball->bounce;
$ball->color(“red”);
$ball->throw;
Simple Ball Class – Output
I blessed
My::Ball=HASH(0x182e602) into
My::Ball
I bounced the ball.
COLOR is red
COLOR is red
I threw the red ball.
Inheritance
• Allows classes which share methods to
use a single code base
• Perl5 allows for inheritance from multiple
parent objects
• Parent objects marked using @ISA array
• @ISA must be a package-local definition,
and not a file-scoped lexical (don’t use
my)
Overridden methods
• A subclass can either use the method
definition defined in the parent class or
override it and define its own method
• To override, just simply implement the
overridden method like any other
• To explicitly access a method in the parent
class, use $self::SUPER->method(@args)
Inheritance example
Package My::Ball::BasketBall;
@ISA=qw(My::Ball);
sub bounce {
print “I dribbled the ball.n”;
}
sub shoot {
my $self=shift;
$self->SUPER::throw();
print “He shoots… He scores!!!n”
}
Inheritance example - usage
my $bball=
My::Ball::BasketBall->new;
$bball->color(“orange”);
$bball->bounce;
$bball->shoot;
Inheritance example – Output
I blessed
My::Ball::BasketBall=HASH(0x1f1
1254) into My::Ball::BasketBall
COLOR is orange
I dribbled the ball.
COLOR is orange
I threw the orange ball.
He shoots... He scores!!!
Summary
• What is Object Oriented Programming?
• How does Perl allow one to create
objects?
• Examples of simple objects
• Inheritance
What’s next?
• Destructors
• Multiple inheritance
• UNIVERSAL object
• Closures as objects
• Using AUTOLOAD to proxy methods
For more information
• perldoc perltoot – Tom’s Object Orienteted
Tutorial
• Object Oriented Perl (Damian Conway)
• Many articles out there, but the above
should get you well covered
Thank You!
For more information:
Issac Goldstand
margol@mirimar.net
http://www.beamartyr.net/
http://www.mirimar.net/
0 comments
Post a comment