Perl objects 101

399 views

Published on

I threw this together for $work recently.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
399
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Perl objects 101

  1. 1. Perl Objects 101 Simple OOP with Perl
  2. 2. Terms Class - a template describing data and behavior specific to the object Object - a container for “some data” which is associated with “some behavior” Method - a function associated with a class Attribute - private data associated with each unique object
  3. 3. Perl Terms Class - a package containing methods Object - a reference with an associated class Method - a subroutine which takes an object or class as its first argument Attribute - any sort of Perl data should work
  4. 4. Bless = magic bless $reference, Classname is any reference (scalar, hash, array, function, file handle) Classname is any package name. $reference The result is a blessed reference; an object.
  5. 5. Methods Methods are subs defined in the class’s package. Call them using the dereferencing arrow: $obj->method( $param1, $param2 ); The first parameter will be an object reference.
  6. 6. Accessing the data Since the object is still a reference, the underlying data are easily accessible. $obj->{attr1} = 42;
  7. 7. No Protection What you just saw is considered BAD. Any code outside of the class can easily tinker with the insides of the object.
  8. 8. Class Methods Same syntax to call Class methods: $my_class->method(); Except the package name is used instead of the object reference.
  9. 9. Constructors Constructors are class methods that return an object. The name is arbitrary, although new() is most commonly used. package MyClass; sub new { bless { attr1 => 42 }, ‘MyClass’; }
  10. 10. Better Constructor It’s first parameter will normally be the class name. package MyClass; sub new { my ( $class ) = @_; bless { attr1 => 42 }, $class; }
  11. 11. Inheritance @ISA array It is a package global. package MyInheritedClass; use vars qw( @ISA ); @ISA = qw( MyClass ); sub my_method2 {};
  12. 12. @ISA Continued When an object or class method is called, Perl gets the package and tries to find a sub in that package with the same name as the method. If found, it calls that. If not, it looks up the @ISA array for other packages, and tries to find the method in those.
  13. 13. UNIVERSAL All classes implicitly inherit from class “UNIVERSAL” as their last base class.
  14. 14. Inheriting Shortcut This is tedious: use vars qw(@ISA); @ISA = qw(MyClass); There is a shortcut: use base ‘MyClass’;
  15. 15. Calling Inherited Methods We can do: package MyInheritedClass; sub method1 { my ( $self ) = @_; MyClass::method1($self); } That’s not OOish and won’t work if MyClass doesn’t have a sub method1.
  16. 16. SUPER Pseudo-Class The right thing would be to do: package MyInheritedClass; sub method1 { my ( $self ) = @_; $self->SUPER::method1($self); }
  17. 17. Inheriting Constructors Normally, a base class constructor will bless the reference into the correct class, so we just need to do some initialization: package MyInheritedClass; sub new { my ( $class, %params ) = @_; my $self = $class->SUPER::new(%params); # do something with the params here... return $self; } Mostly, such constructors are not needed.
  18. 18. Destructors Perl has automatic garbage collection, so mostly, destructors aren’t needed. If they are, create a sub called DESTROY: sub DESTROY { my ($self) = @_; # free some resources }
  19. 19. Multiple Inheritance Just put more stuff into @ISA @ISA = qw(Class1, Class2); or use base qw(Class1 Class2);
  20. 20. Data Storage for Objects Most objects use hashrefs. Convenient to use. No protection. You can’t prevent code from tinkering. Well, there is a thing called “Inside Out Objects”, but we won’t cover that today.
  21. 21. Other ways of OOP Many, many modules have been created to help with this. Currently, Moo/Mouse/Moose is The One True Way This is Perl, so you know that isn’t quite true.
  22. 22. Let’s look at ...some code.

×