develooper Front page | perl.moose | Postings from November 2014

nested roles are badly limited

Thread Next
From:
John Macdonald
Date:
November 21, 2014 21:32
Subject:
nested roles are badly limited
Message ID:
01EF6156B387B14E8278ECC376793BE18F54CC58@exmb2.ad.oicr.on.ca
If a role specifies a requires value, that value must be provided in the consuming class.

It cannot be provided in a consuming role.

It cannot be provided in a separate role that is loaded earlier in the same with statement.

It can only be provided in the parent class directly, or in a role consumed by the parent class in an earlier with statement.

That means that you cannot provide a role that bundles up a bunch of roles to make it easy to get the *right* collection loaded in the *right* order by all end consumers.  The separate with statement issue means that you lose the cross-checking done by loading multiple roles in the same with statement.

Without looking into the internals of Roles, I suspect that the attributes of a role are not installed into the consuming class until after the entire role has been processed (which means that a with inside the role is processed too soon, as well as being processing in the namespace of the consuming role rather than the final consuming class).

Is there a good way to work around this?

$ for i in *; do     echo ==== cat $i;     cat $i;     echo ====;     echo ==== perl $i;     perl $i; done
==== cat test1.pl
package BRole;
use Moose::Role;

requires 'b';

package ARole;
use Moose::Role;

has 'b' => ( is => 'ro', isa => 'Scalar' );

package main;
use Moose;
with 'ARole', 'BRole';
====
==== perl test1.pl
'ARole|BRole' requires the method 'b' to be implemented by 'main' at /oicr/local/boutroslab/sw/NightlyBuilds/2014-11-21/perl/Perl-BL-2014-11-21/lib/site_perl/5.18.2/x86_64-linux/Moose/Exporter.pm line 418
    Moose::with('ARole', 'BRole') called at test1.pl line 13
==== cat test2.pl
package BRole;
use Moose::Role;

requires 'b';

package ARole;
use Moose::Role;

has 'b' => ( is => 'ro', isa => 'Scalar' );
with 'BRole';

package main;
use Moose;
with 'ARole';
====
==== perl test2.pl
'ARole' requires the method 'b' to be implemented by 'main' at /oicr/local/boutroslab/sw/NightlyBuilds/2014-11-21/perl/Perl-BL-2014-11-21/lib/site_perl/5.18.2/x86_64-linux/Moose/Exporter.pm line 418
    Moose::with('ARole') called at test2.pl line 14
==== cat test3.pl
package BRole;
use Moose::Role;

requires 'b';

package ARole;
use Moose::Role;

has 'b' => ( is => 'ro', isa => 'Scalar' );

package main;
use Moose;
with 'ARole';
with 'BRole';
====
==== perl test3.pl



John Macdonald
Software Engineer

Ontario Institute for Cancer Research
MaRS Centre

661 University Avenue

Suite 510
Toronto, Ontario

Canada M5G 0A3


Tel:

Email: John.Macdonald@oicr.on.ca

Toll-free: 1-866-678-6427
Twitter: @OICR_news


www.oicr.on.ca<http://www.oicr.on.ca/>

This message and any attachments may contain confidential and/or privileged information for the sole use of the intended recipient. Any review or distribution by anyone other than the person for whom it was originally intended is strictly prohibited. If you have received this message in error, please contact the sender and delete all copies. Opinions, conclusions or other information contained in this message may not be that of the organization.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About