For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > December 2006 > NULL and UNDEF









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author NULL and UNDEF
John

2006-12-22, 8:02 am

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ürgen Exner

2006-12-22, 8:02 am

John wrote:
> 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


Paul Lalli

2006-12-22, 8:02 am

J=FCrgen Exner wrote:
> John wrote:
>
> 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 =3D 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 =3D 0;
if ($x eq undef) {
print "Yes";
} else {
print "No";
}
'
Use of uninitialized value in string eq at -e line 3.
No


Paul Lalli

Paul Lalli

2006-12-22, 7:03 pm

John wrote:
> 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) { ... }

Robert 'phaylon' Sedlacek

2006-12-22, 7:03 pm

Paul Lalli <mritty@gmail.com> wrote
> Check for null byte:
> if ($x eq '\0') { ... }


Sure you didn't mean C<"\0"> instead of C<'\0'>?

> perl -wle"print '\0'"

\0

gr.,
Robert

--
The mind is its own place, and in itself,
Can make a heaven of hell, a hell of heaven.
-- John Milton, Paradise Lost
Paul Lalli

2006-12-22, 7:03 pm

Robert 'phaylon' Sedlacek wrote:
> Paul Lalli <mritty@gmail.com> wrote
>
> Sure you didn't mean C<"\0"> instead of C<'\0'>?
>
> \0


Whoops! Thanks for the correction!

Paul Lalli

John

2006-12-23, 4:40 am


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



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com