Heya folks,
Sorry in-advance for the long post, but it's the minimal amount of
code that I could demonstrate this issue...
When creating a new MooseX extension, within a rolled-in attribute,
I'm unable to use associated_class method to get the consuming class
(well, it succeeds, but returns undef). I was looking to use this to
ensure at class-creation time that only a single attribute has the
given value set, but I'm unable to determine what the consuming class
is.
A pared down example that demonstrates this is as-follows:
-=-=-
package ASDF;
use Moose;
use Moose::Exporter;
use Moose::Util::MetaRole;
use ASDF::AttributeRole;
Moose::Exporter->setup_import_methods( also => 'Moose' );
sub init_meta {
my ( undef, %options ) = @_;
Moose->init_meta( %options );
Moose::Util::MetaRole::apply_metaclass_roles(
for_class => $options{ for_class },
attribute_metaclass_roles => [
qw( ASDF::AttributeRole ),
],
);
return $options{ for_class }->meta;
};
no Moose;
1;
package ASDF::AttributeRole;
use Moose::Role;
has asdf => (
is => 'ro',
trigger => sub {
my ( $self, $value ) = @_;
printf( "In trigger, assosciated class = %s\n",
$self->associated_class
);
},
);
1;
#!/usr/bin/perl
use lib '.';
package XYZ;
use Moose;
use ASDF;
has qwerty => (
is => 'ro',
asdf => 1,
);
-=-=-
I'd have expected the trigger within ASDF::AttributeRole to inform
that the associated_class is 'XYZ'. Instead, I'm getting:
<<EOOUTPUT
Use of uninitialized value in printf at ASDF/AttributeRole.pm line 10.
In trigger, assosciated class =
EOOUTPUT
Very likely it's something I'm doing wrong here (it's my first attempt
at a MooseX class), hence why post here, and not as a bug report.
Cheers,
Mark.