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

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -