Perl OOP Guide — Object-Oriented Programming with Bless and Methods
In this tutorial, you will learn about Perl OOP Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Perl object-oriented programming uses bless on a reference (typically a hash) to create an object, with methods defined as package subroutines that receive the object reference as the implicit first argument ($self) through the arrow operator.
Defining a Class
package Person;
use strict;
use warnings;
# Constructor
sub new {
my ($class, %args) = @_;
my $self = {
name => $args{name} || "Unknown",
age => $args{age} || 0
};
bless $self, $class;
return $self;
}
# Methods
sub name {
my $self = shift;
return $self->{name};
}
sub age {
my $self = shift;
return $self->{age};
}
sub greet {
my $self = shift;
return "Hello, I'm " . $self->{name};
}
1;
Using the Class
use Person;
my $alice = Person->new(name => "Alice", age => 30);
print $alice->greet(); # Hello, I'm Alice
print $alice->name(); # Alice
print $alice->{age}; # direct access (not recommended)
Inheritance
package Employee;
use parent 'Person';
sub new {
my ($class, %args) = @_;
my $self = $class->SUPER::new(%args);
$self->{position} = $args{position} || "Staff";
return $self;
}
sub position { shift->{position} }
sub greet {
my $self = shift;
my $base = $self->SUPER::greet();
return "$base, I work as " . $self->{position};
}
1;
# Usage
use Employee;
my $bob = Employee->new(name => "Bob", position => "Engineer");
print $bob->greet(); # Hello, I'm Bob, I work as Engineer
Accessors with Moose-style
-- Actually, let's use standard Perl
sub get_name { shift->{name} }
sub set_name {
my ($self, $name) = @_;
$self->{name} = $name;
return $self;
}
# Chaining
my $obj = Person->new(name => "Test");
$obj->set_name("Alice")->set_name("Bob"); # chaining
Common Mistakes
1. Forgetting bless
Without bless, methods fail: "Can't call method without a package or object reference".
2. Direct hash access
$self->{name} bypasses Encapsulation. Use accessor methods for proper Api Design.
3. Not using shift for $self
The first argument to a method is the object reference. Always my $self = shift at the start of methods.
Practice Questions
1. What does bless do? bless associates a reference with a class package, enabling method dispatch on that reference.
2. How do you call a parent method?
Use SUPER: $self->SUPER::method(@args) calls the parent class's method.
3. What is the first argument to any Perl method?
The object (or class name for class methods). Capture with my $self = shift.
FAQ
{{< faq question="Can I bless any reference type?" >}} Yes. Hash refs are most common because hash keys serve as named fields. Array refs and scalar refs can also be blessed. {{< /faq >}}
{{< faq question="How do I check an object's class?" >}}
ref($obj) returns the class name. isa($obj, 'Class') or $obj->isa('Class') checks inheritance.
{{< /faq >}}
{{< faq question="What is the difference between -> and :: ?" >}} -> calls a method on an object or class. :: accesses a package function directly (no inheritance). {{< /faq >}}
What's Next
Now learn about Moose, the modern Perl OOP framework.
| Topic | Description | Link |
|---|---|---|
| Moose | Modern OOP with Moose | {{< ref "16-moose" >}} |
| Context | Scalar and list context | {{< ref "17-context" >}} |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro