Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found