See the code below. It outputs:
--- Class Foo::Bar doesn't modify att1 definition ---
after att1 write: dummy
--- Class Foo::Bar overrides att1 definition, specifies only isa ---
after att1 write: dummy
--- Class Foo::Bar overrides att1 definition, specifies is and isa ---
--- Class Foo::Bar extends att1 definition with "has '+att1'" ---
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?
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.