Re: Moose n00b question about writer / reader
[email protected] ("Chris Prather") Mon, 20 Apr 2015 06:56:39 -0700 (PDT)
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <1429538199009.fa1640fe@Nodemailer> |
------Nodemailer-0.5.0-?=_1-1429538199772
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
The default accessors are pretty simple. Generally what people do is create=
a private one that they then call from their own method.
has foo =3D> ( writer =3D> '=5Ffoo' ... );
sub foo { ... $self->=5Ffoo($value) ... }
-Chris
On Mon, Apr 20, 2015 at 9:22 AM, Andrew Hicox <[email protected]> wrote:
> 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=3F
> -Andy
> On Apr 19, 2015 9:53 PM, =22Kent Fredric=22 <[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 =
attribute,
>>> do my logic in there, then set the attribute value or throw an error, =
right=3F
>>>
>>> Why doesn't this work=3F
>>>
>>> package Bogus {
>>> use Moose;
>>> has =E2=80=98value=E2=80=99, is =3D> =E2=80=9Crw=E2=80=9D, writer =3D> =
=E2=80=9C=5Fvalue=E2=80=9D, isa =3D> =E2=80=9CNum=E2=80=9D;
>>> sub =5Fvalue {
>>> my ($self, $value) =3D @=5F;
>>> ## 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 =22I'll create this sub, Moose can use it=22=
.
>>
>> Whereas that argument in this case is =22Moose, create me a writer =
called
>> =5Fvalue=22.
>>
>> So the RHS of that token is intended for more external interfaces, eg:
>>
>> ---
>>
>> has value =3D> ( is =3D> 'rw', writer =3D> =22set=5Fvalue=22 , reader =
=3D> =22get=5Fvalue=22);
>> ...
>> $object->set=5Fvalue( $arg );
>> $object->get=5Fvalue(); # returns $arg
>>
>> ---
>>
>>
>> so:
>>
>> $writer=5Fsub -> internal attribute store
>> $reader=5Fsub <- internal attribute store
>>
>> $writer=5Fsub and $readersub can sometimes be the same sub, which is =
the
>> default case with =22is =3D> rw=22.
>>
>> For comparion, see: =22is =3D> raw=22, where $object->value won't exist=
.
>>
>>
>> --
>> Kent
>>
>> *KENTNL* - https://metacpan.org/author/KENTNL
>>
>>
------Nodemailer-0.5.0-?=_1-1429538199772
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<div>The default accessors are pretty simple. Generally what people do is =
create a private one that they then call from their own method.</div>
<div><br></div>
<div>has foo =3D> ( writer =3D> '=5Ffoo' ... );</div>
<div>sub foo { ... $self->=5Ffoo($value) ... }</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 Mon, Apr 20, 2015 at 9:22 AM=
, Andrew Hicox <span dir=3D=22ltr=22><<a href=3D=22mailto:andrew@hicox.=
com=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>
<p dir=3D=22ltr=22>Thanks guys! The thing I didn't understand was that =
reader and writer just change the name of the moose default accessor =
methods. </p>
<p dir=3D=22ltr=22>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=3F</p>
<p dir=3D=22ltr=22>-Andy </p>
<div class=3D=22gmail=5Fquote=22>On Apr 19, 2015 9:53 PM, =22Kent =
Fredric=22 <<a href=3D=22mailto:kentfredric@gmail.=
com=22>[email protected]</a>> wrote:<br><blockquote =
class=3D=22gmail=5Fquote=22 style=3D=22margin:0 0 0 .8ex;border-left:1px =
#ccc solid;padding-left:1ex=22>
<div dir=3D=22ltr=22>
<div class=3D=22gmail=5Fextra=22>
<br><div class=3D=22gmail=5Fquote=22>On 20 April 2015 at 07:00, Andrew =
Hicox <span dir=3D=22ltr=22><<a href=3D=22mailto:andrew@hicox.=
com=22>[email protected]</a>></span> wrote:<br><blockquote =
class=3D=22gmail=5Fquote=22 style=3D=22margin:0 0 0 .8ex;border-left:1px =
#ccc solid;padding-left:1ex=22>
<div>This seems like I=E2=80=99d just need to declare a writer sub for the =
attribute, do my logic in there, then set the attribute value or throw an =
error, right=3F</div>
<div><br></div>
<div>Why doesn't this work=3F</div>
<div><br></div>
<blockquote style=3D=22margin:0 0 0 40px;border:none;padding:0px=22>
<div><font style=3D=22font-size:11px=22 face=3D=22Courier New=22>package =
Bogus {</font></div>
<div><font style=3D=22font-size:11px=22 face=3D=22Courier New=22><span =
style=3D=22white-space:pre-wrap=22> </span>use Moose;</font></div>
<div><font style=3D=22font-size:11px=22 face=3D=22Courier New=22><span =
style=3D=22white-space:pre-wrap=22> </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=5Fval=
ue=E2=80=9D, isa =3D>=C2=A0=E2=80=9CNum=E2=80=9D;</font></div>
<div><font style=3D=22font-size:11px=22 face=3D=22Courier New=22><span =
style=3D=22white-space:pre-wrap=22> </span>sub =5Fvalue {</font></div>
<div><font style=3D=22font-size:11px=22 face=3D=22Courier New=22><span =
style=3D=22white-space:pre-wrap=22> </span>my ($self, $value) =3D =
@=5F;</font></div>
<div><font style=3D=22font-size:11px=22 face=3D=22Courier New=22><span =
style=3D=22white-space:pre-wrap=22> </span>## insert some logic =
here</font></div>
<div><font style=3D=22font-size:11px=22 face=3D=22Courier New=22><span =
style=3D=22white-space:pre-wrap=22> </span>$self->value($value);</font>=
</div>
<div><font style=3D=22font-size:11px=22 face=3D=22Courier New=22><span =
style=3D=22white-space:pre-wrap=22> </span>return(1);</font></div>
<div><font style=3D=22font-size:11px=22 face=3D=22Courier New=22><span =
style=3D=22white-space:pre-wrap=22> </span>}</font></div>
<div><font style=3D=22font-size:11px=22 face=3D=22Courier =
New=22>}</font></div>
<div><font style=3D=22font-size:11px=22 face=3D=22Courier =
New=22><br></font></div>
<div><font style=3D=22font-size:11px=22 face=3D=22Courier New=22>use =
Bogus;</font></div>
<div><font style=3D=22font-size:11px=22 face=3D=22Courier New=22>my $a =3D =
new Bogus(value =3D> 1);</font></div>
</blockquote>
<div></div>
</blockquote>
</div>
<br></div>
<div class=3D=22gmail=5Fextra=22>The initial mistake I believe you made was=
thinking that the argument to writer was intended to be a =22I'll create =
this sub, Moose can use it=22.<br><br></div>
<div class=3D=22gmail=5Fextra=22>Whereas that argument in this case is =
=22Moose, create me a writer called =5Fvalue=22.<br><br></div>
<div class=3D=22gmail=5Fextra=22>So the RHS of that token is intended for =
more external interfaces, eg:<br><br>---<br></div>
<div class=3D=22gmail=5Fextra=22>
<br>has value =3D> ( is =3D> 'rw', writer =3D> =22set=5Fvalue=22 ,=
reader =3D> =22get=5Fvalue=22);<br>...<br></div>
<div class=3D=22gmail=5Fextra=22>$object->set=5Fvalue(=C2=A0 $arg =
);<br></div>
<div class=3D=22gmail=5Fextra=22>$object->get=5Fvalue(); # returns =
$arg<br></div>
<div class=3D=22gmail=5Fextra=22>
<br>---<br><br><br></div>
<div class=3D=22gmail=5Fextra=22>so:<br><br></div>
<div class=3D=22gmail=5Fextra=22>$writer=5Fsub -> internal attribute =
store <br></div>
<div class=3D=22gmail=5Fextra=22>$reader=5Fsub <- internal attribute =
store<br><br></div>
<div class=3D=22gmail=5Fextra=22>$writer=5Fsub and $readersub can sometimes=
be the same sub, which is the default case with =22is =3D> rw=22.=
<br><br></div>
<div class=3D=22gmail=5Fextra=22>For comparion, see:=C2=A0 =22is =3D> =
raw=22, where $object->value won't exist.<br></div>
<div class=3D=22gmail=5Fextra=22>
<br clear=3D=22all=22><br>-- <br><div><div dir=3D=22ltr=22>
<div>Kent<font size=3D=221=22><b> <br><br></b></font>
</div>
<div>
<span style=3D=22color:rgb(204,204,204)=22><font size=3D=221=22><b>KENTNL</=
b> - <a href=3D=22https://metacpan.org/author/KENTNL=22>https://metacpan.=
org/author/KENTNL</a></font></span><br></div>
<div><br></div>
</div></div>
</div>
</div>
</blockquote>
</div>
</div></blockquote></div><br>
------Nodemailer-0.5.0-?=_1-1429538199772--