SPUG: "Boolean" return values
Colin Meyer
cmeyer at helvella.org
Mon Oct 13 16:06:10 PDT 2008
On Mon, Oct 13, 2008 at 03:58:22PM -0700, craig at seaperl.com wrote:
> The reason that I've always heard was that in an IF condition you could do
> TRUE if the condition returned a positive number and would be FALSE if it
> was a numeric 0 or a string. Perl does that sort of conversion so that if
> you have a string of "345" that it could be evaluated as a NUMERIC 345 and
> counting as TRUE but that something like "345r2" would be evaluated as a
> string and be considered FALSE.
That's not quite true:
perl -le'print "345r2" ? "true" : "false"'
perldoc perlsyn
Truth and Falsehood
The number 0, the strings '0' and '', the empty list "()", and "undef" are all false in a boolean context. All
other values are true. Negation of a true value by "!" or "not" returns a special false value. When evaluated
as a string it is treated as '', but as a number, it is treated as 0.
-Colin.
More information about the spug-list
mailing list