Re: $meta->superclasses()
[email protected] ("Chris Prather") Sat, 21 Nov 2015 20:01:01 -0800 (PST)
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <1448164860963.a7e4ee79@Nodemailer> |
------Nodemailer-0.5.0-?=_1-1448164861273 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable You are setting the superclasses explicitly so you're not inheriting from = Moose::Object which defines the hooks that call BUILDARGS. -Chris On Sat, Nov 21, 2015 at 2:07 PM, Bill Moseley <[email protected]> wrote: > I need a bit of help understanding this code in Catalyst used for adding = in > Plugins: > my $meta =3D Class::MOP::get=5Fmetaclass=5Fby=5Fname($class);= > $meta->superclasses($plugin, $meta->superclasses); > This is preventing BUILDARGS from running, and I'm not sure why. Can > someone provide an explanation=3F > Thanks! > A Catalyst app extends 'Catalyst', and Catalyst extends = Catalyst::Component. > <http://git.shadowcat.co.uk/gitweb/gitweb.cgi=3Fp=3Dcatagits/Catalyst-Run= time.git;a=3Dblob;f=3Dlib/Catalyst.pm;h=3D7e563cb399229facdf7271c7506b294cb= 0b35fb3;hb=3DHEAD#l5> > A Catalyst Component has a config *class* attribute and it also has a > BUILDARGS > <http://git.shadowcat.co.uk/gitweb/gitweb.cgi=3Fp=3Dcatagits/Catalyst-Run= time.git;a=3Dblob;f=3Dlib/Catalyst/Component.pm;h=3D55f25c0092ec598d69484ba= abe728a53db37af47;hb=3DHEAD#l89> > method. BUILDARGS merges in the component's *class* configuration into = the > instance arguments. > In other words, the component's class configuration can populate the > attributes each time the instance is created. > The issue I'm seeing is for (non Moose::Role) plugins the code above is > called and then BUILDARGS is no longer called and thus the config no = longer > sets attributes. > The behavior of the app changes depending if loading non-Moose::Role > plugins or not. > So, I have three questions: > First, is is incorrect for Catalyst to use =22sub BUILDARGS > <http://git.shadowcat.co.uk/gitweb/gitweb.cgi=3Fp=3Dcatagits/Catalyst-Run= time.git;a=3Dblob;f=3Dlib/Catalyst/Component.pm;h=3D55f25c0092ec598d69484ba= abe728a53db37af47;hb=3DHEAD#l89>=22 > instead of using the =22around=22 method modifier=3F > Second, why is Catalyst::Component::BUILDARGS no longer called once > $meta->superclasses($plugin, $meta->superclasses); > is run=3F Is there some other BUILDARGS now overriding=3F > And third, I have included an example below of a Catalyst app that > demonstrates the behavior above. That app includes a =22foo=22 attribute = which > is initialized by the configuration. > Note that the =22foo=22 attribute has =22init=5Fargs =3D> undef=22. > The =22foo=22 attribute is initialized when the =22MyPlugin=22 is = commented out. > Why is =22init=5Fargs =3D> undef=22 not preventing =22foo=22 from being = initialized=3F > Here's a simple script that shows the behavior. > Moose-2.1604 > Catalyst-5.90103 > $ cat test.pl > package Catalyst::Plugin::MyPlugin; > use Moose; > package Foo; # A Catalyst app. > use Moose; > extends 'Catalyst'; > use Catalyst ( > 'MyPlugin', # <---- comment out to see change > ); > has foo =3D> ( > is =3D> 'ro', > init=5Farg =3D> undef, # How is it that this attribute is ever = set=3F > ); > Foo->config( > foo =3D> { some =3D> 'hash' }, > ); > Foo->setup; > before dispatch =3D> sub { > my $self =3D shift; > use Data::Dumper; > *# Show what the value of the attribute is now.* > *printf =22attribute foo is [%s]\n=22, Dumper $self->foo;* > die =22dead\n=22; > }; > package main; > use strict; > use warnings; > use Catalyst::Test 'Foo'; > request '/hello'; > Running with the MyPlugin I get: > attribute foo is [$VAR1 =3D undef; > ] > Running w/o the plugin: > attribute foo is [$VAR1 =3D { > 'some' =3D> 'hash' > }; > ] > What's also interesting is if I add this to the the Foo class then the > =22foo=22 attribute is set even when the MyPlugin is included. > before BUILDARGS =3D> sub {}; > --=20 > Bill Moseley > [email protected] ------Nodemailer-0.5.0-?=_1-1448164861273 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable <div>You are setting the superclasses explicitly so you're not inheriting = from Moose::Object which defines the hooks that call BUILDARGS.</div> <div><br></div> <div>-Chris</div> <div class=3D=22mailbox=5Fsignature=22><br></div> <br><br><div class=3D=22gmail=5Fquote=22><p>On Sat, Nov 21, 2015 at 2:07 PM= , Bill Moseley <span dir=3D=22ltr=22><<a href=3D=22mailto:moseley@hank.= org=22 target=3D=22=5Fblank=22>[email protected]</a>></span> = wrote:<br></p><blockquote class=3D=22gmail=5Fquote=22 style=3D=22margin:0 0= 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;=22><div><div = dir=3D=22ltr=22> <div>I need a bit of help understanding this code in Catalyst used for = adding in Plugins:<br><br></div> <div> <div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 my $meta =3D = Class::MOP::get=5Fmetaclass=5Fby=5Fname($class);</div> <div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 $meta->superclasses($plug= in, $meta->superclasses);</div> </div> <div><br></div> <div>This is preventing BUILDARGS from running, and I'm not sure why.=C2=A0= Can someone provide an explanation=3F<br><br>Thanks!</div> <div><br></div> <div><br></div> <div>A Catalyst app extends 'Catalyst', and Catalyst <a = href=3D=22http://git.shadowcat.co.uk/gitweb/gitweb.cgi=3Fp=3Dcatagits/Catal= yst-Runtime.git;a=3Dblob;f=3Dlib/Catalyst.pm;h=3D7e563cb399229facdf7271c750= 6b294cb0b35fb3;hb=3DHEAD#l5=22>extends Catalyst::Component.</a> </div> <div><br></div> <div>A Catalyst Component has a config <i>class</i>=C2=A0attribute=C2=A0and= it also has a <a href=3D=22http://git.shadowcat.co.uk/gitweb/gitweb.= cgi=3Fp=3Dcatagits/Catalyst-Runtime.git;a=3Dblob;f=3Dlib/Catalyst/Component= .pm;h=3D55f25c0092ec598d69484baabe728a53db37af47;hb=3DHEAD#l89=22>BUILDARGS= </a> method.=C2=A0 BUILDARGS merges in the component's <i>class</i> = configuration into the instance arguments.</div> <div><br></div> <div>In other words, the component's class configuration can populate the = attributes each time the instance is created.</div> <div><br></div> <div>The issue I'm seeing is for (non Moose::Role) plugins the code above = is called and then BUILDARGS is no longer called and thus the config no = longer sets attributes.</div> <div><br></div> <div>The behavior of the app changes depending if loading non-Moose::Role = plugins or not.</div> <div><br></div> <div>So, I have three questions:</div> <div><br></div> <div>First, is is incorrect for Catalyst to use =22<a href=3D=22http://git.= shadowcat.co.uk/gitweb/gitweb.cgi=3Fp=3Dcatagits/Catalyst-Runtime.= git;a=3Dblob;f=3Dlib/Catalyst/Component.pm;h=3D55f25c0092ec598d69484baabe72= 8a53db37af47;hb=3DHEAD#l89=22>sub BUILDARGS</a>=22 instead of using the = =22around=22 method modifier=3F</div> <div><br></div> <div><br></div> <div>Second, why is Catalyst::Component::BUILDARGS no longer called = once</div> <div><br></div> <div>=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 $meta->superclasses($plugi= n, $meta->superclasses);<br></div> <div><br></div> <div>is run=3F =C2=A0 Is there some other BUILDARGS now = overriding=3F</div> <div><br></div> <div><br></div> <div>And third, I have included an example below of a Catalyst app that = demonstrates the behavior above. That app includes a =22foo=22 attribute = which is initialized by the configuration.</div> <div><br></div> <div>Note that the =22foo=22 attribute has =22init=5Fargs =3D> undef=22.= </div> <div><br></div> <div>The =22foo=22 attribute is initialized when the =22MyPlugin=22 is = commented out.</div> <div><br></div> <div>Why is =22init=5Fargs =3D> undef=22 not preventing =22foo=22 from = being initialized=3F</div> <div><br></div> <div><br></div> <div>Here's a simple script that shows the behavior. =C2=A0</div> <div><br></div> <div>Moose-2.1604<br></div> <div>Catalyst-5.90103</div> <div><br></div> <div><font face=3D=22monospace, monospace=22>$ cat <a href=3D=22http://test= .pl=22>test.pl</a></font></div> <div><font face=3D=22monospace, monospace=22>package = Catalyst::Plugin::MyPlugin;<br></font></div> <div> <div><font face=3D=22monospace, monospace=22>use Moose;</font></div> <div><br></div> <div><font face=3D=22monospace, monospace=22>package Foo; =C2=A0# A = Catalyst app.</font></div> <div><font face=3D=22monospace, monospace=22>use Moose;</font></div> <div><font face=3D=22monospace, monospace=22>extends = 'Catalyst';</font></div> <div><font face=3D=22monospace, monospace=22>use Catalyst (</font></div> <div><font face=3D=22monospace, monospace=22>=C2=A0 =C2=A0 'MyPlugin', = =C2=A0 # <---- comment out to see change</font></div> <div><font face=3D=22monospace, monospace=22>);</font></div> <div><font face=3D=22monospace, monospace=22><br></font></div> <div><font face=3D=22monospace, monospace=22>has foo =3D> = (</font></div> <div><font face=3D=22monospace, monospace=22>=C2=A0 =C2=A0 is =C2=A0=3D>= 'ro',</font></div> <div><font face=3D=22monospace, monospace=22>=C2=A0 =C2=A0 init=5Farg = =3D> undef, =C2=A0# How is it that this attribute is ever = set=3F</font></div> <div><font face=3D=22monospace, monospace=22>);</font></div> <div><font face=3D=22monospace, monospace=22><br></font></div> <div><font face=3D=22monospace, monospace=22>Foo->config(</font></div> <div><font face=3D=22monospace, monospace=22>=C2=A0 =C2=A0 foo =3D> { = some =3D> 'hash' },</font></div> <div><font face=3D=22monospace, monospace=22>);</font></div> <div><font face=3D=22monospace, monospace=22><br></font></div> <div><font face=3D=22monospace, monospace=22>Foo->setup;</font></div> <div><font face=3D=22monospace, monospace=22><br></font></div> <div><font face=3D=22monospace, monospace=22>before dispatch =3D> sub = {</font></div> <div><font face=3D=22monospace, monospace=22>=C2=A0 =C2=A0 my $self =3D = shift;</font></div> <div><font face=3D=22monospace, monospace=22><br></font></div> <div><font face=3D=22monospace, monospace=22>=C2=A0 =C2=A0 use = Data::Dumper;</font></div> <div><font face=3D=22monospace, monospace=22><br></font></div> <div><font face=3D=22monospace, monospace=22>=C2=A0 =C2=A0 <b># Show what = the value of the attribute is now.</b></font></div> <div><font face=3D=22monospace, monospace=22>=C2=A0 =C2=A0 <b>printf = =22attribute foo is [%s]\n=22, Dumper $self->foo;</b><br></font></div> <div><font face=3D=22monospace, monospace=22><br></font></div> <div><font face=3D=22monospace, monospace=22>=C2=A0 =C2=A0 die = =22dead\n=22;</font></div> <div><font face=3D=22monospace, monospace=22>};</font></div> <div><font face=3D=22monospace, monospace=22><br></font></div> <div><font face=3D=22monospace, monospace=22><br></font></div> <div><font face=3D=22monospace, monospace=22>package main;</font></div> <div><font face=3D=22monospace, monospace=22>use strict;</font></div> <div><font face=3D=22monospace, monospace=22>use warnings;</font></div> <div><font face=3D=22monospace, monospace=22>use Catalyst::Test = 'Foo';</font></div> <div><br></div> <div><br></div> <div>request '/hello';</div> </div> <div><br></div> <div>Running with the MyPlugin I get:</div> <div><br></div> <blockquote style=3D=22margin:0px 0px 0px 40px;border:none;padding:0px=22> <div><div>attribute foo is [$VAR1 =3D undef;</div></div> <div><div>]</div></div> </blockquote> <div><br></div> <div>Running w/o the plugin:</div> <div><br></div> <blockquote style=3D=22margin:0px 0px 0px 40px;border:none;padding:0px=22> <div><div>attribute foo is [$VAR1 =3D {</div></div> <div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 'some' =3D> = 'hash'</div></div> <div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 };</div></div> <div><div>]</div></div> </blockquote> <div><br></div> <div><br></div> <div>What's also interesting is if I add this to the the Foo class then the= =22foo=22 attribute is set even when the MyPlugin is included.</div> <div><br></div> <div>before BUILDARGS =3D> sub {};<br></div> <div><br></div> <br clear=3D=22all=22><div><br></div>-- <br><div>Bill Moseley<br><a = href=3D=22mailto:[email protected]=22>[email protected]</a> </div> </div></div></blockquote></div><br> ------Nodemailer-0.5.0-?=_1-1448164861273--