Re: $meta->superclasses()

[email protected] (Bill Moseley) Sun, 22 Nov 2015 10:42:43 -0800
Newsgroups perl.moose
Message-ID <CAKhN_m5DO9TqSpMRZPO_+ky6zjQo4xJPa8Wjy0d91jn3FhVRUw@mail.gmail.com>
--047d7bd6b5ea12d4de0525257b36
Content-Type: text/plain; charset=UTF-8

On Sat, Nov 21, 2015 at 8:01 PM, Chris Prather <[email protected]> wrote:

> You are setting the superclasses explicitly so you're not inheriting from
> Moose::Object which defines the hooks that call BUILDARGS.
>

Maybe I'm not following.  The Catalyst code does this:


>
>>             my $meta = Class::MOP::get_metaclass_by_name($class);
>>             $meta->superclasses($plugin, $meta->superclasses);
>>
>
In this case $plugin = 'Catalyst::Plugin::MyPlugin', and
$meta->superclasses = 'Catalyst'.  Again, Catalyst extends
Catalyst::Component, where BUILDARGS is defined.

I'm not clear how MI is suppose to work, but what I'm seeing is BUILDARGS
is now called in $plugin, and not in Catalyst::Component.

By reversing the order like this:

$meta->superclasses($meta->superclasses, $plugin);


the BUILDARGS is called in Catalyst::Component and not in the $plugin.

This is only an issue when the plugin uses Moose, but that's pretty
common.  Plugins written as roles are fine, of course.


Oh, BTW -- the init_arg problem seems to be related
to MooseX::Emulate::Class::Accessor::Fast.

This code:

package Foo;
use Moose;
use Data::Dumper;
with 'MooseX::Emulate::Class::Accessor::Fast';

has foo => (
    is  => 'ro',
    isa => 'Int', # for error
    init_arg => undef,
);


package main;
use strict;
use warnings;

my $foo = Foo->new( { foo => 'bar' } );

use Data::Dumper;
print Dumper $foo->foo;


Generates this:

$VAR1 = 'bar';


 Which should not have been set -- and clearly isn't an Int.

-- 
Bill Moseley
[email protected]

--047d7bd6b5ea12d4de0525257b36
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On S=
at, Nov 21, 2015 at 8:01 PM, Chris Prather <span dir=3D"ltr">&lt;<a href=3D=
"mailto:[email protected]" target=3D"_blank">[email protected]</a>&gt=
;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0px 0p=
x 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border=
-left-style:solid;padding-left:1ex">
<div>You are setting the superclasses explicitly so you&#39;re not inheriti=
ng from Moose::Object which defines the hooks that call BUILDARGS.</div></b=
lockquote><div><br></div><div>Maybe I&#39;m not following.=C2=A0 The Cataly=
st code does this:</div><div>=C2=A0</div><blockquote class=3D"gmail_quote" =
style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:r=
gb(204,204,204);border-left-style:solid;padding-left:1ex"><div class=3D""><=
div class=3D"h5"><div class=3D"gmail_quote"><blockquote class=3D"gmail_quot=
e" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-colo=
r:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><div dir=
=3D"ltr"><div><br></div>
<div>
<div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 my $meta =3D Class::MOP::get=
_metaclass_by_name($class);</div>
<div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 $meta-&gt;superclasses($plug=
in, $meta-&gt;superclasses);</div></div></div></div></blockquote></div></di=
v></div></blockquote><div><br></div><div>In this case $plugin =3D &#39;Cata=
lyst::Plugin::MyPlugin&#39;, and $meta-&gt;superclasses =3D &#39;Catalyst&#=
39;.=C2=A0 Again, Catalyst extends Catalyst::Component, where BUILDARGS is =
defined.=C2=A0</div><div><br></div><div>I&#39;m not clear how MI is suppose=
 to work, but what I&#39;m seeing is BUILDARGS is now called in $plugin, an=
d not in Catalyst::Component. =C2=A0=C2=A0</div><div><br></div><div>By reve=
rsing the order like this:</div><div><br></div></div></div><blockquote styl=
e=3D"margin:0px 0px 0px 40px;border:none;padding:0px"><div class=3D"gmail_e=
xtra"><div class=3D"gmail_quote"><div>$meta-&gt;superclasses($meta-&gt;supe=
rclasses, $plugin);</div></div></div></blockquote><div class=3D"gmail_extra=
"><div class=3D"gmail_quote"><div><br></div><div>the BUILDARGS is called in=
 Catalyst::Component and not in the $plugin.</div><div><br></div><div>This =
