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

Hi Monks, Can anyone tell me if the following 2 statements would be equivalent as far as checking that a variable is not null.

next unless ! $ex_date; next unless !defined($ex_date);

Basically, i would like to go to the next record if the variable $ex_date is null. any help is much appreciated. thanks Tony

Replies are listed 'Best First'.
Re: Checking Variable for Not Null
by hippo (Archbishop) on May 27, 2015 at 15:00 UTC

    You haven't said what you understand by "null" in this context. "null" is not a concept in Perl. We have defined and we have true. eg:

    ValueTrue?Defined?
    undefNoNo
    0NoYes
    ''NoYes
    1YesYes
    'TIMTOWTDI'YesYes

    BTW, do you really find unless ! to be clearer than if?

      Good point Hippo. If is much clearer.

Re: Checking Variable for Not Null
by Athanasius (Archbishop) on May 27, 2015 at 15:05 UTC

    Hello dirtdog,

    First, you have the logic back-to-front; you want:

    next if !$ex_date; next if !defined($ex_date);

    or

    next unless ex_date; next unless defined($ex_date);

    Second, the two statements are not equivalent. The first will skip to the next record if $ex_date has the value of 0 (zero), "" (the null string), or undef. See perlsyn#Truth-and-Falsehood. The second statement will skip only if $ex_date is undef.

    Which form to use depends on how you define “null.” If by null you mean false, then use the first form; but if you mean undefined, then the second form is the one you want. Without more information, it’s difficult to guess which meaning of “null” best suits your needs; but the second interpretation (null means undef) is usually the safer option.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Hi, what i mean by null in this case is i'm checking a date position in a file (for example position 140-148). If there is no date passed:

      $ex_date=substr($_,140,8);$ex_date =~ s/\s+$//;

      then i would want to skip to the next record.

        In that case, your “null” corresponds to the empty string, so you can test for true or false (because the empty string is “false” in Perl):

        next if !$ex_date;

        But it’s better (because clearer) to make the test explicit:

        next if length $ex_date == 0; # or next if $ex_date eq "";

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Checking Variable for Not Null
by GotToBTru (Prior) on May 27, 2015 at 14:52 UTC

    No. Set $ex_date to 0 as an example.

    Update: make sure you are consistent about the meaning of 'null', because Perl doesn't know what that is. Is null undef? Empty string? 0?

    Dum Spiro Spero
Re: Checking Variable for Not Null
by t_rex_joe (Sexton) on May 27, 2015 at 16:21 UTC
    Tony, This is what I use for detecting if a variable is null or empty.
    if(!(defined $ex_date) || ($ex_date eq '')) { print "EX_DATE IS UNDEF. NEXT L: \"" . __LINE__ . "\"\n"; next; }
    Joe
Re: Checking Variable for Not Null
by locked_user sundialsvc4 (Abbot) on May 27, 2015 at 15:39 UTC

    Another way to say it, is to say that the first example tests for “truthiness.”   As others have said, there are many possible values for $ex_date which are “false-y” but which are not what you are (intending to be ...) testing for.

    And this leads to the very worst kind of bug:   the “it seems to work ...” bug.

    Therefore, even though you are happily dealing with a “DWIM = Do What I Mean” language, you must sometimes be careful to “SWIM = Say What I (Really ...) Mean.”   This is one of those occasions.

    And this, of course, is also in-keeping with a distinction that the SQL language is very careful to draw:   “NULL is not False.”   NULL is:   “the absence of any value at all, therefore neither True nor False.”   So, in your application logic, you should take careful pains to draw the same distinction.   Test for undef (NULL) “first, and explicitly.”   Test for “truthiness” (or better yet, for an explicit value ...) only after you have established that the value is not NULL.   Then, throw an exception (die()) if you encounter “none of the above.”   When debugging-time comes, you (or your successors) will be glad that you “went to all that ‘extra’ trouble.”