Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^14: How to completely destroy class attributes with Test::Most?

by nysus (Parson)
on Aug 29, 2019 at 00:06 UTC ( [id://11105205]=note: print w/replies, xml ) Need Help??


in reply to Re^13: How to completely destroy class attributes with Test::Most?
in thread How to completely destroy class attributes with Test::Most?

I'll give a concrete example to make clear what I mean:

package FileParser; use parent qw ( Dondley::WestfieldVote::FileCollector ); sub get_data { my $s = shift; return $s->get_obj_prop('data', 'raw_data', $_[0]); } package FileCollector; sub get_obj_prop { my $s = shift; my $obj = shift; my $prop = shift; my $file = shift || $s->selected_file; # grabs the current file in +iterator if (!$prop || !$obj || !$file) { $s->_croak ("Missing arguments to get_obj_prop method" . ' at ' . (caller(0))[1] . ', line ' . (caller(0))[2] ); } my $o = $obj . '_obj'; my $object = $s->{_files}{$file}{$o}; my $attr = "_$prop"; if (! exists $object->{$attr} ) { $s->croak ("Non-existent $obj object attribute requested: '$prop'" . ' at ' . (caller(0))[1] . ', line ' . (caller(0))[2] ); } my $value = $object->{$attr}; if (ref $value eq 'ARRAY') { return @$value; } else { return $value; } } package Data.pm sub new { my $class = shift; my $data = shift; my $obj = bless { _raw_data => $data, # raw Spreadsheet::Read object _stripped_data => undef, _num_rows => undef, _cols => undef, _num_cols => undef, _non_blank_cols => undef, _first_row => undef, }, $class; return $obj; } # And finally, in a file: my $fp = FileParser->new('some/dir'); while ($fp->next_parseable_file) { my $data = $fp->get_data; }

This seems super convenient. If I'm not using an iterator, I can just pass a file to the get_data() method.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re^15: How to completely destroy class attributes with Test::Most?
by jcb (Parson) on Aug 29, 2019 at 01:34 UTC

    With the "bundle" model and a slight modification, that last loop becomes:

    foreach my $data ($fp->bundle_parseable_files->all->get_data) { # do something with each $data }

    The AUTOLOAD in File::Collector::Iterator::All is slightly different:

    sub AUTOLOAD { our $AUTOLOAD; my $self = shift; my @result = (); if (defined wantarray) { push @result, $$self->$AUTOLOAD(@_) while ($$self->next_file) +} else { $$self->$AUTOLOAD(@_) while ($$self->next_file) } return wantarray ? @result : \@result; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2024-04-24 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found