Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Object Oriented Perl Programming Issac Goldstand Mirimar Networks

Similar presentations

Presentation on theme: "Introduction to Object Oriented Perl Programming Issac Goldstand Mirimar Networks"— Presentation transcript:

1 Introduction to Object Oriented Perl Programming Issac Goldstand Mirimar Networks

2 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

3 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

4 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

5 Interface Class name (eg, “Ball”) Methods (eg, “throw”, “catch”, “bounce”) Properties (eg, “color”, “type”) Constructors

6 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

7 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; }

8 Simple Ball Class - Property sub color { my $self=shift; if (@_) {$self->{COLOR}=shift;} print “COLOR is “, $self ->{COLOR},”\n”; return $self->{COLOR}; }

9 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”; }

10 Simple Ball Class – Usage my $ball=new My::Ball; $ball->bounce; $ball->color(“red”); $ball->throw;

11 Simple Ball Class – Output I blessed My::Ball=HASH(0x182e602) into My::Ball I bounced the ball. COLOR is red I threw the red ball.

12 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)

13 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)

14 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” }

15 Inheritance example - usage my $bball= My::Ball::BasketBall->new; $bball->color(“orange”); $bball->bounce; $bball->shoot;

16 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!!!

17 Summary What is Object Oriented Programming? How does Perl allow one to create objects? Examples of simple objects Inheritance

18 What’s next? Destructors Multiple inheritance UNIVERSAL object Closures as objects Using AUTOLOAD to proxy methods

19 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

20 Thank You! For more information: Issac Goldstand

Download ppt "Introduction to Object Oriented Perl Programming Issac Goldstand Mirimar Networks"

Similar presentations

Ads by Google