NULL and UNDEF

J

John

Hi

Is it the case, in Perl, that both the following

if ($x eq undef) and if ($x eq '')

will give identical results?

If so, how can you tell if the $x is actually null or undefined?

Regards
John
 
J

Jürgen Exner

John said:
Is it the case, in Perl, that both the following

if ($x eq undef)

Somehow I doubt that even works. I would expect perl to throw an error.
and if ($x eq '')

will give identical results?

What happened when you tried it?
If so, how can you tell if the $x is actually null or undefined?

perldoc -f defined
perldoc -f length (if you want an alternative to eq '')

jue
 
P

Paul Lalli

Jürgen Exner said:
Somehow I doubt that even works. I would expect perl to throw an error.

It "works" fine. No errors. Just warnings. At least one warning,
because your using undef in a string context. If $x actually is
undefined, then you're using another undef in string context, so you
get a second warning:

$ perl -wle'
my $x;
if ($x eq undef) {
print "Yes";
} else {
print "No";
}
'
Use of uninitialized value in string eq at -e line 3.
Use of uninitialized value in string eq at -e line 3.
Yes

$ perl -wle'
my $x = q{};
if ($x eq undef) {
print "Yes";
} else {
print "No";
}
'
Use of uninitialized value in string eq at -e line 3.
Yes

$ perl -wle'
my $x = 0;
if ($x eq undef) {
print "Yes";
} else {
print "No";
}
'
Use of uninitialized value in string eq at -e line 3.
No


Paul Lalli
 
P

Paul Lalli

John said:
Subject: NULL and UNDEF

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) { ... }
 
J

John

Hi

Many thanks for those replies. Much appreciated,
I guess I have fallen into the bad habit of using "eq undef".
So, check not defined, then empty string then null byte (with double
primes).
I've tried this on some empty XML <city/> and it works well.

Thanks again
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top