Intro to References and ObjectOriented Perl - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Intro to References and ObjectOriented Perl

Description:

Intro to References and ObjectOriented Perl – PowerPoint PPT presentation

Number of Views:217
Avg rating:3.0/5.0
Slides: 44
Provided by: cis55
Category:

less

Transcript and Presenter's Notes

Title: Intro to References and ObjectOriented Perl


1
Intro to References and Object-Oriented Perl
  • CS640
  • November 9, 2004
  • Fran Fabrizio

2
References
  • Similar to a pointer in C
  • Two types, symbolic and hard
  • Symbolic
  • _at_john (47,brown,186)
  • _at_mary (23,hazel,128)
  • _at_vitals ('john','mary')
  • vitals0 is a 'symbolic reference' because its
    value is the name of another variable, which can
    be looked up in the symbol table and used to get
    at the value of that other variable.
  • Thus vitals01 john1 'brown'

3
Hard References
  • Hard references are the real references
  • Refers directly to the actual value (referent)
  • This is actually how perl variables work. The
    symbol table entry for _at_myarr holds a reference
    to _at_myarr's value. (You can then create
    additional references to that value)

4
The Backslash Operator
  • You use the \ operator to create hard references
  • _at_myarr (1,2,3)
  • myref \_at_myarr
  • Note that references are scalars, even if the
    values that they reference are not

5
Reference Examples
  • scalarref \foo
  • hashref \ENV
  • arrayref \_at_ARGV
  • coderef \mysubroutine
  • globref \STDOUT
  • constref \3.14159
  • This one is not like the others....

6
Anonymous Data
  • In the previous examples, we were making
    references to existing variables
  • myref \_at_myarr
  • You can also reference anonymous data
  • constref \3.14159
  • arrref 1,2,3,4
  • These values are only available through their
    references. There is no variable for them.
    Thus, they are anonymous.

7
Creating Anonymous Data
  • Scalars
  • scalarref \3.14159
  • Arrays
  • _at_array (1,2,3) arrref \_at_array vs.
  • arrref 1,2,3
  • Hashes
  • hash ('apple' gt 'green') vs.
  • hashref 'apple' gt 'green'

8
Using Hard References
  • When you want to use the value that a reference
    refers to, you dereference it
  • One way to do this is to use the reference as a
    variable name
  • scalar 5
  • ref \scalar
  • print ref Prints 5
  • Array example arrayref0

9
Three ways to dereference
  • Variable as variable name
  • arrayref0
  • Dereferencing a block
  • arrayref0
  • Using arrow notation
  • arrayref-gt0
  • Only used for arrays, hashes and coderefs

10
Dereferencing a Block
  • sub mysub ...
  • hash myname gt 'Fran',
  • mycode gt \mysub
  • hashmycode (1,2,3)
  • hashmycode returns a reference to a
    subroutine, which we dereference and call with
    the parameters (1,2,3)
  • Same as mysub(1,2,3) and mysub(1,2,3)

11
Using -gt notation
  • Arrays
  • _at_array ('red','green','blue')
  • arr_ref \_at_array
  • arr_ref-gt1 is 'green'
  • Hashes
  • candy 'taste' gt 'sweet',
  • 'calories' gt 'high'
  • candy-gttaste returns 'sweet'
  • candytaste does not work!

12
Arrow Notation Continued
  • array3-gtEnglish-gt0 Jan
  • Can chain -gt together
  • Here, array3 is expected to contain a hashref,
    and then you expect the referent hash to have a
    key English and the value tied to key English
    to be an arrayref.
  • Even if arrow3 doesn't exist, Perl will create
    it for you
  • Autovivification
  • This can be tricky, watch out!

13
Arrow Notation Continued
  • Is always optional between brackets or braces
  • array3English0 Jan
  • This is what's happening under the hood with
    multidimensional arrays
  • array021 is the same as array0-gt2-gt1

14
The Rules of Perl OO
  • To create a class, build a package
  • To create a method, write a subroutine
  • To create an object, bless a referent

- From Object-Oriented Perl by Damian Conway
15
Creating a Class
  • package DNA

That's it!
16
Creating a Method
  • package DNA
  • sub print
  • Put some code here that knows how
  • to print DNA
  • That's it!

17
Using The Class
  • package main
  • now, assume dna holds a reference to
  • a DNA object. We'll cover that magic later
  • dna-gtprint()

18
Just Another Reference
  • arrayref-gtindex
  • hashref-gtkey
  • coderef-gt(_at_args)
  • objref-gtmethod(_at_args)

19
The Hidden Argument
  • When you call an object method, the first
    argument is always a reference to the calling
    object.
  • objref-gtmethod(_at_args)
  • is really doing
  • objref-gtmethod(objref, _at_args)
  • Methods need access to the objects they work on
    this is Perl's way

20
self
  • So DNAprint(_at_args) is NOT the same as
    dnaobj-gtprint(_at_args)
  • dnaobj-gtprint(_at_args) is really
    dnaobj-gtprint(dnaobj, _at_args)
  • package DNA
  • sub print
  • my (self, _at_args) _at__
  • ...

21
bless
  • Perl objects are not a separate data structure
    perl objects can be hashes, arrays, even scalars
    any type of perl variable can be an object
  • bless() is how you take an ordinary perl variable
    and tell it that it is a member of a particular
    class

22
Making a DNA object
  • seq
  • _name gt 'NFKB1',
  • _sequence gt 'ACCTGATGTTA',
  • _location gt '4q24',
  • bless seq, 'DNA'
  • Now seq refers to an object of class DNA. seq
    is untouched but its referent is now labeled as
    type 'DNA'.

23
Constructors
  • Classes can make their own objects
  • package DNA
  • sub new
  • my class _0
  • my objref _name gt _1,
  • _sequence gt _2,
  • _location gt _3,
  • bless objref, class
  • return objref redundant

24
Constructors
  • seq DNAnew('DNA', 'NFKB1',
  • 'ACTTGTCACAAGG', '4q24')
  • seq-gtprint()
  • This works, but we have to type the class name
    'DNA' twice, which is awkward.

25
Constructors
  • seq DNA-gtnew('NAME','ACGAA','11q2')
  • Perl will automatically pass the name of the
    class as a string as the first parameter if the
    left side of the -gt is a class name instead of a
    specific object
  • So the above is implicitly
  • seq DNA-gtnew('DNA','NAME','ACGAA','11q2')

26
Constructor, Simplified
  • package DNA
  • sub new
  • bless _name gt _1,
  • _sequence gt _2,
  • _location gt _3 , _0

27
Constructor, Clarified
  • Named arguments
  • package DNA
  • sub new
  • my (class, arg) _at__
  • bless _name gt argname,
  • _sequence gt argsequence,
  • _location gt arglocation ,
    class

28
Constructor, Clarified
  • Now the constructor is self-documenting
  • my dna
  • DNA-gtnew( name gt 'NFKB1',
  • sequence gt 'ATTCGCA',
  • location gt '5q2',
  • )

29
Data Access
  • Accessors (read-only)
  • sub name _0-gt_name
  • or perhaps more clearly
  • sub name
  • my self shift
  • return self-gt_name
  • ---------------
  • print dna-gtname()

30
Good Manners Not Enforced
  • The OO way
  • dna-gtname()
  • But Perl will also let you do
  • dna-gt_name 'Foo'
  • just like any other hashref
  • Perl does not by default enforce OO methodology,
    so be careful. There are ways to ensure that
    private data cannot be directly accessed/modified
    like this.

31
Data Modification
  • Mutators (read/write access)
  • sub name
  • my (self, name) _at__
  • self-gt_name name if name
  • return self-gt_name

32
Cleaner Data Access
  • Use get and set methods
  • sub get_name _0-gt_name
  • sub set_name
  • my (self, name) _at__
  • self-gt_name name

33
Methods
  • Expand the print() method
  • sub print
  • my self shift
  • print Name self-gt_name \n
  • print Sequence self-gt_sequence \n
  • print Location self-gt_location \n\n

34
Methods
  • Finding the complement string
  • sub reverse
  • my self shift
  • my revseq reverse self-gt_sequence
  • revseq tr/ACGTacgt/TGCAtgca/
  • return revseq

35
Methods
  • Validating the sequence
  • sub validate
  • my self shift
  • my stub self-gt_sequence
  • stub tr/ACTGactg//d
  • return length(stub) 0

36
Putting It All Together
  • Create a module, and name the file the same as
    your package (DNA.pm)
  • package DNA
  • use strict useful in all Perl
  • sub new
  • as before

37
Putting It All Together (2)
  • accessors
  • sub get_name _0-gt_name
  • sub get_sequence _0-gt_sequence
  • sub get_location _0-gt_location
  • mutators
  • sub set_name
  • my (self, name) _at__
  • self-gt_name name

38
Putting It All Together (3)
  • sub print as before
  • sub reverse as before
  • sub validate as before
  • don't forget this guy last
  • 1

39
Using the DNA class
  • !/usr/local/bin/perl
  • use strict
  • use DNA
  • my dna
  • DNA-gtnew( name gt 'Kappa Inhibitor',
  • sequence gt 'ATGTTCAAA',
  • location gt '8q12'
  • )

40
Using The DNA Class (2)
  • Print summary of the sequence
  • dna-gtprint()
  • Use an accessor
  • print Meet me at dna-gtget_location() \n
  • Change the name
  • dna-gtset_name('Beta Inhibitor')
  • print My name is dna-gtget_name() \n

41
Usings the DNA Class (3)
  • validate
  • if (dna-gtvalidate())
  • print Sequence validated.\n
  • reverse sequence
  • print My complement is .
  • dna-gtreverse() . \n

42
What's Next?
  • AUTOLOADing
  • Creating accessors and mutators on the fly
  • Class Data
  • Attributes and methods shared by the class as a
    whole instead of belonging to individual objects
  • Constructor default values
  • Destructors
  • Truly encapsulated data

43
What's Beyond That?
  • Blessing things other than hashes
  • Inheritance
  • Polymorphism
  • etc....
  • In other words, everything else other
  • OO languages can do!
Write a Comment
User Comments (0)
About PowerShow.com