Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I want a role to consume another role, and provide an attribute that the consumed role requires, but Moose won't see the attribute provided by the first role.

An example:

package Role::Logger; use Moose::Role; requires qw<smeg>; package Role::App; use Moose::Role; has smeg => ( is => 'rw' ); with 'Role::Logger'; package App::FixIt; use Moose; with "Role::App"; sub run { print "smeg=" . shift->smeg; } package main; use Moose; App::FixIt->new( { smeg => 1 } )->run;

This code returns the error 'Role::App' requires the method 'smeg' to be implemented by 'App::FixIt'. But Role::App doesn't have any requirements, it's Role::Logger which requires smeg, and smeg is provided by Role::App.

The Moose documentation to required attributes warns that a requirement met by an attribute in a consuming class must be met before the role is consumed, so:

package Role::App; use Moose::Role; requires 'gazpacho'; package App::FixIt; use Moose; with Role::App; has gazpacho => ( is => 'rw' );
breaks because the requirement is met after the role is consumed. Here, swapping the order of with and has solves the problem. OTOH, requirement met by a subroutine would also be ok, because the subroutine is defined before with imposes the required constraints at runtime.

In summary: If a role requires a method and the requirement is met by:
a sub in a consuming classNo problem
a sub in a consuming roleNo problem
an attribute in a consuming classbe careful of the has/with order
an attribute in a consuming rolecan't be done?

Thanks for your thoughts!

In reply to Moose role with requirement consuming another role by Boldra

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (5)
    As of 2019-09-21 06:47 GMT
    Find Nodes?
      Voting Booth?
      The room is dark, and your next move is ...

      Results (270 votes). Check out past polls.