Re: Superclass method modifiers don't run when attribute is extended/modified

[email protected] (Stevan Little)
Newsgroups perl.moose
Message-ID <[email protected]>
Bernardo,

On May 15, 2008, at 10:42 AM, [email protected] wrote:
>
> I expected "after att1 write: dummy" to be printed in every case.  
> Why does
> extending (or overriding with only some options) an attribute  
> definition
> prevent the after modifier of the parentclass being called?

The "after" modifier is applied to accessor generated in the original  
class, it is not re-applied for each subclass. When you redefine or  
extend the "attr1" attribute in your subclasses you are defining a  
completely new "attr1" accessor and therefore overriding the original  
one. Remember that "after" is applied to the method specifically, it  
does not care if the method was written by you, or by an attribute  
and so it will do nothing special if its applied to the accessor.

- Stevan


>
>
> Bernardo
>
> =====================================================================
>
> #!/usr/bin/env perl
>
> {
>     package Role1;
>     use Moose::Role;
>
>     has 'att1' => ( is => 'rw', isa => 'Any' );
> }
> {
>     package Foo;
>     use Moose;
>     with 'Role1';
>
>     after 'att1' => sub {
>         my($self, $att1) = @_;
>
>         if (defined $att1) {
>             print "after att1 write: ", $att1, "\n";
>         }
>         else {
>             print "after att1 read\n";
>         }
>     }
> }
>
> print "--- Class Foo::Bar doesn't modify att1 definition ---\n";
> {
>     package Foo::Bar;
>     use Moose;
>     extends 'Foo';
>
>     has 'att2' => ( is => 'rw' );
> }
> $o = Foo::Bar->new(att1 => 'kk');
> $o->att1('dummy');
>
> print "--- Class Foo::Bar overrides att1 definition, specifies only
> isa ---\n";
> {
>     package Foo::Bar;
>     use Moose;
>     extends 'Foo';
>
>     has 'att2' => ( is => 'rw' );
>     has 'att1' => ( isa => 'Str' );
> }
> undef $o;
> $o = Foo::Bar->new(att1 => 'kk');
> $o->att1('dummy');
>
> print "--- Class Foo::Bar overrides att1 definition, specifies is and
> isa ---\n";
> {
>     package Foo::Bar;
>     use Moose;
>     extends 'Foo';
>
>     has 'att2' => ( is => 'rw' );
>     has 'att1' => ( is => 'rw', isa => 'Str' );
> }
> undef $o;
> $o = Foo::Bar->new(att1 => 'kk');
> $o->att1('dummy');
>
> print "--- Class Foo::Bar extends att1 definition with
> \"has '+att1'\" ---\n";
> {
>     package Foo::Bar;
>     use Moose;
>     extends 'Foo';
>
>     has 'att2' => ( is => 'rw' );
>     has '+att1' => ( isa => 'Str' );
> }
> undef $o;
> $o = Foo::Bar->new(att1 => 'kk');
> $o->att1('dummy');
>
> ==================================================================
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.