Method modifiers (before/after)
- add some extra checking before or after a method runs
package Odd::Class;
use Moose;
has foo => ( is => 'rw' );
before foo => sub {
confess 'You can only read or write foo when the time is even'
unless time % 2 == 0;
};
after qw/foo expensive_calculation do_stuff/ => sub {
my $self = shift;
Carp::cluck 'CPU limit exceeded' if $self->cpu->limit->is_exceeded;
exit(1); # you die
}
- You can't modify args or return value
- Only side-effects