perl objects 101
DESCRIPTION
I threw this together for $work recently.TRANSCRIPT
![Page 1: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/1.jpg)
Perl Objects 101Simple OOP with Perl
![Page 2: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/2.jpg)
TermsClass - a template describing data and behavior specific to the objectObject - a container for “some data” which is associated with “some behavior”Method - a function associated with a classAttribute - private data associated with each unique object
![Page 3: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/3.jpg)
Perl TermsClass - a package containing methodsObject - a reference with an associated classMethod - a subroutine which takes an object or class as its first argumentAttribute - any sort of Perl data should work
![Page 4: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/4.jpg)
Bless = magicbless $reference, Classname
$reference is any reference (scalar, hash, array, function, file handle)Classname is any package name.
The result is a blessed reference; an object.
![Page 5: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/5.jpg)
MethodsMethods 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.
![Page 6: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/6.jpg)
Accessing the dataSince the object is still a reference, the underlying data are easily accessible.
$obj->{attr1} = 42;
![Page 7: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/7.jpg)
No ProtectionWhat you just saw is considered BAD.
Any code outside of the class can easily tinker with the insides of the object.
![Page 8: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/8.jpg)
Class MethodsSame syntax to call Class methods:
$my_class->method();
Except the package name is used instead of the object reference.
![Page 9: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/9.jpg)
ConstructorsConstructors 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’;
}
![Page 10: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/10.jpg)
Better ConstructorIt’s first parameter will normally be the class name.package MyClass;
sub new {
my ( $class ) = @_;
bless { attr1 => 42 }, $class;
}
![Page 11: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/11.jpg)
Inheritance@ISA arrayIt is a package global.
package MyInheritedClass;
use vars qw( @ISA );
@ISA = qw( MyClass );
sub my_method2 {};
![Page 12: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/12.jpg)
@ISA ContinuedWhen 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.
![Page 13: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/13.jpg)
UNIVERSALAll classes implicitly inherit from class “UNIVERSAL” as their last base class.
![Page 14: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/14.jpg)
Inheriting ShortcutThis is tedious:use vars qw(@ISA);
@ISA = qw(MyClass);
There is a shortcut:use base ‘MyClass’;
![Page 15: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/15.jpg)
Calling Inherited MethodsWe 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.
![Page 16: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/16.jpg)
SUPER Pseudo-ClassThe right thing would be to do:package MyInheritedClass;
sub method1 {
my ( $self ) = @_;
$self->SUPER::method1($self);
}
![Page 17: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/17.jpg)
Inheriting ConstructorsNormally, 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.
![Page 18: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/18.jpg)
DestructorsPerl 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
}
![Page 19: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/19.jpg)
Multiple InheritanceJust put more stuff into @ISA
@ISA = qw(Class1, Class2);
oruse base qw(Class1 Class2);
![Page 20: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/20.jpg)
Data Storage for ObjectsMost 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.
![Page 21: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/21.jpg)
Other ways of OOPMany, many modules have been created to help with this.
Currently, Moo/Mouse/Moose is The One True WayThis is Perl, so you know that isn’t quite true.
![Page 22: Perl objects 101](https://reader033.vdocument.in/reader033/viewer/2022052909/5599a6e61a28ab17698b4848/html5/thumbnails/22.jpg)
Let’s look at...some code.