Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Installing role with Moose::Exporter

by tilly (Archbishop)
on May 03, 2011 at 00:42 UTC ( #902602=perlquestion: print w/ replies, xml ) Need Help??
tilly has asked for the wisdom of the Perl Monks concerning the following question:

I have a class that I'm writing that uses Moose::Exporter. I'd like to be able to force the class that uses it to import an associated role. Is there any reasonable way to do this? The docs suggest that there is, but I'm having trouble making it work.

Just to avoid the possibility of an X-Y problem, the reason why I need Moose::Exporter is to have utility functions that have access to meta. But I'd also like to require that certain other methods are defined. Which is easy to do with a role.

Comment on Installing role with Moose::Exporter
Re: Installing role with Moose::Exporter
by stvn (Monsignor) on May 03, 2011 at 01:46 UTC

    I would try asking this question either on the Moose mailing list or #moose@irc.perl.org. I personally don't know the answer but people who hang out in those two places do. And unfortunately for you, I am really the only one of the Moose code team that hangs out here on Perlmonks.

    -stvn
      Thanks. I finally figured it out. The trick is in when init_meta is called. This code works:
      package TestMooseExporter; use Data::Dumper; $Data::Dumper::Indent = 1; use Moose (); use Moose::Exporter; use Moose::Util::MetaRole; Moose::Exporter->setup_import_methods( with_meta => [qw( hello )], also => "Moose", ); sub hello { # print Dumper($_) for @_; print $_[0]->{package}; } sub init_meta { shift; my %args = @_; Moose->init_meta(%args); Moose::Util::MetaRole::apply_base_class_roles( for => $args{for_class}, roles => ['TestRole'], ); return $args{for_class}->meta; } 1;
      However in a class that uses this, the role is not applied until after the class compiles. So my test code which tried to call a function inside of the module failed to find the role applied. But if I wait and look for methods later, it all works.
Re: Installing role with Moose::Exporter
by SimonClinch (Chaplain) on May 03, 2011 at 11:32 UTC
    How about showing us what you are trying to import and how you are trying to do it.

    One world, one people

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://902602]
Approved by planetscape
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-09-19 10:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (135 votes), past polls