Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Undefined undef oddness

by Cybercosis (Monk)
on Jul 24, 2001 at 00:39 UTC ( #99161=perlquestion: print w/replies, xml ) Need Help??

Cybercosis has asked for the wisdom of the Perl Monks concerning the following question:

I won't get into the train of thought that led to the following thought experiment:

$foo = "NARGLE"; print ref($foo), "\n"; defined(ref($foo)) ? print "Y\n" : print "N\n"; ref($foo) == undef() ? print "Y\n" : print "N\n";

The print ref() obviously prints nothing but a newline. However, I was quite surprised when the two trinary statements both printed "Y". I went back to the documentation and looked up both defined() and undef(). Defined "Returns a boolean value telling whether EXPR has a value other than the undefined value 'undef'...". Looking over at the undef docs, it "always returns the undefined value". How is it possible for ref($foo) to be both defined and == undef?


nemo accipere quod non merere

Replies are listed 'Best First'.
Re: Undefined undef oddness
by japhy (Canon) on Jul 24, 2001 at 00:42 UTC
    You can only test for undefinedness via defined(). If you use undef with == or eq (or any other operator like them), it gets converted to 0 or the empty string.

    Jeff japhy Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Undefined undef oddness
by PrakashK (Pilgrim) on Jul 24, 2001 at 00:59 UTC
    According to, "perldoc -f ref", ref returns a true value if EXPR is a reference, false otherwise. It does not say undef is returned.

    A defined test on a "false" value returns true, since even a "false" value is defined.

    The following snippet:

    print defined 0 ? 'Y' : 'N';
    prints 'Y'.
Re: Undefined undef oddness
by I0 (Priest) on Jul 24, 2001 at 01:09 UTC
    0 == 0

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://99161]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2022-11-29 04:54 GMT
Find Nodes?
    Voting Booth?