my @holdem; my %dispatch = ( one => \&subone, two => \&subtwo ); my $target; open( $pipe, "-|", "ls" ); while ( <$pipe> ) { if ( $target ) { $target->( $_ ); } else { push @holdem, $_; my $decision = look_for_evidence( $_ ); if ( $decision =~ /one|two/ ) { $target = $dispatch{$decision}; $target->( $_ ) for ( @holdem ); } } }