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

Moose n00b question about writer / reader

Thread Next
From:
Andrew Hicox
Date:
April 19, 2015 19:00
Subject:
Moose n00b question about writer / reader
Message ID:
08CCE890-96D0-4BCF-8BFB-3352C9DD72C6@hicox.com
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.pod#Accessor_methods>

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
		$self->value($value);
		return(1);
	}
}

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,

-Andy




Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About