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.

In reply to Why does changing a Moose attributes' properties remove the around modifier by mcrose

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.