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.


In reply to moose reader / writer by ag4ve

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.