Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Parsing output from a 'system' call into objects?

by NetWallah (Canon)
on Feb 22, 2017 at 18:55 UTC ( [id://1182550]=note: print w/replies, xml ) Need Help??


in reply to Parsing output from a 'system' call into objects?

I'm late to this party, as usual, but since the OP mentioned "Objects" - here is a working OO version:
use strict; use warnings; BEGIN{ package Company; my (%by_id, %by_type); sub new{ my ($class,$coid,$type,$state,$id) = @_; my $self = bless {COID=>$coid,TYPE=>$type,STATE=>$state,ID=>$id}, + $class; $by_id{$id} = $self; push @{$by_type{$type}}, $self; return $self; } sub All_ID_callback{ # Class method my ($callback) = @_; for (sort keys %by_id){ $callback->($by_id{$_}, $_); } } sub get_id{ # CLASS method my ($id) = @_; return $by_id{$id}; } sub for_each_bytype_callback{ # CLASS method my ($type, $callback) = @_; for my $o (@{$by_type{$type}}){ $callback->($o); } } sub PRINT{ my ($self, $header) = @_; $header and print "$header\n"; print ' ID = ', $self->{ID}."\n"; print ' CoID = ', $self->{COID}."\n"; print ' State = ', $self->{STATE}."\n"; } } #--- Main ---- #my $testout=`$test list`; my $testout=' CoID: Type: State: ID: ExampleCo A former 97546 ExampleCo B pending 48541 ExampleCo A ready 75521 ExampleCo B former 50123 ExampleCo A contact 60047 ExampleCo B contact 19425 '; for my $l (split("\n",$testout)) { chomp $l; my $company = Company::->new(split(' ',$l,4)); } print "---In ID sequence---\n"; Company::All_ID_callback( ## In ID sequence sub{ my $o=shift; return unless $o->{TYPE} eq "A"; $o->PRINT(); }); print "---Selecting by type---\n"; Company::for_each_bytype_callback("A", sub{my $o=shift; $o->PRINT(); } );

        ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-16 09:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found