Reading from pipe or from stdin, how?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Hello.  I have a program where you specify the name of the file you
want to read from command line.  But, I want to modify it so that if
no file is specified, it reads from stdin.  so here's a code snippet:

my $file = "";

# Parse command line to get file option.
open( FILE, "<", $file) or die "Could not open $file\n";

my $line;

while( ($file && $line = <FILE> ) ||
       (!$file && $line = <STDIN>)) {

This doesn't seem to work:

Unix> echo "blah" | ./pxmlgrep
Value of <HANDLE> construct can be "0"; test with defined() at ./
pxmlgrep line 233.
Can't modify logical and (&&) in scalar assignment at ./pxmlgrep line
234, near "<FILE> )
Execution of ./pxmlgrep aborted due to compilation errors.

Any suggestions?


Site Timeline