Perl · Course navigation

Perl packages, bless, and inheritance

Create objects as blessed hashes, override methods in a subclass, and call the parent version.

On this page
Before you start

Perl references and subroutines.

  • Create objects as blessed hashes, override methods in a subclass, and call the parent version.
  • Predict the sample output and explain the state changes.
  • Complete the practice task and check the boundary cases.

The concept

Perl builds objects from ordinary data. A package is a namespace, and a class is a package whose subroutines are methods. bless attaches a package name to a reference, usually a hash, which makes it an object of that class. A method call $obj->method passes the object as the first argument, conventionally named $self.

Inheritance uses the parent pragma. -norequire tells it that the parent package is defined in the same file. A subclass can override a method and call the parent's version with SUPER::. isa tests whether an object belongs to a class or a subclass, can tests whether a method exists, and ref returns the class name. Method calls are resolved at run time, so the right version runs for each object.

Code example

Perl
use strict;
use warnings;

package Animal;

sub new {
    my ($class, %args) = @_;
    my $self = { name => $args{name} };
    return bless $self, $class;
}

sub name { return $_[0]{name} }
sub sound { return 'generic noise' }

sub speak {
    my $self = shift;
    return $self->name . ' says ' . $self->sound;
}

package Dog;
use parent -norequire, 'Animal';

sub sound { return 'Woof' }

sub speak {
    my $self = shift;
    return $self->SUPER::speak() . '!';
}

package main;

my @animals = (Animal->new(name => 'Generic'), Dog->new(name => 'Rex'));
print $_->speak, "\n" for @animals;
print $animals[1]->isa('Animal') ? "a Dog is an Animal\n" : "not an Animal\n";
print Dog->can('speak') ? "Dog can speak\n" : "Dog cannot speak\n";
print ref($animals[1]), "\n";
Output
Generic says generic noise
Rex says Woof!
a Dog is an Animal
Dog can speak
Dog

Follow the code

Open the walkthrough to inspect the example one step at a time.

Show guided walkthrough
Guided walkthroughReady
1use strict;
2use warnings;
3
4package Animal;
5
6sub new {
7 my ($class, %args) = @_;
8 my $self = { name => $args{name} };
9 return bless $self, $class;
10}
11
12sub name { return $_[0]{name} }
13sub sound { return 'generic noise' }
14
15sub speak {
16 my $self = shift;
17 return $self->name . ' says ' . $self->sound;
18}
19
20package Dog;
21use parent -norequire, 'Animal';
22
23sub sound { return 'Woof' }
24
25sub speak {
26 my $self = shift;
27 return $self->SUPER::speak() . '!';
28}
29
30package main;
31
32my @animals = (Animal->new(name => 'Generic'), Dog->new(name => 'Rex'));
33print $_->speak, "\n" for @animals;
34print $animals[1]->isa('Animal') ? "a Dog is an Animal\n" : "not an Animal\n";
35print Dog->can('speak') ? "Dog can speak\n" : "Dog cannot speak\n";
36print ref($animals[1]), "\n";

Follow the example

Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.

View the complete dry-run table
Prepared trace of the published program. Some rows group repeated iterations.
Step / lineWhat happensState after stepOutput so far
bless turns the plain hash into an object of the class that new was called on.—
—
Dog inherits from Animal. -norequire says the parent is in this file.—
—
Call the parent's speak and add an exclamation mark.—
—
The class name is passed to new as $class, so Dog->new creates a Dog.—
—
Each object runs its own class's speak. Animal::speak calls sound, which resolves to Dog::sound for the dog.—
Generic says generic noise
Rex says Woof!
isa follows the inheritance chain.—
Generic says generic noise
Rex says Woof!
a Dog is an Animal
can checks that a method exists, including inherited ones.—
Generic says generic noise
Rex says Woof!
a Dog is an Animal
Dog can speak
ref on a blessed reference returns the class name.—
Generic says generic noise
Rex says Woof!
a Dog is an Animal
Dog can speak
Dog

Behind the scenes

How Perl processes this code

Perl compiles the script internally before executing it. strict and warnings help identify mistakes.

Cost and common mistakes

Method dispatch is O(depth of the inheritance chain) per call, with caching.

  • Object data in a plain hash is public by convention only. Modules such as Moo, Moose, and Object::Pad add stricter classes.
  • Forgetting use strict in each file hides typos in method and variable names.
  • If a subclass overrides new, it must call the parent's new through SUPER::new, or the parent's fields are not set.

Try it yourself

How would you add a Cat class?

Reveal answer and reasoning

Add package Cat; use parent -norequire, 'Animal'; sub sound { return 'Meow' }. The existing speak method works unchanged.

Further reading