Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Check Variables for NULL

by Trihedralguy (Pilgrim)
on Feb 07, 2007 at 20:55 UTC ( #598879=perlquestion: print w/replies, xml ) Need Help??
Trihedralguy has asked for the wisdom of the Perl Monks concerning the following question:

I was wondering how I go about doing a simple if statement like this:

if ($value == "blank") { Do this } else { Do that };

Where blank would be how to check if the variable has nothing in it. I tried doing NULL, 'NULL' and '', but that doesnt work?

Edit: g0n - code tags

Replies are listed 'Best First'.
Re: Check Variables for NULL
by Tanktalus (Canon) on Feb 07, 2007 at 20:58 UTC

    What is this NULL you speak of? Perhaps you have the wrong language? There is no NULL in Perl...

    You probably want undef. That's the Perl equivalent of C/C++'s NULL. Check out defined.

    if (defined $value) { Do that } else { Do this }

    You can use not to reverse that.

      Actually C doesn't have NULL either. It just has various (sometimes incompatible) variations on #define NULL 0. However 0 as a value for pointers in C is magical in a sort of similar fashion to the magic represented by undef in Perl.

      DWIM is Perl's answer to Gödel

        Check the C standard. It has a NULL. How it's implemented is implementation defined, which means it could be anything. Section 7.1.6 of the 1990 standard (yes, there's a 1999 standard - I don't have a copy of that) includes the text:

        The macros are

        which expands to an implementation-defined null pointer constant
        You're confusing C with C++ which defines NULL to be equivalent to the integer constant 0. Normally in C, the define is actually (void*)0.

Re: Check Variables for NULL
by davorg (Chancellor) on Feb 08, 2007 at 09:16 UTC

    Perl tries to make things as easy as possible for you and this is a good example.

    In most cases, what you actually want is as simple as:

    if ($value) { # $value contains something useful } else { # $value is "empty" }

    (Note that the logic is reversed from your original example)

    What we're doing here is checking the "truth" of $value. $value will be "false" if it contains any of the following values:

    • "undef" (i.e. if it hasn't been given a value)
    • The number 0
    • The string "0"
    • The empty string ("")

    Any other value in $value will evaluate to "true".

    This is all you need in most cases. There are a couple of "corner cases" that you sometimes have to consider:

    • Sometimes 0 is an acceptable value. You account for that by using if ($value or $value == 0).
    • Sonetimes you might want to test that $value contains nothing but whitespace characters. The easiest way to do that is by checking for the presence of non-whitespace characters with if ($value =~ /\S/).
      Sometimes 0 is an acceptable value. You account for that by using if ($value or $value == 0).

      This may be more inclusive than you wish. (it will be true for both undef and the empty string), as it's basically asking for everything that's true or everyhing that's false. You likely wanted eq instead of == :

      ($value or $value eq 0)
Re: Check Variables for NULL
by Trihedralguy (Pilgrim) on Feb 07, 2007 at 21:13 UTC
    sorry I didnt give enough information apparently.

    Basically i perl script that pulls in data from the user. But I'm trying to write and if statement that will say something like "Hey, you didnt put any data in that box!" So I want to check if the $value is "0" or "blank" give the error, if it has data run the rest of the script.
      Well the answer depends on how you get the value from the "box". Most UI components return empty input as an empty string, but some might return undef instead.

      # let's say you've got the user input in $input.. if (defined($input) && $input ne "") { # input is defined and not empty } else { print "input wasn't filled in\n"; }
      Note that compare strings with eq and ne instead of == and != since == and != coerce their arguments to numbers.

      See equality operators and defined in the documentation.

        Your awesome!!
        Thanks so much for your help, for some reason I just forgot about "EQ and NE"
Re: Check Variables for NULL
by Moron (Curate) on Feb 08, 2007 at 14:35 UTC
    The worst-case similar scenario I have encountered is where 0, '', undef(), "NULL", "null", "N/A" and "NA" all had to evaluate false and just about everything else as true. Commenting out my extra NA requirement would produce something like:
    if ( True( $value ) ) { } else { } sub True { defined $_[0] and $_[0] and ( uc( $_[0] ) ne "NULL" ) # and $_[0] !~ /^N\/?A$/ ; }


    Free your mind

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://598879]
Approved by Joost
[Discipulus]: April: the Perl Tk official month..

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2018-04-24 07:17 GMT
Find Nodes?
    Voting Booth?