Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Testing <> for undefined

by danielcid (Scribe)
on Jul 08, 2004 at 15:59 UTC ( [id://372842]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://372842]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found