Moose n00b question about writer / reader

[email protected] (Andrew Hicox) Sun, 19 Apr 2015 14:00:23 -0500
Newsgroups perl.moose
Message-ID <[email protected]>
--Apple-Mail=_E007F336-CD3A-4DC2-94AE-3A5E1B066F18
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8

Hello Moose list,

Apologies if this is a dumb question, but I have repeatedly tried to =
understand this section of the documentation, and I need some help:

=
http://search.cpan.org/~ether/Moose-2.1404/lib/Moose/Manual/Attributes.pod=
#Accessor_methods =
<http://search.cpan.org/~ether/Moose-2.1404/lib/Moose/Manual/Attributes.po=
d#Accessor_methods>

I want to create a class that has some attributes. However, there=E2=80=99=
s some logic that needs to be executed when setting values on these =
attributes. In some cases, I=E2=80=99ll need to throw an error like =
=E2=80=9Cyou can=E2=80=99t set that value on this object attribute=E2=80=9D=
, I also need to be able to modify the value on the way into the object =
sort of like a DECODE() statement in SQL, etc.

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?

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);

Can what I=E2=80=99m trying to do even be done in Moose?
again, sorry for the dumb question. I=E2=80=99m quite sure I=E2=80=99ve =
just missed something in my understanding.

thanks everyone,

-Andy




--Apple-Mail=_E007F336-CD3A-4DC2-94AE-3A5E1B066F18
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=utf-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html =
charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" =
class=3D"">Hello Moose list,<div class=3D""><br class=3D""></div><div =
class=3D"">Apologies if this is a dumb question, but I have repeatedly =
tried to understand this section of the documentation, and I need some =
help:</div><div class=3D""><br class=3D""></div><div class=3D""><a =
href=3D"http://search.cpan.org/~ether/Moose-2.1404/lib/Moose/Manual/Attrib=
utes.pod#Accessor_methods" =
class=3D"">http://search.cpan.org/~ether/Moose-2.1404/lib/Moose/Manual/Att=
ributes.pod#Accessor_methods</a></div><div class=3D""><br =
class=3D""></div><div class=3D"">I want to create a class that has some =
attributes. However, there=E2=80=99s some logic that needs to be =
executed when setting values on these attributes. In some cases, I=E2=80=99=
ll need to throw an error like =E2=80=9Cyou can=E2=80=99t set that value =
on this object attribute=E2=80=9D, I also need to be able to modify the =
value on the way into the object sort of like a DECODE() statement in =
SQL, etc.</div><div class=3D""><br class=3D""></div><div class=3D"">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?</div><div class=3D""><br class=3D""></div><div =
class=3D"">Why doesn't this work?</div><div class=3D""><br =
class=3D""></div><blockquote style=3D"margin: 0 0 0 40px; border: none; =
padding: 0px;" class=3D""><div class=3D""><font face=3D"Courier New" =
style=3D"font-size: 11px;" class=3D"">package Bogus {</font></div><div =
class=3D""><font face=3D"Courier New" style=3D"font-size: 11px;" =
class=3D""><span class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span>use Moose;</font></div><div class=3D""><font face=3D"Courier New" =
style=3D"font-size: 11px;" class=3D""><span class=3D"Apple-tab-span" =
style=3D"white-space:pre">	</span>has&nbsp;=E2=80=98value=E2=80=99, =
is =3D&gt;&nbsp;=E2=80=9Crw=E2=80=9D, writer =3D&gt;&nbsp;=E2=80=9C_value=E2=
=80=9D, isa =3D&gt;&nbsp;=E2=80=9CNum=E2=80=9D;</font></div><div =
class=3D""><font face=3D"Courier New" style=3D"font-size: 11px;" =
class=3D""><span class=3D"Apple-tab-span" style=3D"white-space:pre">	=
</span>sub _value {</font></div><div class=3D""><font face=3D"Courier =
New" style=3D"font-size: 11px;" class=3D""><span class=3D"Apple-tab-span" =
style=3D"white-space:pre">		</span>my ($self, $value) =3D =
@_;</font></div><div class=3D""><font face=3D"Courier New" =
style=3D"font-size: 11px;" class=3D""><span class=3D"Apple-tab-span" =
style=3D"white-space:pre">		</span>## insert some logic =
here</font></div><div class=3D""><font face=3D"Courier New" =
style=3D"font-size: 11px;" class=3D""><span class=3D"Apple-tab-span" =
style=3D"white-space:pre">		=
</span>$self-&gt;value($value);</font></div><div class=3D""><font =
face=3D"Courier New" style=3D"font-size: 11px;" class=3D""><span =
class=3D"Apple-tab-span" style=3D"white-space:pre">		=
</span>return(1);</font></div><div class=3D""><font face=3D"Courier New" =
style=3D"font-size: 11px;" class=3D""><span class=3D"Apple-tab-span" =
style=3D"white-space:pre">	</span>}</font></div><div class=3D""><font=
 face=3D"Courier New" style=3D"font-size: 11px;" =
class=3D"">}</font></div><div class=3D""><font face=3D"Courier New" =
style=3D"font-size: 11px;" class=3D""><br class=3D""></font></div><div =
class=3D""><font face=3D"Courier New" style=3D"font-size: 11px;" =
class=3D"">use Bogus;</font></div><div class=3D""><font face=3D"Courier =
New" style=3D"font-size: 11px;" class=3D"">my $a =3D new Bogus(value =
=3D&gt; 1);</font></div></blockquote><div class=3D""><br =
class=3D""></div><div class=3D"">Can what I=E2=80=99m trying to do even =
be done in Moose?</div><div class=3D"">again, sorry for the dumb =
question. I=E2=80=99m quite sure I=E2=80=99ve just missed something in =
my understanding.</div><div class=3D""><br class=3D""></div><div =
class=3D"">thanks everyone,</div><div class=3D""><br class=3D""></div><div=
 class=3D"">-Andy</div><div class=3D""><br class=3D""></div><div =
class=3D""><br class=3D""></div><div class=3D""><br =
class=3D""></div></body></html>=

--Apple-Mail=_E007F336-CD3A-4DC2-94AE-3A5E1B066F18--