| 1 | package TaskPipe::Task_SeparateArray; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | extends 'TaskPipe::Task'; | ||
| 5 | use Data::Dumper; | ||
| 6 | |||
| 7 | |||
| 8 | has test_pinterp => (is => 'ro', isa => 'ArrayRef[HashRef]', default => sub{[{ | ||
| 9 | |||
| 10 | unimportant => 'data', | ||
| 11 | array => [{ | ||
| 12 | one => 'result 1-1', | ||
| 13 | two => 'result 1-2', | ||
| 14 | three => 'result 1-3' | ||
| 15 | }, { | ||
| 16 | one => 'result 2-1', | ||
| 17 | two => 'result 2-2', | ||
| 18 | three => 'result 2-3' | ||
| 19 | }] | ||
| 20 | |||
| 21 | }]}); | ||
| 22 | |||
| 23 | |||
| 24 | sub action{ | ||
| 25 | my ($self) = @_; | ||
| 26 | |||
| 27 | confess "Need an 'array' parameter" unless $self->pinterp->{array}; | ||
| 28 | my $array_ref; | ||
| 29 | |||
| 30 | if ( ref $self->pinterp->{array} eq ref [] ){ | ||
| 31 | |||
| 32 | $array_ref = $self->pinterp->{array}; | ||
| 33 | |||
| 34 | } elsif( ref $self->pinterp->{array} eq ref '' ) { | ||
| 35 | |||
| 36 | my @vals; | ||
| 37 | eval '@vals = ('.$self->pinterp->{array}.');'; | ||
| 38 | $array_ref = []; | ||
| 39 | foreach my $val (@vals){ | ||
| 40 | push @$array_ref, +{ li => $val }; | ||
| 41 | } | ||
| 42 | |||
| 43 | } else { | ||
| 44 | |||
| 45 | confess "unrecognised type for 'array' parameter. Expected string or arrayref but got ".ref($self->pinterp->{array}); | ||
| 46 | |||
| 47 | } | ||
| 48 | |||
| 49 | if ( @$array_ref ){ | ||
| 50 | confess "value specified in 'array' should be an array of hashrefs" unless ref($array_ref->[0]) eq ref {}; | ||
| 51 | } | ||
| 52 | |||
| 53 | return $array_ref; | ||
| 54 | |||
| 55 | } | ||
| 56 | |||
| 57 | 1; |