How to implement class inheritance with Perl Moo extends?
Question
How to implement class inheritance with Perl Moo extends?
In Perl, using Moo for object-oriented programming allows elegant and lightweight class inheritance with the extends keyword. However, when demonstrating inheritance within a single file or sandboxed environment, the base class must be declared first, within the same script. Otherwise, Perl will attempt to load a separate module file (e.g., ParentClass.pm), which doesn't exist in a sandbox or single script context.
To fix the execution error, define both the parent and child classes inside the same script block, ensuring extends can find the parent class without relying on external files. This allows your inheritance example to run successfully with perl - or other limited environments.
Fixed Example: Inline Moo Inheritance
use strict;
use warnings;
use feature 'say';
{
package Animal;
use Moo;
# Attribute 'name' accessible in child classes
has 'name' => (
is => 'ro',
required => 1,
);
sub speak {
my ($self) = @_;
return $self->name . " makes a sound.";
}
}
{
package Dog;
use Moo;
extends 'Animal';
# Override parent's speak method
sub speak {
my ($self) = @_;
return $self->name . " barks.";
}
# Additional method
sub fetch {
my ($self) = @_;
return $self->name . " is fetching!";
}
}
# Script's main program
my $pet = Dog->new(name => 'Fido');
say $pet->speak(); # Fido barks.
say $pet->fetch(); # Fido is fetching!
Explanation of Key Perl Concepts
- Sigils:
$selfis a scalar holding the object reference; attributes use->to access methods or data. - extends: Sets up the
@ISAarray behind the scenes for inheritance, tellingDogto look inAnimalif a method is missing. - Attributes: Defined with
hasinMoodeclare accessor methods automatically (here,$obj->name). - Method overriding: Defining a method with the same name in the child replaces the parent's version.
- Context:
Moouses Perl's context-sensitive features; constructors always return a new object reference. - TMTOWTDI: Perl’s “There’s More Than One Way To Do It” applies as you can mix and match Moo, manual inheritance, or Moose for heavier features.
Common Pitfalls
- Forgetting to declare the base class in the same script or available @INC path leads to
Can't locate ParentClass.pm. - Not using
use Moo;in both parent and child packages results in missing attribute or constructor support. - Overriding methods without calling parent method (if needed) can change behavior unexpectedly; call parent as
$self->Animal::speak()if required.
Version Notes
Mooworks on Perl 5.8.1 and later; this example uses strict, warnings, and feature 'say' available in Perl 5.10+.
By defining parent and child classes in the same script, Moo inheritance works seamlessly in sandboxed environments without needing external module files.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 25ms
Fido barks.
Fido is fetching!
(empty)Was this helpful?
Related Questions
- How to use Moose::Util::TypeConstraints for custom types?
- How to implement method delegation in Perl Moo?
- How to use Type::Tiny with Moo for strict typing in Perl?
- How to use required attributes in Perl Moo and Moose?
- How to convert a Perl Moo class to Moose?
- How to use Moo lazy_build for attribute defaults in Perl?