The != operator performs a numeric comparison. Perl has built-in magic that gives scalars both numeric and string interpretations. It does the best it can at providing useful defaults when a veriable doesn't have a useful numeric equivalent.

'Hi' defaults to the number zero.

undef also defaults to zero.

When you use != against undef you are doing a numeric comparison against zero. Of your variables, only 1 is not numerically equal to zero.

You could try string comparison, eq, but that won't solve your problem either. As a string undef is equivalent to the empty string.

Using defined does exactly what you want.

Using defined also indicates that you intentionally dealing with the case where a variable might not be initialized. Uninitialized variables are an endless source of bugs. That us why you should have warnings turned on. Similarly, use strict; helps prevent a lot of other bugs.

Your program spits out warnings, if warnings are turned on

#!/usr/bin/perl use warnings; my($str,$empty_str,$number,$number_zero)=('Hi','',1,0); if($str!=undef){print"\$str is defined\n"} if($empty_str!=undef){print"\$empty_str is defined\n"} if($number!=undef){print"\$number is defined\n"} if($number_zero!=undef){print"\$number_zero is defined\n"}

Output:

Use of uninitialized value in numeric ne (!=) at ./delme.perl line 7. Argument "Hi" isn't numeric in numeric ne (!=) at ./delme.perl line 7. Use of uninitialized value in numeric ne (!=) at ./delme.perl line 8. Argument "" isn't numeric in numeric ne (!=) at ./delme.perl line 8. Use of uninitialized value in numeric ne (!=) at ./delme.perl line 9. $number is defined Use of uninitialized value in numeric ne (!=) at ./delme.perl line 10.

In reply to Re: why direct undef comparison isn't working by wazat
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.