Re: BUILD and how to create a subclass object when creating a parent
[email protected] (Karen Etheridge) Wed, 25 Apr 2018 10:54:12 -0700
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <CAPJsHfAGoPRiW+aLSs3Hn8u534SWz0Uwzvb+smi0Cze+xXhtdQ@mail.gmail.com> |
--0000000000006b3461056aaff551 Content-Type: text/plain; charset="UTF-8" I'm not sure you mean by create a subclass instance on creation of its superclass. Are you intending to have the superclass object have an attribute that holds an instance of the subclass? Perhaps can you show a diagram of what you intend your data to look like after construction is complete? On Mon, Apr 23, 2018 at 7:53 AM, Arjan Widlak - United Knowledge < [email protected]> wrote: > Hi, > > I tried to use the BUILD method to create an instance of a subclass on > creating an instance of the parent. This does not work and the > documentation clearly explains why: > > http://search.cpan.org/dist/Moose/lib/Moose/Manual/Construct > ion.pod#BUILD_and_parent_classes > > BUILD methods are called from parent to child and hence this triggers a > loop. > > I am able to do this with the around method modifier, however I need to > override the behaviour in the subclass. This is also seems error prone and > bad practice. > > But if this cannot be done this way, how should it be done? How can I > always create a subclass instance on creation of it's superclass? > > Kind regards, > > Arjan > --0000000000006b3461056aaff551 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-size:small">I&#= 39;m not sure you mean by create a subclass instance on creation of its sup= erclass. Are you intending to have the superclass object have an attribute = that holds an instance of the subclass?=C2=A0 Perhaps can you show a diagra= m of what you intend your data to look like after construction is complete?= <br></div></div><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">O= n Mon, Apr 23, 2018 at 7:53 AM, Arjan Widlak - United Knowledge <span dir= =3D"ltr"><<a href=3D"mailto:[email protected]" target=3D"_blank">= [email protected]</a>></span> wrote:<br><blockquote class=3D"gmai= l_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left= :1ex">Hi,<br> <br> I tried to use the BUILD method to create an instance of a subclass on crea= ting an instance of the parent. This does not work and the documentation cl= early explains why:<br> <br> <a href=3D"http://search.cpan.org/dist/Moose/lib/Moose/Manual/Construction.= pod#BUILD_and_parent_classes" rel=3D"noreferrer" target=3D"_blank">http://s= earch.cpan.org/dist/Mo<wbr>ose/lib/Moose/Manual/Construct<wbr>ion.pod#BUILD= _and_parent_<wbr>classes</a><br> <br> BUILD methods are called from parent to child and hence this triggers a loo= p.<br> <br> I am able to do this with the around method modifier, however I need to ove= rride the behaviour in the subclass. This is also seems error prone and bad= practice.<br> <br> But if this cannot be done this way, how should it be done? How can I alway= s create a subclass instance on creation of it's superclass?<br> <br> Kind regards,<br> <br> Arjan<br> </blockquote></div><br></div> --0000000000006b3461056aaff551--