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

tmcd at panix.com tmcd at panix.com
Tue Oct 17 22:03:03 PDT 2006


On Tue, 17 Oct 2006, Wayne Walker <wwalker at bybent.com> wrote:
> if ( (! defined $product) || ($product == undef))

The second one has several problems.
- If you use "perl -w", and you should, it makes sure that undefined
  values don't get used.  So the above leads to
      Use of uninitialized value in numeric eq (==) at ...
- Since "==" is a numeric comparison, undef is converted to 0.  So
      $product == undef
  is exactly identical to
      $product == 0

I use @array to find the number of elements (hence whether $ARGV[EXPR]
was provided or not), defined($var) to find whether a variable is
undef or not, and exists($table{KEY}) to find whether an element of a
table is defined.  As I mentioned, you shouldn't use a variable in a
value context ($i + 3) unless you know that it's defined.

-- 
Tim McDaniel; Reply-To: tmcd at panix.com


More information about the Austin mailing list