is only an issue when the plugin uses Moose, but that&#39;s pretty common.=
=C2=A0 Plugins written as roles are fine, of course.</div><div><br></div><d=
iv><br></div><div>Oh, BTW -- the init_arg problem seems to be related to=C2=
=A0MooseX::Emulate::Class::Accessor::Fast.</div><div><br></div><div>This co=
de:</div><div><br></div></div></div><blockquote style=3D"margin:0px 0px 0px=
 40px;border:none;padding:0px"><div class=3D"gmail_extra"><div class=3D"gma=
il_quote"><div><div>package Foo;</div></div></div></div><div class=3D"gmail=
_extra"><div class=3D"gmail_quote"><div><div>use Moose;</div></div></div></=
div><div class=3D"gmail_extra"><div class=3D"gmail_quote"><div><div>use Dat=
a::Dumper;</div></div></div></div><div class=3D"gmail_extra"><div class=3D"=
gmail_quote"><div><div>with &#39;MooseX::Emulate::Class::Accessor::Fast&#39=
;;</div></div></div></div><div class=3D"gmail_extra"><div class=3D"gmail_qu=
ote"><div><div><br></div></div></div></div><div class=3D"gmail_extra"><div =
class=3D"gmail_quote"><div><div>has foo =3D&gt; (</div></div></div></div><d=
iv class=3D"gmail_extra"><div class=3D"gmail_quote"><div><div>=C2=A0 =C2=A0=
 is =C2=A0=3D&gt; &#39;ro&#39;,</div></div></div></div><div class=3D"gmail_=
extra"><div class=3D"gmail_quote"><div><div>=C2=A0 =C2=A0 isa =3D&gt; &#39;=
Int&#39;, # for error</div></div></div></div><div class=3D"gmail_extra"><di=
v class=3D"gmail_quote"><div><div>=C2=A0 =C2=A0 init_arg =3D&gt; undef,</di=
v></div></div></div><div class=3D"gmail_extra"><div class=3D"gmail_quote"><=
div><div>);</div></div></div></div><div class=3D"gmail_extra"><div class=3D=
"gmail_quote"><div><div><br></div></div></div></div><div class=3D"gmail_ext=
ra"><div class=3D"gmail_quote"><div><div><br></div></div></div></div><div c=
lass=3D"gmail_extra"><div class=3D"gmail_quote"><div><div>package main;</di=
v></div></div></div><div class=3D"gmail_extra"><div class=3D"gmail_quote"><=
div><div>use strict;</div></div></div></div><div class=3D"gmail_extra"><div=
 class=3D"gmail_quote"><div><div>use warnings;</div></div></div></div><div =
class=3D"gmail_extra"><div class=3D"gmail_quote"><div><div><br></div></div>=
</div></div><div class=3D"gmail_extra"><div class=3D"gmail_quote"><div><div=
>my $foo =3D Foo-&gt;new( { foo =3D&gt; &#39;bar&#39; } );</div></div></div=
></div><div class=3D"gmail_extra"><div class=3D"gmail_quote"><div><div><br>=
</div></div></div></div><div class=3D"gmail_extra"><div class=3D"gmail_quot=
e"><div><div>use Data::Dumper;</div></div></div></div><div class=3D"gmail_e=
xtra"><div class=3D"gmail_quote"><div><div>print Dumper $foo-&gt;foo;</div>=
</div></div></div></blockquote><div class=3D"gmail_extra"><div class=3D"gma=
il_quote"><div><br></div><div>Generates this:</div><div><br></div></div></d=
iv><blockquote style=3D"margin:0 0 0 40px;border:none;padding:0px"><div cla=
ss=3D"gmail_extra"><div class=3D"gmail_quote"><div>$VAR1 =3D &#39;bar&#39;;=
</div></div></div></blockquote><div class=3D"gmail_extra"><div class=3D"gma=
il_quote"><div><br></div><div>=C2=A0Which should not have been set -- and c=
learly isn&#39;t an Int.</div></div><div><br></div>-- <br><div class=3D"gma=
il_signature">Bill Moseley<br><a href=3D"mailto:[email protected]" target=3D=
"_blank">[email protected]</a></div>
</div></div>

--047d7bd6b5ea12d4de0525257b36--