Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Trouble coercing array elements

by ikegami (Pope)
on Jul 28, 2011 at 21:46 UTC ( #917352=note: print w/ replies, xml ) Need Help??


in reply to Trouble coercing array elements

[ The parent was crossposted on StackOverflow. ]

Building on my StackOverflow answer (where I didn't handle the trait), here's one that does:

{ package MyArray; use namespace::autoclean; use Moose; use Moose::Util::TypeConstraints; subtype 'MyArray::Ents::Level2' => as 'ArrayRef[Str|Num|Int]'; coerce 'MyArray::Ents::Level2' => from 'Str|Num|Int' => via { [ $_ ] }; my $level2_constraint = Moose::Util::TypeConstraints::find_type_constraint( 'MyArray::Ents::Level2'); # Doesn't work if I remove this?!? subtype 'MyArray::Ents' => as 'ArrayRef[MyArray::Ents::Level2]'; coerce 'ArrayRef[MyArray::Ents::Level2]' => from 'ArrayRef[MyArray::Ents::Level2|Str|Num|Int]' => via { [ map $level2_constraint->coerce($_), @$_ ] }; no Moose::Util::TypeConstraints; has 'myarray' => ( traits => ['Array'], is => 'rw', isa => 'ArrayRef[MyArray::Ents::Level2]', default => sub { [] }, handles => { push => 'push', get => 'get', set => 'set', elements => 'elements', count => 'count', }, coerce => 1, ); no Moose; __PACKAGE__->meta->make_immutable; } use Modern::Perl; use Data::Dumper qw( Dumper ); sub dump_it { local $Data::Dumper::Useqq = 1; local $Data::Dumper::Terse = 1; local $Data::Dumper::Indent = 0; print(Dumper($_[0]), "\n"); } my $a0 = MyArray->new( myarray => [ 0, [ 0, 0, 0 ], [1,2,3] ] ) ; dump_it($a0); $a0->set(1, 'cat'); dump_it($a0); $a0->push(2.0); dump_it($a0);
bless( {"myarray" => [[0],[0,0,0],[1,2,3]]}, 'MyArray' ) bless( {"myarray" => [[0],["cat"],[1,2,3]]}, 'MyArray' ) bless( {"myarray" => [[0],["cat"],[1,2,3],[2]]}, 'MyArray' )

Unfortunately, the following is not compatible with the trait:

subtype 'MyArray::Ents::Level2' => as 'ArrayRef[Str|Num|Int]'; coerce 'MyArray::Ents::Level2' => from 'Str|Num|Int' => via { [ $_ ] }; my $level2_constraint = Moose::Util::TypeConstraints::find_type_constraint( 'MyArray::Ents::Level2'); subtype 'MyArray::Ents' => as 'ArrayRef[MyArray::Ents::Level2]'; coerce 'MyArray::Ents' => from 'ArrayRef[MyArray::Ents::Level2|Str|Num|Int]' => via { [ map $level2_constraint->coerce($_), @$_ ] }; ... isa => 'MyArray::Ents'

It results in:

Can't call method "is_a_type_of" on an undefined value at .../Moose/Me +ta/Attribute/Native/Trait.pm line 98

I don't like adding coercions to global types, although ArrayRef[MyArray::Ents::Level2] is not as global as others.


Comment on Re: Trouble coercing array elements
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://917352]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2014-09-20 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls