Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

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

Thanks ikegami! Playing around with your code helped me learn this cool stuff. I soon realized that I wanted to leave both ArrayRef and Objects alone. Starting from ikegami's code, I added this, cleaned it up (I hope) and am posting it here for reference. Perl rocks! Moose rocks!

{ package MyArray; use namespace::autoclean; use Moose; use Moose::Util::TypeConstraints; subtype 'ValueOrObject' => as 'Value|Object'; subtype 'Inside::MyArray::Ents' => as 'ArrayRef[Value]|Object'; coerce 'Inside::MyArray::Ents' => from 'Value' => via { return [ $_ ] }; my $level2_constraint = Moose::Util::TypeConstraints::find_type_constraint( 'Inside::MyArray::Ents'); subtype 'MyArray::Ents' => as 'ArrayRef[Inside::MyArray::Ents]'; coerce 'MyArray::Ents' => from 'ArrayRef[Inside::MyArray::Ents|ValueOrObject]' => via { [ map $level2_constraint->coerce($_), @$_ ] }; has 'myarray' => ( traits => ['Array'], is => 'rw', isa => 'MyArray::Ents', default => sub { [] }, handles => { push => 'push', pop => 'pop', get => 'get', set => 'set', elements => 'elements', count => 'count', }, coerce => 1, ); __PACKAGE__->meta->make_immutable; } use Modern::Perl; use Math::VectorReal; my $a0 = MyArray->new( myarray => [ 0, [ 0, 0, 0 ], [1,2,3] ] ) ; use Data::Dumper; print Dumper $a0; $a0->set(1,'cat'); print Dumper $a0; $a0->push('dog'); print Dumper $a0; $a0->push('1.0'); print Dumper $a0; my $vec1 = vector(1,2,3); $a0->push($vec1); print Dumper $a0; my $vec3 = $a0->get(5); my $vec2 = $a0->pop; print Dumper $vec2; print Dumper $vec3; say $vec1-$vec2; 1;

In reply to Re: Trouble coercing array elements by docdurdee
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 rifling through the Monastery: (7)
    As of 2018-04-26 11:41 GMT
    Find Nodes?
      Voting Booth?