perl - Dynamically generate writer/reader from attribute names in Moose -


in moose can put restrictions on instance attributes or add getters/setters so:

has 'color' => (    => 'rw',   isa => 'str',   writer => '_set_color', ); 

my question is, there way dynamically array of elements? it's possible this:

has ['color', 'temperature', 'size'] => (    => 'rw',   isa => 'str', ); 

but there way create each of these items own writer (or reader), e.g. _set_color, _set_temperature, _set_size? tried see if following yielded insight, returned error

bad accessor/reader/writer/predicate/clearer format, must hash ref

has ['color', 'temperature', 'size'] => (    => 'rw',   isa => 'str',   writer => sub {     print dumper(\@_);     return; ); 

what hoping (which doesn't work):

has ['color', 'temperature', 'size'] => (    => 'rw',   isa => 'str',   writer => "_set_$_"; ); 

i need custom writers, going ones provided moose not work me.

has isn't magic. it's subroutine call. should work (untested):

for (qw[colour temperature size]) {   has $_ => (         => 'rw',     isa    => 'str',     writer => "_set_$_",   ); } 

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 -