in reply to Can I seek in a command's piped output?
If you need to make a routing decision after reading several lines from a pipe, you'll need to hold everything you read in a scalar or array until you are able to make your decision, and then send the accumulated stuff, along with whatever follows, to the appropriate recipient.
Update: So for example, something like:
(not tested)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 ); } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Can I seek in a command's piped output?
by wardy3 (Scribe) on Feb 12, 2009 at 04:26 UTC | |
by fullermd (Vicar) on Feb 12, 2009 at 11:35 UTC | |
by ikegami (Patriarch) on Feb 12, 2009 at 16:01 UTC | |
by wardy3 (Scribe) on Feb 16, 2009 at 00:26 UTC | |
by graff (Chancellor) on Feb 12, 2009 at 15:18 UTC |