Last month I wrote about using Moose’s overridefunctionto, well, override a superclass’s method. Chris Pratheron the #moose IRC channelsuggested soon after that the aroundmethod modifier(or its little sisters beforeand after) might be a better choice if you’re also calling the original method inside. He noted that “at a minimum overrideonly works if you’re subclassing, aroundwill apply to composed methods too.”
His point was that when you decide to compose roles(also know as traits) instead of or in addition to more traditional inheritance, overridesimply doesn’t work: only a method modifier will do. (And as Graham Knopand Karen Etheridgelater remarked on IRC, overrideisn’t even an option if you’re using Mooas an alternative to Moose.)
Modifying a role’s method with aroundmight look like this:
#!/usr/bin/env perl
use v5.12; # for strict and say
use warnings;
package Local::Role::Hungry;
use Moose::Role;
requires 'name';
sub wants_food {
my $self = shift;
say $self->name, ' is hungry!';
return;
}
package Local::GuineaPig;
use Moose;
has name => (is => 'ro');
with 'Local::Role::Hungry';
around wants_food => sub {
my ($orig, $self, @args) = @_;
say $self->name, ' runs to the front of the cage!';
$self->$orig(@args);
say 'Wheek!';
return;
};
package Local::Dog;
use Moose;
has name => (is => 'ro');
with 'Local::Role::Hungry';
around wants_food => sub {
my ($orig, $self, @args) = @_;
say $self->name, ' runs to the kitchen!';
$self->$orig(@args);
say 'Woof!';
return;
};
before wants_food => sub {
my $self = shift;
say $self->name, ' is jumping!';
};
package main;
my $dog = Local::Dog->new(name => 'Seamus');
my @pigs = map { Local::GuineaPig->new(name => $_) }
qw<Cocoa Ginger Pepper>;
for my $animal ($dog, @pigs) {
$animal->wants_food();
}
Running the above yields:
Seamus runs to the kitchen!
Seamus is hungry!
Woof!
Cocoa runs to the front of the cage!
Cocoa is hungry!
Wheek!
Ginger runs to the front of the cage!
Ginger is hungry!
Wheek!
Pepper runs to the front of the cage!
Pepper is hungry!
Wheek!
It’s a little more involved than overriding a sub, since method modifiers are passed both the consumed role’s original method ($origabove) and the instance ($selfabove) as parameters. It has the same effect, though, and you can call the original method by saying $self->$orig(parameters).
If all you want to do is have something happen either before or after the original method, just use beforeor after:
before wants_food => sub {
my $self = shift;
say $self->name, ' is jumping!';
};
Note that there’s no return value in a beforeor aftermodifier, as those are handled by the original method.
Modifiers aren’t limited to consuming classes; they can be in roles and modify their consumers’ methods. They also have a couple of other tricks:
- You can pass an array referenceto modify multiple methods at once.
- You can use the contents of a variable to specify the modified method name, and use that same variable in the modifier itself.
- You can use a regular expressionto select methods. (Beware if you’re using Moo that its Class::Method::Modifiersmodule doesn’t support this.)
Putting these together gives you constructs like these:
after qw<foo bar baz> => sub {
say 'Something got called';
};
for my $method_name (qw<foo bar baz>) {
before $method_name => sub {
say "Calling $method_name...";
};
}
before qr/^request_/ => sub {
my ($self, @args) = @_;
$self->is_valid(@args) or die 'Invalid arguments';
};
Moose comes with great introductory manuals for method modifiers and roles, so be sure to check those out. There’s a lot more to them and a blog can only cover so much.


Comments
3 responses to “Taming the Moose: Method modifiers instead of overrides in object-oriented Perl”
[…] Taming the Moose: Method modifiers instead of overrides in object-oriented Perl […]
Nice article. As a minor aside, I find
my ($orig, $self) = splice @_, 0, 2;is rather confusing to many new developers. Compounding that with thearoundmodifier makes it even harder. How aboutmy ( $orig, $self, @args ) = @_;and later, you can call$self->$orig(@args). I think it’s much easier to read.Thanks! I’ve updated the code with your suggestion.