SlideShare a Scribd company logo
Perl Objects 101
Simple OOP with Perl
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
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
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.
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.
Accessing the data
Since the object is still a reference, the
underlying data are easily accessible.
$obj->{attr1} = 42;
No Protection
What you just saw is considered BAD.
Any code outside of the class can easily tinker
with the insides of the object.
Class Methods
Same syntax to call Class methods:
$my_class->method();

Except the package name is used instead of the
object reference.
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’;
}
Better Constructor
It’s first parameter will normally be the class
name.
package MyClass;
sub new {
my ( $class ) = @_;
bless { attr1 => 42 }, $class;
}
Inheritance
@ISA array
It is a package global.
package MyInheritedClass;
use vars qw( @ISA );
@ISA = qw( MyClass );
sub my_method2 {};
@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.
UNIVERSAL
All classes implicitly inherit from class
“UNIVERSAL” as their last base class.
Inheriting Shortcut
This is tedious:
use vars qw(@ISA);
@ISA = qw(MyClass);

There is a shortcut:
use base ‘MyClass’;
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.
SUPER Pseudo-Class
The right thing would be to do:
package MyInheritedClass;
sub method1 {
my ( $self ) = @_;
$self->SUPER::method1($self);
}
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.
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
}
Multiple Inheritance
Just put more stuff into @ISA
@ISA = qw(Class1, Class2);

or
use base qw(Class1 Class2);
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.
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.
Let’s look at
...some code.

More Related Content

PPT
1 1 5 Clases
 
PPT
Java collections concept
PPT
Collection Framework in java
PDF
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
PPTX
collection framework in java
PPTX
Dictionaries and Sets in Python
PPT
Python Dictionaries and Sets
PDF
Java collections
1 1 5 Clases
 
Java collections concept
Collection Framework in java
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
collection framework in java
Dictionaries and Sets in Python
Python Dictionaries and Sets
Java collections

What's hot (20)

PDF
Java Collection Framework
PPTX
9collection in c#
PDF
Array list (java platform se 8 )
PPTX
Collections framework in java
PDF
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
PPTX
Java Chapter 05 - Conditions & Loops: part 7
DOCX
C# Collection classes
DOCX
Java collections notes
PPTX
Python dictionary
PDF
Lecture 4 - Object Interaction and Collections
PDF
Java ArrayList Tutorial | Edureka
PDF
Collection framework (completenotes) zeeshan
PDF
5 collection framework
PDF
Python Programming - V. Sequences (List and Tuples) and Dictionaries
PPTX
Object oriented programming in php
PPTX
List interface in collections framework
PPT
L11 array list
Java Collection Framework
9collection in c#
Array list (java platform se 8 )
Collections framework in java
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Chapter 05 - Conditions & Loops: part 7
C# Collection classes
Java collections notes
Python dictionary
Lecture 4 - Object Interaction and Collections
Java ArrayList Tutorial | Edureka
Collection framework (completenotes) zeeshan
5 collection framework
Python Programming - V. Sequences (List and Tuples) and Dictionaries
Object oriented programming in php
List interface in collections framework
L11 array list
Ad

Viewers also liked (8)

DOCX
Packaging 4 word new font size
DOCX
PDF
Perl testing 101
PPT
The Dan Cooper Team Experience
PPT
Periodic Table Presentation
PDF
Fyna portafolio ligth version pre
PPT
Dna structure
PPT
Advent Season
Packaging 4 word new font size
Perl testing 101
The Dan Cooper Team Experience
Periodic Table Presentation
Fyna portafolio ligth version pre
Dna structure
Advent Season
Ad

Similar to Perl objects 101 (20)

PPTX
Ruby object model
PDF
OOPs Concept
PDF
Object Oriented Programming in PHP
PPTX
encapsulation, inheritance, overriding, overloading
PPTX
Introduction to Ruby’s Reflection API
PPTX
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
PDF
PDF
Java defining classes
PPTX
Ruby object model - Understanding of object play role for ruby
PPTX
FFW Gabrovo PMG - PHP OOP Part 3
PPT
Advanced php
PPT
Stoop 400 o-metaclassonly
PDF
Reflection and Introspection
PDF
09 Object Oriented Programming in PHP #burningkeyboards
PPTX
Object oriented programming with python
DOCX
Some Viva Questions Of C++ asked during the interview
PPTX
OOP in PHP.pptx
PPTX
Python oop third class
PPT
PHP-05-Objects.ppt
ODP
Object-Oriented Programming with Perl and Moose
Ruby object model
OOPs Concept
Object Oriented Programming in PHP
encapsulation, inheritance, overriding, overloading
Introduction to Ruby’s Reflection API
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
Java defining classes
Ruby object model - Understanding of object play role for ruby
FFW Gabrovo PMG - PHP OOP Part 3
Advanced php
Stoop 400 o-metaclassonly
Reflection and Introspection
09 Object Oriented Programming in PHP #burningkeyboards
Object oriented programming with python
Some Viva Questions Of C++ asked during the interview
OOP in PHP.pptx
Python oop third class
PHP-05-Objects.ppt
Object-Oriented Programming with Perl and Moose

Recently uploaded (20)

PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Approach and Philosophy of On baking technology
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Empathic Computing: Creating Shared Understanding
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Encapsulation theory and applications.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Encapsulation_ Review paper, used for researhc scholars
Programs and apps: productivity, graphics, security and other tools
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Understanding_Digital_Forensics_Presentation.pptx
Approach and Philosophy of On baking technology
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Empathic Computing: Creating Shared Understanding
The AUB Centre for AI in Media Proposal.docx
Spectral efficient network and resource selection model in 5G networks
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Big Data Technologies - Introduction.pptx
Encapsulation theory and applications.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
20250228 LYD VKU AI Blended-Learning.pptx
Unlocking AI with Model Context Protocol (MCP)
Per capita expenditure prediction using model stacking based on satellite ima...
Encapsulation_ Review paper, used for researhc scholars

Perl objects 101

  • 1. Perl Objects 101 Simple OOP with Perl
  • 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. 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. 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. 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. Accessing the data Since the object is still a reference, the underlying data are easily accessible. $obj->{attr1} = 42;
  • 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. Class Methods Same syntax to call Class methods: $my_class->method(); Except the package name is used instead of the object reference.
  • 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. Better Constructor It’s first parameter will normally be the class name. package MyClass; sub new { my ( $class ) = @_; bless { attr1 => 42 }, $class; }
  • 11. Inheritance @ISA array It is a package global. package MyInheritedClass; use vars qw( @ISA ); @ISA = qw( MyClass ); sub my_method2 {};
  • 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. UNIVERSAL All classes implicitly inherit from class “UNIVERSAL” as their last base class.
  • 14. Inheriting Shortcut This is tedious: use vars qw(@ISA); @ISA = qw(MyClass); There is a shortcut: use base ‘MyClass’;
  • 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. SUPER Pseudo-Class The right thing would be to do: package MyInheritedClass; sub method1 { my ( $self ) = @_; $self->SUPER::method1($self); }
  • 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. 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. Multiple Inheritance Just put more stuff into @ISA @ISA = qw(Class1, Class2); or use base qw(Class1 Class2);
  • 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. 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.