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? -Andy On Apr 19, 2015 9:53 PM, "Kent Fredric" <kentfred...@gmail.com> wrote: > > On 20 April 2015 at 07:00, Andrew Hicox <and...@hicox.com> wrote: > >> 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); >> >> > 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 => ( is => 'rw', writer => "set_value" , reader => "get_value"); > ... > $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 => rw". > > For comparion, see: "is => raw", where $object->value won't exist. > > > -- > Kent > > *KENTNL* - https://metacpan.org/author/KENTNL > >