nested roles are badly limited

[email protected] (John Macdonald) Fri, 21 Nov 2014 21:32:26 +0000
Newsgroups perl.moose
Message-ID <[email protected]>
--_000_01EF6156B387B14E8278ECC376793BE18F54CC58exmb2adoicronca_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

If a role specifies a requires value, that value must be provided in the co=
nsuming 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 b=
y all end consumers.  The separate with statement issue means that you lose=
 the cross-checking done by loading multiple roles in the same with stateme=
nt.

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 proces=
sed 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 =3D=3D=3D=3D cat $i;     cat $i;     echo =3D=3D=
=3D=3D;     echo =3D=3D=3D=3D perl $i;     perl $i; done
=3D=3D=3D=3D cat test1.pl
package BRole;
use Moose::Role;

requires 'b';

package ARole;
use Moose::Role;

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

package main;
use Moose;
with 'ARole', 'BRole';
=3D=3D=3D=3D
=3D=3D=3D=3D 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/si=
te_perl/5.18.2/x86_64-linux/Moose/Exporter.pm line 418
    Moose::with('ARole', 'BRole') called at test1.pl line 13
=3D=3D=3D=3D cat test2.pl
package BRole;
use Moose::Role;

requires 'b';

package ARole;
use Moose::Role;

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

package main;
use Moose;
with 'ARole';
=3D=3D=3D=3D
=3D=3D=3D=3D 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_per=
l/5.18.2/x86_64-linux/Moose/Exporter.pm line 418
    Moose::with('ARole') called at test2.pl line 14
=3D=3D=3D=3D cat test3.pl
package BRole;
use Moose::Role;

requires 'b';

package ARole;
use Moose::Role;

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

package main;
use Moose;
with 'ARole';
with 'BRole';
=3D=3D=3D=3D
=3D=3D=3D=3D 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: [email protected]

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 dist=
ribution by anyone other than the person for whom it was originally intende=
d is strictly prohibited. If you have received this message in error, pleas=
e contact the sender and delete all copies. Opinions, conclusions or other =
information contained in this message may not be that of the organization.

--_000_01EF6156B387B14E8278ECC376793BE18F54CC58exmb2adoicronca_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<html dir=3D"ltr">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style id=3D"owaParaStyle" type=3D"text/css">P {margin-top:0;margin-bottom:=
0;}</style>
</head>
<body ocsi=3D"0" fpstyle=3D"1">
<div style=3D"direction: ltr;font-family: Tahoma;color: #000000;font-size: =
10pt;">If a role specifies a requires value, that value must be provided in=
 the consuming class.<br>
<br>
It cannot be provided in a consuming role.<br>
<br>
It cannot be provided in a separate role that is loaded earlier in the same=
 with statement.<br>
<br>
It can only be provided in the parent class directly, or in a role consumed=
 by the parent class in an earlier with statement.<br>
<br>
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 b=
y all end consumers.&nbsp; The separate with statement issue means that you=
 lose the cross-checking done by loading
 multiple roles in the same with statement.<br>
<br>
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 proces=
sed too soon, as well as being processing
 in the namespace of the consuming role rather than the final consuming cla=
ss).<br>
<br>
Is there a good way to work around this?<br>
<br>
$ for i in *; do&nbsp;&nbsp;&nbsp;&nbsp; echo =3D=3D=3D=3D cat $i;&nbsp;&nb=
sp;&nbsp;&nbsp; cat $i;&nbsp;&nbsp;&nbsp;&nbsp; echo =3D=3D=3D=3D;&nbsp;&nb=
sp;&nbsp;&nbsp; echo =3D=3D=3D=3D perl $i;&nbsp;&nbsp;&nbsp;&nbsp; perl $i;=
 done<br>
