true
con la excepción de las
cadenas ""
y "0"
.
true
excepto el 0
.
true
.
undefined
es false
.
''
. El operador defined trabaja con cualquier
valor. En versiones previas de Perl se requería que el argumento fuera un
lvalue, una expresión que se pueda interpretar como la parte izquierda
de una asignación.
if (defined(0)) { ... } # TRUE, error en Perl 4 if (defined(()) { ... } # TRUE, error en Perl 4
Cita de Modern Perl:
The conditional directives —if, unless, and the ternary conditional operator— all evaluate an expression in boolean context.
As comparison operators such aseq
,==
,ne
, and!=
all produce boolean results when evaluated.
Empty hashes and arrays evaluate to false.
Perl 5 has no single true value, nor a single false value.
Any number that evaluates to 0 is false. This includes 0, 0.0, 0e0, 0x0, and so on.
The empty string (''
) and'0'
evaluate to false, but the strings '0.0', '0e0', and so on do not.
The idiom'0 but true'
evaluates to0
in numeric context but evaluates to true in boolean context, thanks to its string contents.
Both the empty
list and undef
evaluate to false.
Empty arrays and hashes return the number 0 in scalar context, so they evaluate to false in boolean context.
An array which contains a single element — even undef
—
evaluates to true in boolean context.
A hash which contains any elements — even a key and a value of undef — evaluates to true in boolean context.