[Linux-disciples] Opening the wrong file

Stephen R Laniel linux-disciples@bostoncoop.net
Thu, 13 Nov 2003 10:56:39 -0500


I just noticed a bug in a little Apache-log post-processing script that I
wrote. If the user specifies an input file on the command line
('log [filename]'), the script opens that input file; otherwise the
script defaults to opening /var/log/apache/access.log. The relevant chunk
of code is

my $logfile = (@ARGV ? $ARGV[0] : '/var/log/apache/access.log');
#[...]
open( STDIN, "<$logfile" )
	or die "Could not open $logfile.\n";

The trouble I just noticed is that if I try to use syntax like

zcat /var/log/apache/access.log.10.gz |~/log

it doesn't work: the script doesn't recognize that a file has been piped
in, so it uses /var/log/apache/access.log.

Is there a way to fix this? The logic I want is

* if a file's been piped in, use that.
* if a file's been specified on the command line, use that.
* if both a pipe and a file have been specified ... figure out this
  case at some other time
* if neither a pipe nor a file have been specified, use
  /var/log/apache/access.log.

Is this easy to do?

-- 
``As Dick and Jane lay down in the back of Dick's sport utility
  vehicle, Spot ate the spicy stew. He wanted to help. Spot knew Jane was
  a racist who saw the stew as a symbol of Dick's ethnicity. He also knew
  it added to her deep-seated feelings of shame.''
 -http://www.mcsweeneys.net/2003/11/07kennedy.html/