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
test.plpackage 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;
OUT#!/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;
What I want:$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
$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 (Bishop) on Sep 19, 2011 at 14:19 UTC | |
by ag4ve (Monk) on Sep 19, 2011 at 19:03 UTC |