Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Role::Tiny Moo::Role question on getting dymanic requires

by tmaly (Monk)
on Apr 05, 2016 at 17:01 UTC ( [id://1159626]=perlquestion: print w/replies, xml ) Need Help??

tmaly has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I am using Moo and Moo::Role, and I cannot figure out the best way to dynamically get a list of methods that are required in order to compose a role.

I am trying to dynamically compose two roles, but I need a way to know what the requires are if any exists for one of the roles I am trying to compose.

Best regards
Ty

UPDATE

This needs to be a Moo / Role::Tiny solution as that is all that I have access to

  • Comment on Role::Tiny Moo::Role question on getting dymanic requires

Replies are listed 'Best First'.
Re: Role::Tiny Moo::Role question on getting dymanic requires
by Haarg (Priest) on Apr 08, 2016 at 15:55 UTC

    The only supported way to get that information is to use Moose's meta model, which will also work against Moo classes. Poking into Moo's internals is unsupported and could break at any time.

    A better question is why you are looking for this information in the first place. It seems like a case where you are trying to answer the wrong question.

Re: Role::Tiny Moo::Role question on getting dymanic requires
by 1nickt (Canon) on Apr 05, 2016 at 18:50 UTC

    Not sure I understand what you mean by "what the requires are" ... or by "a list of methods that are required in order to compose a role".

    Do you mean the attributes of the class that are required to be populated when you instantiate it, ie required arguments?

    The way forward always starts with a minimal test.

      Under Role::Tiny there is a
      requires qw(foo bar);
      Described as "Declares a list of methods that must be defined to compose role."
      I am looking for a way to introspect a role from the outside to determine if any requires has been declared and what specific methods were declared in the requires. In this case foo and bar.

        There's no official way to do that, but if you can live with an undocumented approach, you can inspect $Role::Tiny::INFO{YourRole}{requires} e.g.
        package BasicRole; use Moo::Role; requires qw(abra cadabra); 1; package App; use Moo; with 'BasicRole'; sub abra { 'abra' } sub cadabra { 'cadabra' } 1; package main; use strict; use Data::Dump 'dd'; my $app = App->new(); dd $Role::Tiny::INFO{BasicRole}{requires}; __END__ Output: ["abra", "cadabra"]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1159626]
Approved by mtmcc
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 07:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found