in reply to Testing <> for undefined

*fixing my previous post:

You could also do something like that:
# Test if the is an argument if(!@ARGV) { die "My help message\n"; } # Test if the argument is a file if(! -e $ARGV[0]) { print "File $ARGV0 does not exist\n"; }
's -DBC

Replies are listed 'Best First'.
Re^2: Testing <> for undefined
by BbTrumpet (Acolyte) on Jul 08, 2004 at 16:41 UTC
    I have done this in the past, and it seems to work fine:
    my $file = $ARGV[0] ? $ARGV[0] : die "USAGE:  prog.pl file_pathname\n";
    open(FILE,"$file") or die "Unable to open file $file\n";
      I have to say that I hate your use of the trinary operator there. You're expressing that you want to assign die's result to $file. Instead:
      my $file = $ARGV[0] or die "Usage: prog.pl file_pathname\n";
      (as a bonus, you don't have the redundant mention of $ARGV[0]).
      Better, if the filename might be zero:
      @ARGV or die "Usage: prog.pl file_pathname\n"; my $file = $ARGV[0];

      We're not really tightening our belts, it just feels that way because we're getting fatter.