Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: moose reader / writer

by FunkyMonk (Chancellor)
on Sep 19, 2011 at 14:19 UTC ( [id://926753]=note: print w/replies, xml ) Need Help??


in reply to moose reader / writer

Using Moose::Meta::Attribute::Native::Trait::Array lets you write code like...
package Thing; use Moose; has '_data' => ( is => 'ro', isa => 'ArrayRef', traits => [ 'Array' ], default => sub { [] }, # writer => '_add_data', # reader => '_get_data', handles => { add_data => 'push', get_data => 'get', }, ); package main; use Data::Dump; my $thing = Thing->new; $thing->add_data({$_ => $_**2 }) for 1..5; dd $thing->get_data(2); dd $thing; __END__ { 3 => 9 } bless({ _data => [{ 1 => 1 }, { 2 => 4 }, { 3 => 9 }, { 4 => 16 }, { 5 => 25 + }], }, "Thing")

Does that do the job for you?

Replies are listed 'Best First'.
Re^2: moose reader / writer
by ag4ve (Monk) on Sep 19, 2011 at 19:03 UTC

    just to update the node for future googlers, this works great (needed 'elements' and not 'get' i think - it's what worked). but, here's what i have:

    package Thing; use Data::Dumper; use Moose; has 'search' => ( is => 'rw', isa => 'HashRef', ); has '_data' => ( is => 'ro', isa => 'ArrayRef', traits => [ 'Array' ], default => sub { [] }, handles => { _add_data => 'push', _get_data => 'elements', }, ); sub data { my ($self, $stuff, @err) = @_; if( $stuff and ref( $stuff ) eq 'ARRAY' ) { my $values = [ values %{ $self->search } ]; foreach my $row ( @{ $stuff } ) { $self->_add_data( $row ) if( scalar( grep { $_ ~~ $values } v +alues %{ $row } ) > 1 ); } } else { return $self->_get_data; } } 1;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-19 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found