Introduction to Object Oriented Perl Programming - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Introduction to Object Oriented Perl Programming

Description:

Methods (eg, 'throw', 'catch', 'bounce') Properties (eg, 'color', 'type' ... I bounced the ball. COLOR is red. COLOR is red. I threw the red ball. Inheritance ... – PowerPoint PPT presentation

Number of Views:110
Avg rating:3.0/5.0
Slides: 21
Provided by: issacgo
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Object Oriented Perl Programming


1
Introduction to Object Oriented Perl Programming
  • Issac Goldstand
  • Mirimar Networks
  • www.mirimar.net
  • margol_at_mirimar.net

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 thats
    logically connected
  • Each object has a specific purpose multiple
    objects are placed together like building blocks
  • Objects are built by defining classes 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 MyBall
  • sub new
  • my protoshift
  • my classref(proto) proto
  • my self
  • bless self,class
  • print I blessed self into class\n
  • return self

8
Simple Ball Class - Property
  • sub color
  • my selfshift
  • if (_at__) self-gtCOLORshift
  • print COLOR is , self
  • -gtCOLOR,\n
  • return self-gtCOLOR

9
Simple Ball Class Method
  • sub bounce
  • my selfshift
  • print I bounced the ball.\n
  • sub throw
  • my selfshift
  • print I threw the ,self-gtcolor, ball.\n

10
Simple Ball Class Usage
  • my ballnew MyBall
  • ball-gtbounce
  • ball-gtcolor(red)
  • ball-gtthrow

11
Simple Ball Class Output
  • I blessed MyBallHASH(0x182e602) into MyBall
  • I bounced the ball.
  • COLOR is red
  • 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 _at_ISA array
  • _at_ISA must be a package-local definition, and not
    a file-scoped lexical (dont 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 selfSUPER-gtmethod(_at_args)

14
Inheritance example
  • Package MyBallBasketBall
  • _at_ISAqw(MyBall)
  • sub bounce
  • print I dribbled the ball.\n
  • sub shoot
  • my selfshift
  • self-gtSUPERthrow()
  • print He shoots He scores!!!\n

15
Inheritance example - usage
  • my bball
  • MyBallBasketBall-gtnew
  • bball-gtcolor(orange)
  • bball-gtbounce
  • bball-gtshoot

16
Inheritance example Output
  • I blessed MyBallBasketBallHASH(0x1f11254)
    into MyBallBasketBall
  • 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
Whats next?
  • Destructors
  • Multiple inheritance
  • UNIVERSAL object
  • Closures as objects
  • Using AUTOLOAD to proxy methods

19
For more information
  • perldoc perltoot Toms 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
margol_at_mirimar.net http//www.beamartyr.net/ http
//www.mirimar.net/
Write a Comment
User Comments (0)
About PowerShow.com