develooper Front page | perl.moose | Postings from April 2015

Moose n00b question about writer / reader

Thread Next
Andrew Hicox
April 19, 2015 19:00
Moose n00b question about writer / reader
Message ID:
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: <>

I want to create a class that has some attributes. However, there’s some logic that needs to be executed when setting values on these attributes. In some cases, I’ll need to throw an error like “you can’t set that value on this object attribute”, 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’d 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 ‘value’, is => “rw”, writer => “_value”, isa => “Num”;
	sub _value {
		my ($self, $value) = @_;
		## insert some logic here

use Bogus;
my $a = new Bogus(value => 1);

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

thanks everyone,


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About