Re: Moose n00b question about writer / reader
[email protected] (Kent Fredric) Mon, 20 Apr 2015 14:53:31 +1200
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <CAATnKFDoGXH=prNXVCo5dNkywWeVeiif1jB5-rX+Bjk0wF-ekg@mail.gmail.com> |
--20cf301afaf70e257d05141f0965 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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 att= ribute, > do my logic in there, then set the attribute value or throw an error, rig= ht? > > 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_v= alue"); ... $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. --=20 Kent *KENTNL* - https://metacpan.org/author/KENTNL --20cf301afaf70e257d05141f0965 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <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"ma= ilto:[email protected]" target=3D"_blank">[email protected]</a>></span> wr= ote:<br><blockquote 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 jus= t need to declare a writer sub for the attribute, do my logic in there, the= n set the attribute value or throw an error, right?</div><div><br></div><di= v>Why doesn't this work?</div><div><br></div><blockquote style=3D"margi= n:0 0 0 40px;border:none;padding:0px"><div><font style=3D"font-size:11px" f= ace=3D"Courier New">package Bogus {</font></div><div><font style=3D"font-si= ze:11px" face=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><di= v><font style=3D"font-size:11px" face=3D"Courier New"><span style=3D"white-= space:pre-wrap"> </span>sub _value {</font></div><div><font style=3D"font-s= ize:11px" face=3D"Courier New"><span style=3D"white-space:pre-wrap"> </spa= n>my ($self, $value) =3D @_;</font></div><div><font style=3D"font-size:11px= " face=3D"Courier New"><span style=3D"white-space:pre-wrap"> </span>## ins= ert some logic 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);</font></div><div><font style=3D"font-size:11px" face=3D"Courier N= ew"><span style=3D"white-space:pre-wrap"> </span>return(1);</font></div><d= iv><font style=3D"font-size:11px" face=3D"Courier New"><span style=3D"white= -space:pre-wrap"> </span>}</font></div><div><font style=3D"font-size:11px" = face=3D"Courier 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 New">use Bogus;</font></div><div><font style=3D"font-size:11px"= face=3D"Courier New">my $a =3D new Bogus(value =3D> 1);</font></div></b= lockquote><div></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 intended to be a "I'll create this sub, Moose can use i= t".<br><br></div><div class=3D"gmail_extra">Whereas that argument in t= his case is "Moose, create me a writer called _value".<br><br></d= iv><div class=3D"gmail_extra">So the RHS of that token is intended for more= external interfaces, eg:<br><br>---<br></div><div class=3D"gmail_extra"><b= r>has value =3D> ( is =3D> 'rw', writer =3D> "set_val= ue" , reader =3D> "get_value");<br>...<br></div><div clas= s=3D"gmail_extra">$object->set_value(=C2=A0 $arg );<br></div><div class= =3D"gmail_extra">$object->get_value(); # returns $arg<br></div><div clas= s=3D"gmail_extra"><br>---<br><br><br></div><div class=3D"gmail_extra">so:<b= r><br></div><div class=3D"gmail_extra">$writer_sub -> internal attribute= store <br></div><div class=3D"gmail_extra">$reader_sub <- internal attr= ibute store<br><br></div><div class=3D"gmail_extra">$writer_sub and $reader= sub can sometimes be the same sub, which is the default case with "is = =3D> rw".<br><br></div><div class=3D"gmail_extra">For comparion, se= e:=C2=A0 "is =3D> raw", where $object->value won't exis= t.<br></div><div class=3D"gmail_extra"><br clear=3D"all"><br>-- <br><div cl= ass=3D"gmail_signature"><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"https://metacpan.org/author/KENTNL" targe= t=3D"_blank">https://metacpan.org/author/KENTNL</a></font></span><br></div>= <div><br></div></div></div> </div></div> --20cf301afaf70e257d05141f0965--