APM: Next dumb Q: IF NULL????

Bob Apthorpe apthorpe+pm at cynistar.net
Tue Oct 17 19:37:23 PDT 2006


Hi,

CaptNemo wrote:
> Why is it so hard to check for null values in perl!?!?!

One issue is that perl doesn't have a prominent NULL value as do C or
SQL, so it doesn't have the same tests for NULLness. As people have
pointed out, perl does have the notion of defined()ness which is
probably what you want. Also, this is complicated by the notion of
existence when dealing with associative arrays (hashes) - does a certain
key exist within a hash, and (if so) is the value associated with that
key defined?

Add to this the 'truthiness' of values in conditionals:

perl -e 'print "True!\n" if ("");'
                perl -e 'print "True!\n" if (0);'
                                perl -e 'print "True!\n" if ();'
                                                 perl -e 'print
"True!\n" if ("0");'

all print nothing which may or may not be what you want.

However,

perl -e 'print "True!\n" if (NULL);'

will print "True!" since NULL is considered a (bareword) string literal.

The -w flag or "use warnings;" will grumble about that syntax which is
one reason to enable warnings unless you have a really good reason not to.

Perl also has a bunch of conditional operators that also may not be
doing what you expect.

> 1) checking is a variable is empty:
> 
> 	IF ($product eq "")...   # String comparison
> 	IF ($product <> "")...   # <> is the null filehandle - this really isn't what you want!
> 	IF ($product ne "")...   # String comparison
> 	IF ($product != "")...   # Numerical comparison
> [...]

All of these are reasonable tries, especially if you have experience in
multiple languages. Still, if you find yourself "fumbling with your
keys" (one of these *has* to work!), that might be a sign you need to
step back and read the docs closely, though you do get points for both
experimentation *and* asking for help. :)

BTW, <> is really useful but not as a conditional operator.

> 2) checking to see if command line args were present:

This fragment may help:

----
    use strict;
    use warnings;

    use Getopt::Long;

    my %options = ();
    my @optlst = qw( debug verbose help usage owner version );

    ### map options to variables
    for my $kk (@optlst) {
        my ($nkk, $junk) = split(/[=!]/, $kk, 2);
        $options{$nkk} = '' if ($nkk);
    }

    GetOptions(\%options, @optlst);
----

This puts option values in the %options hash. For example, you'd check
if --debug was set by checking:

    if ($option{'debug'}) {
        debug_something();
    }

It takes a while to find and understand time-saving modules like
Getopt::Std or Getopt::Long and to get used to perl's idioms for
defined()ness or NULLness.

The larger-picture question may be "what command-line options have been
set?", rather than "what's in ARGV?" Or maybe not.

It helps to read other people's code whether you're just learning or
have been using a language for a while. For example, you can tell when
someone is trying to code C in Perl (using low-level constructs and a
lot of code to do something fairly simple, especially reading from a file.)

It's hard to break habits that work successfully in other languages so
when I'm trying to sort out how to do something in PHP or Ruby, I have
to keep reminding myself to look at the language I'm using, not the
language I'm used to.

hth,

-- Bob


More information about the Austin mailing list