Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: moose reader / writer

by FunkyMonk (Canon)
on Sep 19, 2011 at 14:19 UTC ( #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?


Comment on Re: moose reader / writer
Download Code
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
Node Status?
node history
Node Type: note [id://926753]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (18)
As of 2015-07-06 13:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (75 votes), past polls