Re: Moose n00b question about writer / reader
[email protected] (Andrew Hicox) Mon, 20 Apr 2015 08:22:46 -0500
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <CAKwjTgbCKNsp_OUh8tMv72mPi=o62=Zf5fNPD4esdFJSKvnfxw@mail.gmail.com> |
--089e0141ab7468ed4b051427d355 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Thanks guys! The thing I didn't understand was that reader and writer just change the name of the moose default accessor methods. Looks like trigger will do what I want, though I have to ask ... is there a way to simply override the default moose accessor methods with your own? -Andy On Apr 19, 2015 9:53 PM, "Kent Fredric" <[email protected]> wrote: > > On 20 April 2015 at 07:00, Andrew Hicox <[email protected]> wrote: > >> This seems like I=E2=80=99d just need to declare a writer sub for the at= tribute, >> do my logic in there, then set the attribute value or throw an error, ri= ght? >> >> Why doesn't this work? >> >> package Bogus { >> use Moose; >> has =E2=80=98value=E2=80=99, is =3D> =E2=80=9Crw=E2=80=9D, writer =3D> = =E2=80=9C_value=E2=80=9D, isa =3D> =E2=80=9CNum=E2=80=9D; >> sub _value { >> my ($self, $value) =3D @_; >> ## insert some logic here >> $self->value($value); >> return(1); >> } >> } >> >> use Bogus; >> my $a =3D new Bogus(value =3D> 1); >> >> > The initial mistake I believe you made was thinking that the argument to > writer was intended to be a "I'll create this sub, Moose can use it". > > Whereas that argument in this case is "Moose, create me a writer called > _value". > > So the RHS of that token is intended for more external interfaces, eg: > > --- > > has value =3D> ( is =3D> 'rw', writer =3D> "set_value" , reader =3D> "get= _value"); > ... > $object->set_value( $arg ); > $object->get_value(); # returns $arg > > --- > > > so: > > $writer_sub -> internal attribute store > $reader_sub <- internal attribute store > > $writer_sub and $readersub can sometimes be the same sub, which is the > default case with "is =3D> rw". > > For comparion, see: "is =3D> raw", where $object->value won't exist. > > > -- > Kent > > *KENTNL* - https://metacpan.org/author/KENTNL > > --089e0141ab7468ed4b051427d355 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <p dir=3D"ltr">Thanks guys! The thing I didn't understand was that read= er and writer just change the name of the moose default accessor methods. <= /p> <p dir=3D"ltr">Looks like trigger will do what I want, though I have to ask= ... is there a way to simply override the default moose accessor methods w= ith your own?</p> <p dir=3D"ltr">-Andy </p> <div class=3D"gmail_quote">On Apr 19, 2015 9:53 PM, "Kent Fredric"= ; <<a href=3D"mailto:[email protected]">[email protected]</a>>= ; wrote:<br type=3D"attribution"><blockquote class=3D"gmail_quote" style=3D= "margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D= "ltr"><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">On 20 April= 2015 at 07:00, Andrew Hicox <span dir=3D"ltr"><<a href=3D"mailto:andrew= @hicox.com" target=3D"_blank">[email protected]</a>></span> wrote:<br><bl= ockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #= ccc solid;padding-left:1ex"><div>This seems like I=E2=80=99d just need to d= eclare a writer sub for the attribute, do my logic in there, then set the a= ttribute value or throw an error, right?</div><div><br></div><div>Why doesn= 't this work?</div><div><br></div><blockquote style=3D"margin:0 0 0 40p= x;border:none;padding:0px"><div><font style=3D"font-size:11px" face=3D"Cour= ier New">package Bogus {</font></div><div><font style=3D"font-size:11px" fa= ce=3D"Courier New"><span style=3D"white-space:pre-wrap"> </span>use Moose;<= /font></div><div><font style=3D"font-size:11px" face=3D"Courier New"><span = style=3D"white-space:pre-wrap"> </span>has=C2=A0=E2=80=98value=E2=80=99, is= =3D>=C2=A0=E2=80=9Crw=E2=80=9D, writer =3D>=C2=A0=E2=80=9C_value=E2= =80=9D, isa =3D>=C2=A0=E2=80=9CNum=E2=80=9D;</font></div><div><font styl= e=3D"font-size:11px" face=3D"Courier New"><span style=3D"white-space:pre-wr= ap"> </span>sub _value {</font></div><div><font style=3D"font-size:11px" fa= ce=3D"Courier New"><span style=3D"white-space:pre-wrap"> </span>my ($self,= $value) =3D @_;</font></div><div><font style=3D"font-size:11px" face=3D"Co= urier New"><span style=3D"white-space:pre-wrap"> </span>## insert some log= ic here</font></div><div><font style=3D"font-size:11px" face=3D"Courier New= "><span style=3D"white-space:pre-wrap"> </span>$self->value($value);</f= ont></div><div><font style=3D"font-size:11px" face=3D"Courier New"><span st= yle=3D"white-space:pre-wrap"> </span>return(1);</font></div><div><font sty= le=3D"font-size:11px" face=3D"Courier New"><span style=3D"white-space:pre-w= rap"> </span>}</font></div><div><font style=3D"font-size:11px" face=3D"Cour= ier New">}</font></div><div><font style=3D"font-size:11px" face=3D"Courier = New"><br></font></div><div><font style=3D"font-size:11px" face=3D"Courier N= ew">use Bogus;</font></div><div><font style=3D"font-size:11px" face=3D"Cour= ier New">my $a =3D new Bogus(value =3D> 1);</font></div></blockquote><di= v></div></blockquote></div><br></div><div class=3D"gmail_extra">The initial= mistake I believe you made was thinking that the argument to writer was in= tended to be a "I'll create this sub, Moose can use it".<br><= br></div><div class=3D"gmail_extra">Whereas that argument in this case is &= quot;Moose, create me a writer called _value".<br><br></div><div class= =3D"gmail_extra">So the RHS of that token is intended for more external int= erfaces, eg:<br><br>---<br></div><div class=3D"gmail_extra"><br>has value = =3D> ( is =3D> 'rw', writer =3D> "set_value" , r= eader =3D> "get_value");<br>...<br></div><div class=3D"gmail_e= xtra">$object->set_value(=C2=A0 $arg );<br></div><div class=3D"gmail_ext= ra">$object->get_value(); # returns $arg<br></div><div class=3D"gmail_ex= tra"><br>---<br><br><br></div><div class=3D"gmail_extra">so:<br><br></div><= div class=3D"gmail_extra">$writer_sub -> internal attribute store <br></= div><div class=3D"gmail_extra">$reader_sub <- internal attribute store<b= r><br></div><div class=3D"gmail_extra">$writer_sub and $readersub can somet= imes be the same sub, which is the default case with "is =3D> rw&qu= ot;.<br><br></div><div class=3D"gmail_extra">For comparion, see:=C2=A0 &quo= t;is =3D> raw", where $object->value won't exist.<br></div><= div class=3D"gmail_extra"><br clear=3D"all"><br>-- <br><div><div dir=3D"ltr= "><div>Kent<font size=3D"1"><b> <br><br></b></font></div><div><span style= =3D"color:rgb(204,204,204)"><font size=3D"1"><b>KENTNL</b> - <a href=3D"htt= ps://metacpan.org/author/KENTNL" target=3D"_blank">https://metacpan.org/aut= hor/KENTNL</a></font></span><br></div><div><br></div></div></div> </div></div> </blockquote></div> --089e0141ab7468ed4b051427d355--