=3D=3D=3D=3D cat test1.pl<br>
package BRole;<br>
use Moose::Role;<br>
<br>
requires 'b';<br>
<br>
package ARole;<br>
use Moose::Role;<br>
<br>
has 'b' =3D&gt; ( is =3D&gt; 'ro', isa =3D&gt; 'Scalar' );<br>
<br>
package main;<br>
use Moose;<br>
with 'ARole', 'BRole';<br>
=3D=3D=3D=3D<br>
=3D=3D=3D=3D perl test1.pl<br>
'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/si=
te_perl/5.18.2/x86_64-linux/Moose/Exporter.pm line 418<br>
&nbsp;&nbsp;&nbsp; Moose::with('ARole', 'BRole') called at test1.pl line 13=
<br>
=3D=3D=3D=3D cat test2.pl<br>
package BRole;<br>
use Moose::Role;<br>
<br>
requires 'b';<br>
<br>
package ARole;<br>
use Moose::Role;<br>
<br>
has 'b' =3D&gt; ( is =3D&gt; 'ro', isa =3D&gt; 'Scalar' );<br>
with 'BRole';<br>
<br>
package main;<br>
use Moose;<br>
with 'ARole';<br>
=3D=3D=3D=3D<br>
=3D=3D=3D=3D perl test2.pl<br>
'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_per=
l/5.18.2/x86_64-linux/Moose/Exporter.pm line 418<br>
&nbsp;&nbsp;&nbsp; Moose::with('ARole') called at test2.pl line 14<br>
=3D=3D=3D=3D cat test3.pl<br>
package BRole;<br>
use Moose::Role;<br>
<br>
requires 'b';<br>
<br>
package ARole;<br>
use Moose::Role;<br>
<br>
has 'b' =3D&gt; ( is =3D&gt; 'ro', isa =3D&gt; 'Scalar' );<br>
<br>
package main;<br>
use Moose;<br>
with 'ARole';<br>
with 'BRole';<br>
=3D=3D=3D=3D<br>
=3D=3D=3D=3D perl test3.pl<br>
<br>
<div><br>
<div style=3D"font-family:Tahoma; font-size:13px">
<p class=3D"western" style=3D"margin-bottom:0cm"><font color=3D"#39793b"><f=
ont style=3D"font-size:9pt" size=3D"2"><span lang=3D"en-US"><b>John Macdona=
ld</b></span></font></font><font color=3D"#39793b"><font style=3D"font-size=
:9pt" size=3D"2"><b><br>
</b></font></font><font style=3D"font-size:8pt" size=3D"1"><span lang=3D"en=
-US">Software Engineer<br>
<br>
</span></font><font color=3D"#39793b"><font style=3D"font-size:9pt" size=3D=
"2"><b>Ontario Institute for Cancer Research</b></font></font><font color=
=3D"#39793b"><font style=3D"font-size:8pt" size=3D"1"><br>
</font></font><font style=3D"font-size:8pt" size=3D"1">MaRS Centre</font></=
p>
<p class=3D"western" style=3D"margin-bottom:0cm"><font style=3D"font-size:8=
pt" size=3D"1">661 University Avenue</font></p>
<p class=3D"western" style=3D"margin-bottom:0cm"><font style=3D"font-size:8=
pt" size=3D"1">Suite 510
<br>
Toronto, Ontario </font></p>
<p class=3D"western" style=3D"margin-bottom:0cm"><font style=3D"font-size:8=
pt" size=3D"1">Canada&nbsp;M5G 0A3</font><font style=3D"font-size:8pt" size=
=3D"1"><span lang=3D"en-US"><br>
</span></font><br>
</p>
<p class=3D"western" style=3D"margin-bottom:0cm"><font style=3D"font-size:8=
pt" size=3D"1"><span lang=3D"en-US">Tel:
</span></font></p>
<p class=3D"western" style=3D"margin-bottom:0cm"><font style=3D"font-size:8=
pt" size=3D"1"><span lang=3D"en-US">Email: [email protected]</span>=
</font></p>
<p class=3D"western" style=3D"margin-bottom:0cm"><font style=3D"font-size:8=
pt" size=3D"1">Toll-free: 1-866-678-6427</font><font style=3D"font-size:8pt=
" size=3D"1"><span lang=3D"en-US"><br>
</span></font><font style=3D"font-size:8pt" size=3D"1">Twitter: @OICR_news<=
/font></p>
<p class=3D"western" style=3D"margin-bottom:0cm"><br>
</p>
<p class=3D"western" style=3D"margin-bottom:0cm"><font color=3D"#0000ff"><u=
><a class=3D"western" href=3D"http://www.oicr.on.ca/"><font style=3D"font-s=
ize:8pt" size=3D"1">www.oicr.on.ca</font></a></u></font><font style=3D"font=
-size:8pt" size=3D"1"><span lang=3D"en-US"><br>
<br>
</span></font><font color=3D"#000000"><font face=3D"Arial, sans-serif"><fon=
t style=3D"font-size:8pt" size=3D"1">This message and any attachments may c=
ontain confidential and/or privileged information for the sole use of the i=
ntended recipient. Any review or distribution
 by anyone other than the person for whom it was originally intended is str=
ictly prohibited. If you have received this message in error, please contac=
t the sender and delete all copies. Opinions, conclusions or other informat=
ion contained in this message may
 not be that of the organization.</font></font></font></p>
</div>
</div>
</div>
</body>
</html>

--_000_01EF6156B387B14E8278ECC376793BE18F54CC58exmb2adoicronca_--