Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

[ 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/ line 98

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

In reply to Re: Trouble coercing array elements by ikegami
in thread Trouble coercing array elements by docdurdee

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (2)
    As of 2018-01-16 21:43 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (192 votes). Check out past polls.