Objected Oriented Perl - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Objected Oriented Perl

Description:

Objected Oriented Perl – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 19
Provided by: PaulL155
Category:
Tags: img | objected | oriented | perl

less

Transcript and Presenter's Notes

Title: Objected Oriented Perl


1
Objected Oriented Perl
  • An introduction

2
Classes in Perl
  • A class is defined by storing code which defines
    it in a separate file, and then useing that file
  • The file must be named with the name of the class
    (starting with an capital letter), followed by
    the extension .pm
  • After the shebang in your main file, this line
    of code
  • use Classname
  • You can then create instances of the class
    anywhere in your file.

3
Defining an Object
  • In Perl, an object is simply a reference
    containing the members of a class.
  • typically, a reference to a hash, but can be any
    kind of reference
  • The reference becomes an object when it is
    blessed when you tell Perl the reference
    belongs to a certain class.

4
Simple Example
  • package Student
  • obj Name gt Bob,
  • ID gt 123
  • bless (obj, Student)
  • obj is now an object of the class Student
  • package Student is the first line of your .pm
    file. It identifies all following code as
    belonging to this class/package/module

5
Constructors
  • Unlike C, a Constructor in Perl is simply
    another subroutine. Typically named new, but
    you can give it any name you want.
  • package Student
  • sub new
  • my ref Name gt , ID gt 0
  • bless (ref, Student)
  • return ref
  • In this example, dont actually have to give ref
    any elements. You can define them all in a later
    subroutine, if you choose.

6
Calling the Constructor
  • As you may be able to guess, TMTOWTDI
  • student new Student
  • student Student-gtnew
  • student Studentnew(Student)
  • First two methods get translated to 3rd method
    internally by perl. This has beneficial
    consequences

7
Arguments to Constructor
  • (actually, this applies to arguments to any class
    method)
  • Every time the constructor is called, first
    argument to function is the name of the class.
  • Remaining arguments are caller-defined
  • obj new Student (Bob, 123)
  • obj Student-gtnew(Bob, 123)
  • obj Studentnew(Student, Bob, 123)
  • So, when defining constructor, often see this
  • sub new
  • my class shift
  • my (name, ID) _at__
  • my ref Name gt name, ID gt ID
  • bless (ref, class)
  • return ref

8
More Methods
  • Within the .pm file, any subroutines you declare
    become methods of that class.
  • For all object methods, first argument is always
    the object that the method is being called on.
    This is also beneficial
  • sub name
  • my ref shift
  • my name shift
  • return ref-gtName if name eq undef
  • ref-gtName name
  • To call this method
  • obj-gtname(Bob)
  • Perl translates this to
  • Studentname(obj, Bob)

9
One more thing
  • You need to place the following statement at the
    end of your .pm file
  • 1
  • This is because the use keyword needs to take
    something that returns a true value. Perl
    returns the last statement evaluated.

10
?Be Kind to One Another ?
  • Note that class variables are not strictly
    private in the C sense.
  • There is nothing preventing the user of your
    class from modifying the data members directly,
    bypassing your interface functions.
  • Perls general philosophy is If someone wants to
    shoot himself in the foot, who are you to stop
    him?
  • When using other peoples classes, its almost
    always a better idea to use the functions theyve
    given you, and pretend you cant get at the
    internal data.
  • There are, of course, methods you can use to
    prevent users from doing this.
  • Really not worth the trouble
  • Significantly beyond scope of this course

11
Standard Modules
  • Perl distributions come with a significant number
    of pre-installed modules that you can use in your
    own programs.
  • These files are found in system-specified
    directories.
  • To find where the files are located on your
    system, examine the _at_INC array
  • print _at_INC\n
  • Gives a listing of all directories that are
    looked at when Perl finds a use statement.

12
Standard Module Example
  • Well look at a pre-defined class that implements
    Complex-number support in Perl.
  • This module is defined in a subdirectory of the
    include path, called Math.
  • use MathComplex
  • The constructor for this class is called make,
    not new.
  • z MathComplex-gtmake(4,3)
  • Creates an instance of the class Complex.
  • z can be thought to hold the value 4 3i

13
More MathComplex
  • Complex.pm is also good enough to overload the
    basic mathematical operators to work with members
    of the complex class
  • Overloading operators is beyond the scope of this
    lecture read the Camel chapter 13 for more info
  • It also defines a constant i sqrt(-1)
  • use MathComplex
  • z MathComplex-gtmake(3,5)
  • print z\n
  • z z 4
  • print z\n
  • z z 3i
  • print z\n

Prints 3 5i 7 5i 7 2i
14
Privacy?
  • If you examine the Complex.pm file, youll see
    that the internal structure of the class is (at
    least partially) represented by an array
    reference called cartesian. Perl will let you
    modify this directly
  • z MathComplex-gtmake (3,5)
  • z-gtcartesian0 40
  • Dont do that. Instead, use the functions
    provided by the class Re() and Im()
  • z-gtRe(40) set real part of z to 40
  • img z-gtIm() get img. part of z

15
Pragmatic modules
  • Some modules do not define classes, but rather
    effect how your perl script is compiled.
  • These modules are called pragmatic modules or
    just pragmas.
  • By convention, they should be all lower-case.
    (whereas class modules start with a capital)
  • Youve already seen two of these pragmas
    warnings and strict.

16
use and no
  • Certain pragmas take a list of options, declaring
    which policies of the pragma to import.
  • use warnings qw(syntax numeric)
  • This will only give warnings that fall into the
    syntax or numeric categories
  • See Camel page 863 for a full list
  • Pragmas can be turned off using no
  • use warnings
  • no warnings digit
  • something that would give warnings

17
More Standard Pragmas
  • use integer
  • print 10/3, 10/3 prints 3
  • no integer
  • print 10/3, 10/3 3.333333333
  • use constant PI gt 4 atan2 1, 1
  • See chapter 31 of Camel for more

18
Help on Standard Modules
  • For documentation for all these modules, you have
    several resources
  • Camel, chapters 31 32
  • Unix program perldoc
  • ex, perldoc MathComplex
  • CPAN http//www.cpan.org
Write a Comment
User Comments (0)
About PowerShow.com