Most of the time, the way perl works will just make your work easier, and Do What You Mean. For exemple, in C, if you have an int with value 42, and the string "42", you have to convert either one of them to the other format. The == operator (and all numeric operators) makes it simpler by implicitly converting both sides to numbers, this way you don't have to care if it was user input, extracted from a string, the output of an operation on other numbers or another source, you can just do 42 > "40" and perl will understand what you mean and compare 42 and 40.

Perl is good at guessing most of the time, but it's not psychic, so when you try undef == 0, it will use the implicit meaning of undef ("nothing"), and compare "nothing" to 0, and tell you that's the same thing. That's why you have to write explicitly that you want to know wether a variable is defined or not.


In reply to Re: why can't compare a variable with undef directly? by Eily
in thread why can't compare a variable with undef directly? by Anonymous Monk

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.