http://www.perlmonks.org?node_id=915199

mcrose has asked for the wisdom of the Perl Monks concerning the following question:

Why does the following code not work for the derived class? Changing the properties of the base class' accessor removes the around modifier defined in the base class. When I simply inherit the base class without modifying it, however, the around modifier fires off properly.

I know I can work around this, but I'm surprised it doesn't work as-is.

use strict; use warnings; use 5.010; package My::Base; use Moose; has 'attr' => (is => 'rw', isa => 'Str', required => 1); around 'attr' => sub { my $orig = shift; my $self = shift; my $response = $self->$orig(@_); return "The value of attr is '$response'" }; package My::Derived; use Moose; extends 'My::Base'; has '+attr' => (required => 0, lazy_build => 1); sub _build_attr { return "default value"; } package My::Inherited; use Moose; extends 'My::Base'; package main; use Test::More tests => 9; use Test::Exception; throws_ok {My::Base->new()} qr/Attribute \(attr\) is required/, q/base + requires 'attr' at construction/; my $base = new_ok('My::Base' => [attr => 'constructor value']); cmp_ok($base->attr, 'eq', "The value of attr is 'constructor value'", +'base is correct'); lives_ok {My::Derived->new()} q/derived doesn't require 'attr' at cons +truction/; my $der = new_ok('My::Derived'); cmp_ok($der->attr, 'eq', "The value of attr is 'default value'", 'deri +ved is correct'); throws_ok {My::Base->new()} qr/Attribute \(attr\) is required/, q/inhe +rited requires 'attr' at construction/; my $inh = new_ok('My::Inherited' => [attr => 'constructor value']); cmp_ok($inh->attr, 'eq', "The value of attr is 'constructor value'", ' +inherited is correct');
The above gives the following output:
1..9 ok 1 - base requires 'attr' at construction ok 2 - The object isa My::Base ok 3 - base is correct ok 4 - derived doesn't require 'attr' at construction ok 5 - The object isa My::Derived not ok 6 - derived is correct # Failed test 'derived is correct' # at foobar.pl line 43. # got: 'default value' # expected: 'The value of attr is 'default value'' ok 7 - inherited requires 'attr' at construction ok 8 - The object isa My::Inherited ok 9 - inherited is correct # Looks like you failed 1 test of 9.