in reply to Check Variables for NULL

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:

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:

Replies are listed 'Best First'.
Re^2: Check Variables for NULL
by jhourcle (Prior) on Feb 08, 2007 at 11:40 UTC
    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)