Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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.

Replies are listed 'Best First'.
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 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.

      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 (anonymized user) (Curate) 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?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2021-03-07 18:37 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (122 votes). Check out past polls.