I was surprised at this result when I tried to write a couple of lines of code that would prevent an email from going out to an active mailing list, unless I (as the developer) had expressly set "is_dev_mode" to be 0 or 1 in an anonymous hash supplied as the parameter. Without expressly telling the subroutine whether or not we're in development mode, I figured, the subroutine needed to assume that we are, so as not to actually send an email to all of my important customers. So:

send_email ( { subject => "Test subject", body => "Test body", } ); sub send_email { my $param = shift; my $subject = $param->{subject}; my $body = $param->{body}; my $is_dev_mode = exists $param->{is_dev_mode} ? $param->{is_dev_mode} : undef; $is_dev_mode = 1 unless ( $is_dev_mode == 0 || $is_dev_mode == 1 ) ; # In case the CGI param is ''; default is to be "safe" (is_de +v_mode == 1) _blast_email_to_all_my_important_customers( $subject, $body ) unles +s ( $is_dev_mode ); }

So I run the code, and... did all my important customers just get an embarrassing test email?

They sure did. Now, let's add "use strict;" and "use warnings;" and try it again, and throw in a warn statement to try to figure out what's happened.

use strict; use warnings; send_email ( { subject => "Test subject", body => "Test body", } ); sub send_email { my $param = shift; my $subject = $param->{subject}; my $body = $param->{body}; my $is_dev_mode = exists $param->{is_dev_mode} ? $param->{is_dev_mode} : undef; $is_dev_mode = 1 unless ( $is_dev_mode == 0 || $is_dev_mode == 1 ) ; # In case the CGI param is ''; default is to be "safe" (is_de +v_mode == 1) warn "is_dev_mode is '", $is_dev_mode, "'\n"; _blast_email_to_all_my_important_customers( $subject, $body ) unles +s ( $is_dev_mode ); }

Result:

Use of uninitialized value $is_dev_mode in numeric eq (==) at C:\Users +\davel\Desktop\test-of-dev-mode-flag-revised.pl line 19. Use of uninitialized value $is_dev_mode in warn at C:\Users\davel\Desk +top\test-of-dev-mode-flag-revised.pl line 23. is_dev_mode is ''

Thanks for the warning, but the customers just got ANOTHER email blast.

OK, I see how $is_dev_mode was undef, and hence the warning. But if $is_dev_mode is undef, then $is_dev_mode is not equal to zero, right? And if $is_dev_mode is undef, then $is_dev_mode is not equal to 1, right? And if neither is true, then the "unless it's true" clause means $is_dev_mode gets 1 as its value (so as to avoid the embarrassing blast to all my important customers), right? Nope! is_dev_mode is still undefined.

An interesting and surprising result to me. I don't think I'll be setting variables to undef and using them in this kind of boolean test again.

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. Now, in hindsight of course, it's apparent that the "==" operator is going to convert both sides to a number in order to chug along and do the best it can, and 0 seems to be the closest thing to a number for undef. I think.


In reply to 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.