On Thu, Dec 24, 2009 at 05:34:07PM -0500, Chris Prather wrote: > On Thu, Dec 24, 2009 at 4:55 PM, Mike Friedman <fri...@friedo.com> wrote: > > You should be able to do this with triggers. For example: > > > > has foo => ( isa => 'Str', is => 'rw', trigger => \&_munge_foo ); > > > > sub _munge_foo { > > my ( $self, $new_foo, $old_foo ) = @_; > > > > $new_foo =~ s/\W+/_/gs; > > $self->{foo} = $new_foo; > > } > > > > The trigger will be called every time foo is set, including in the > > constructor. > > > > > > Mike > > A Trigger really is the wrong way to go about this. While yes this > will work it's not considered "best practice".
I don't think Chris was strong enough here... accessing the hash values directly for a Moose object is *never* correct (except in really really weird edge cases like custom serialization code, and only if absolutely necessary). If you find yourself thinking that you want to access a hash slot directly like this, you're doing it wrong, and you should really try to find the right way to do it. (In this case, this is not the intended use for triggers, which is why you can't do what you want to do directly with accessor methods and such.) -doy