perl - In Moose, how do I modify an attribute any time it is set? -


if have attribute needs modified time set, there slick way of doing short of writing accessor , mucking around directly content of $self, done in example?

package foo; use moose;  has 'bar' => (     isa => 'str',     reader => 'get_bar', );  sub set_bar {     ($self, $bar) = @_;     $self->{bar} = "modified: $bar"; } 

i considered trigger, seemed require same approach.

is working directly hash reference in $self considered bad practice in moose, or worrying non-issue?

i'm not sure kind of modification need, might able achieve need using type coercion:

package foo; use moose;  use moose::util::typeconstraints;  subtype 'modstr'      => 'str'     => { /^modified: /};  coerce 'modstr'     => 'str'     => via { "modified: $_" };  has 'bar' => (      isa => 'modstr',       => 'rw',      coerce => 1, ); 

if use approach, not values modified. passes validation modstr used directly:

my $f = foo->new(); $f->bar('modified: bar');  # set without modification 

this weakness ok or make approach unusable. in right circumstances, might advantage.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -