1 2 3 4 5 6 7 8
package ABC; use Moose; has 'container' => ( is => 'rw', isa => 'ArrayRef', default => sub { return [ 0..10 ] }, );
1 2 3 4 5 6 7 8 9 10 11 12
sub wertX { my $self = shift; if( @_ ) { if( $_[0] =~ /^[wb]$/ ) { $self->container->[11] = $_[0]; } else { confess "Invalid value $_[0] for attribute wertX"; } } return $self->container->[11] //= 'w'; # defaultwert }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
package ABC; use Moose; has 'container' => ( is => 'rw', isa => 'ArrayRef', default => sub { return [ 0..10 ] }, traits => [ 'Array' ], handles => { get_piece_by_id => 'get', # funktioniert # die folgenden funktionieren nicht get_color => sub { return shift->get_piece_by_id(0)->{color}->[0]; }, set_color => sub { shift->container->[0]->{color}->[0]) = shift; # leider kein set }, }, );
1 2 3 4 5 6 7 8
handles => { add_item => 'push', .... }, sub add_item { my ($self, @items) = @_; ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
has 'container' => ( traits => ['Array'], is => 'rw', isa => 'ArrayRef', lazy_build => 1, handles => { get_by_id => 'get', }, ); sub _build_container { return [ 'w', 1 .. 145 ]; } has 'color' => ( is => 'rw', isa => 'Str', lazy_build => 1, trigger => sub { $_[0]->container->[0] = $_[1] }, ); sub _build_color { return shift->get_by_id(0) || 'w'; } has 'counter' => ( traits => ['Counter'], is => 'rw', isa => 'Num', lazy_build => 1, handles => { inc_counter => 'inc', dec_counter => 'dec', reset_counter => 'reset', }, trigger => sub { $_[0]->container->[145] = $_[1] }, ); sub _build_counter { return shift->get_by_id( 145 ) || 1; }
2014-04-29T12:39:39 Muffi...
von daher würd ichs (wenns wirklich kurz sein soll) eher mit sub { ... } statt add_item probieren