Click here to get back home

not defined v. eq undef

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
not defined v. eq undef John 01-25-2008
Posted by John on January 25, 2008, 4:19 am
Please log in for more thread options
Hi

Are these the same?

if ($x eq undef) {print "hi"}
if (not defined $x) {print "hi}

If so, which is preferred?

Thank you
John


Note: I have tried posting to perl beginners (the obvious choice for a
simple question) but the posts do not appear.




Posted by A. Sinan Unur on January 25, 2008, 5:56 am
Please log in for more thread options

> Hi

use strict;
use warnings;

> Are these the same?

No.

> if ($x eq undef) {print "hi"}
> if (not defined $x) {print "hi}

Can't find string terminator '"' anywhere before EOF at C:\Temp\t.pl
line 2.

> If so, which is preferred?

Testing equality with undef will generate a warning regardless of the
value of $x.

unless ( defined $x )

or

if ( not defined $x )

would not. They would also convey the intended meaning better.

> Note: I have tried posting to perl beginners (the obvious choice for a
> simple question) but the posts do not appear.

You can ask all Perl related questions here. However, you should read
and follow the posting guidelines to get the best help.

Sinan

--
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>


Posted by Ben Morrow on January 25, 2008, 6:46 am
Please log in for more thread options

>
> > if ($x eq undef) {print "hi"}
> > if (not defined $x) {print "hi}
>
> Testing equality with undef will generate a warning regardless of the
> value of $x.

More importantly, it doesn't test that $x is undef. ($x eq undef) is
equivalent to ($x eq ''), modulo warnings.

Ben


Posted by John W. Krahn on January 25, 2008, 6:12 am
Please log in for more thread options
John wrote:
>
> Are these the same?
>
> if ($x eq undef) {print "hi"}
> if (not defined $x) {print "hi}

No. ($x eq undef) has no valid meaning in perl.


> Note: I have tried posting to perl beginners (the obvious choice for a
> simple question) but the posts do not appear.

Have you subscribed?

http://lists.cpan.org/showlist.cgi?name=beginners



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by brian d foy on January 25, 2008, 6:34 am
Please log in for more thread options

> Hi
>
> Are these the same?
>
> if ($x eq undef) {print "hi"}
> if (not defined $x) {print "hi}
>
> If so, which is preferred?

The trick is to show what you intend to do. Since there is a builtin to
check if something is defined, use the builtin. :)

Similar ThreadsPosted
short form for (defined $x and defined $y and ... )? March 25, 2006, 12:14 am
return and undef October 4, 2005, 8:35 am
NULL and UNDEF December 22, 2006, 6:41 am
$hash($key) = undef vs = 1 August 3, 2007, 11:22 am
Where is EOM defined? August 16, 2005, 1:01 pm
undef takes forever October 12, 2004, 8:20 pm
CGI::uploadInfo returning undef? April 18, 2005, 1:00 pm
Checking for undef after performing get() January 9, 2006, 5:01 pm
perl -pi with INPUT_RECORD_SEPARATOR=undef (bug?) May 7, 2006, 4:51 pm
undef in Hash of Arrays June 25, 2006, 2:07 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap