So your problem is ...

DB<1> p undef == 0 1 DB<2>

correct???

Perl does automatic type-casting² and emits warnings at best.

That's why you can do things like

my $count; $count++; print "Count is $count"

(undef->integer->string)

If you want to test against undef use operators like defined or //

edit

> I would not have thought that "( $an_undefined_variable == 0 )" would evaluate to true because, golly, undef isn't a number and so the "==" operator is testing whether one number is the same as another number.

Perl is not C, types are dynamic. That's why Perl has so many more operators than other languages like + and . where others only have + °

BTW: JS has the worst of both worlds in this respect, types are dynamic but operators are spare. I.e. sometimes 1 + 1 means 11 there.

And in Python 3/2 results in 1 , because you divided two integers! (IIRC they adjusted something in Py3 but I think it's a new operator // )

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

update

°) Perl has also two equality operators for numeric == and string eq

That's why

DB<1> p undef eq "" 1 DB<2> p undef eq "0" DB<3>

²) i.e. internally. Externally do all scalars have a well defined numeric and string value.


In reply to Re: Surprising result when using undef by LanX
in thread Surprising result when using undef by davebaker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.