in reply to Parsing output from a 'system' call into objects?
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
|
|---|