[SLL] Perl question
Ken Meyer
kmeyer at blarg.net
Wed Mar 19 04:57:14 EST 2003
But the test fails if the argument is "0", and also if it is "2", which is
kind of pernicious.
In the first case, the argument "0", which is an argument that starts with a
digit and should return the argument name, instead causes the "and" test to
fail ("and" is lower precedence than the match binding operator "=~")
In the second case, the test works, but the argument passed in "$maxfail" is
indistinguishable from the error condition.
I think...
Ken Meyer -- a new student of the arcane
-----Original Message-----
From: linux-list-admin at ssc.com [mailto:linux-list-admin at ssc.com]On
Behalf Of Creede Lambard
Sent: Tuesday, March 18, 2003 6:01 PM
To: Phil Mocek
Cc: linux-list at ssc.com
Subject: Re: [SLL] perl question
On Tue, 2003-03-18 at 17:36, Phil Mocek wrote:
> Note that the code provided by Paul does not check whether $ARGV[0]
> is defined. It checks whether $ARGV[0] evaluates to true. If
> $ARGV[0] is defined as 0 (zero), it will evaluate to false, and
> $maxfail will be set to the default value of 2.
Actually, it does. If you don't supply an argument to the script,
$ARGV[0] is undefined. Undefined variables in Perl always evaluate to
"false." So in this expression:
if ( $ARGV[0] and $ARGV[0] =~ /^\d+/ )
if nothing is passed to the script the value of $ARGV[0] is the special
Perl value "undef", which evaluates to "false". That short-circuits the
"and" expression, which stops evaluating and passes control to the
"else" section of the "if" expression.
I will agree that it doesn't specifically check whether $ARGV[0] is
defined, and if I were writing this code I would add the check for
"defined $ARGV[0]" as you did, just to make it clear that that's what I
was checking for.
--
Creede Lambard <creede at penguinsinthenight.com>
More information about the linux-list
mailing list