Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

moose reader / writer

by ag4ve (Monk)
on Sep 19, 2011 at 10:40 UTC ( [id://926711]=perlquestion: print w/replies, xml ) Need Help??

ag4ve has asked for the wisdom of the Perl Monks concerning the following question:

I'm guessing i don't totally understand how getters / setters work because I just can't figure out how to get this to work:

Thing.pm

package Thing; use Data::Dumper; use Moose; has 'search' => ( is => 'rw', isa => 'HashRef', ); has '_data' => ( is => 'ro', isa => 'ArrayRef', traits => [ 'Array' ], default => sub { [] }, writer => '_add_data', reader => '_get_data', ); sub data { my ($self) = shift; if( @_ ) { my $stuff = shift; my $values = [ values %{ $self->search } ]; foreach my $row ( @{ $stuff } ) { print Dumper [ $row ]; print @{ $self->_get_data }, "\n"; print $row, "\n"; $self->_add_data( push @{ $self->_get_data }, $row ) if( scal +ar( grep { $_ ~~ $values } values %{ $row } ) > 1 ); } } else { return $self->_get_data; } } 1;
test.pl
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use lib './'; use Thing; my $test = Thing->new; my $search = { 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four' }; $test->search( $search ); print Dumper $test->search; my $data = [{ thing => 'two', blah => 'barf', => other => 'four' }]; $test->data( $data ); $data = [{ thing => 'three', blah => 'one', => other => 'four', bl => +"another", of => "something" }]; $test->data( $data ); print Dumper $test->data;
OUT
$VAR1 = { '4' => 'four', '1' => 'one', '3' => 'three', '2' => 'two' }; $VAR1 = [ { 'blah' => 'barf', 'other' => 'four', 'thing' => 'two' } ]; HASH(0x1c48728) Attribute (_data) does not pass the type constraint because: Validatio +n failed for 'ArrayRef' with value 1 at /Thing.pm line 30 Thing::data('Thing=HASH(0x1c41a68)', 'ARRAY(0x1caa868)') calle +d at ./test.pl line 18
What I want:
$VAR1 = [ { 'blah' => 'barf', 'other' => 'four', 'thing' => 'two' }, { 'blah' => 'one', 'of' => 'something', 'other' => 'four', 'thing' => 'three', 'bl' => 'another' } ];

So, if I do $thing->data( [{ 'one' => 'thing', 'to' => 'use', 'another one' => 'one', 'two' => 'two' }, { 'another' => 'thing', 'to' => 'push', 'and => 'so on', 'just to make => 'one, 'this valid' => 'two' }] ); it adds each valid hash to _data.

Replies are listed 'Best First'.
Re: moose reader / writer
by FunkyMonk (Chancellor) on Sep 19, 2011 at 14:19 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: perlquestion [id://926711]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2024-04-18 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found