perl - Modify Moose attribute methods -
i'm creating list of attributes (more 3 shown below), of share common methods. possible add trigger 1 of methods:
# create bunch of attributes $attr ( qw( title name address ) ) { has $attr => ( => 'rw', isa => 'str' ); around $attr => sub { # more stuff here. } } # add trigger has_another_method 'title' => ( trigger => \&_trigger_title ); i know can meta information attributes, haven't found enable me change attribute methods (and perhaps reason). being able keep code clean, , mean common bits defined in 1 place. if not, can create attribute separately, , include trigger method.
update
the answers have made clear changing attribute after has been created not idea. instead, i've opted different method allows me keep attribute options in 1 place. example little simplistic, demonstrates idea:
# create bunch of attributes $attr ( qw( title name address ) ) { %options = ( => 'rw', isa => 'str' ); # add trigger title attribute. $options{ 'trigger' } = \&_trigger_title if $attr eq 'title'; has $attr => ( %options ); around $attr => sub { # more stuff here. } }
triggers attribute on attribute, defined read only. could find_meta( $attribute )->get_attribute('trigger')->set_value( $attribute, sub { new trigger }), you're breaking encapsulation here.
i declare common attributes in loop, , declare special cases elsewhere.
Comments
Post a Comment