seedstitch has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks:

This seems so simple, yet I've not found a clue in the O'Reilly book nor an extensive library of scripts at my disposal:

I need to process either a stream of millions of data points or a single atom with the same script:

> cat stream | foo

> foo bar (where bar is a single element form the stream)

I'm using the following convenient method to processing a stream from STDIN:

while (<>){
process stuff---
}
I tried this hack:

if ($ARGV[0]) {$inline = $ARGV[0]}

do{
process stuff--
}while ($inline = <>);

Course, now perl thinks $ARGV[0] is a file for input, and if I delete @ARGV, perl now waits patiently on STDIN.

I need to be able to disable STDIN when an invocation argument is suppplied. Is that possible?

thanks!

Replies are listed 'Best First'.
Re: Stream and Atom flexibility
by hipowls (Curate) on Mar 15, 2008 at 01:29 UTC

    You could do something like

    if (@ARGV) { process($_) for @ARGV; } else { while (<>) { process($_); } }
    where sub process does the real work.