John said:
There is no such thing as "NULL" in Perl. The string containing no
characters is referred to as either the empty string or the null
string. The character with ASCII code 0 is referred to as the null
byte. A pattern match which does not look for any characters is set to
be a null pattern. But "NULL" by itself is meaningless.
Is it the case, in Perl, that both the following
if ($x eq undef) and if ($x eq '')
will give identical results?
Functionally, yes. The eq operator imposes string context on both of
its operands. The undefined value is treated as the empty string in
string context, so the above are functionally equivalent(*). However,
with warnings enabled, the former will give a warning about using undef
in a string context for at least the undef constant. (If $x actually
*is* undefined, it too will produce a warning, but that's the same with
both expressions).
If so, how can you tell if the $x is actually null or undefined?
Again, no such thing as "null".
Check for undefined:
if (!defined $x) { ... }
Check for empty string:
if ($x eq '') { ... }
Check for null byte:
if ($x eq '\0') { ... }
Paul Lalli
(*) Since undef in a numeric context is treated as 0, it also follows
that these two will produce the same results, with the same caveat
about the warning:
if ($x == undef) { ... }
if ($x == 0) { ... }