Just another Perl shrine | |
PerlMonks |
Re: Code cleanup; how best to deal with: defined(%hash) is deprecated at... (defined--)by tye (Sage) |
on May 22, 2014 at 06:33 UTC ( [id://1087068]=note: print w/replies, xml ) | Need Help?? |
After reading a ton of Perl code (much of it written by co-workers), I decided that the use of defined and exists can often be nearly random. Much less frequently but much more seriously, I've seen several cases of failures (some of them even in Production) caused by code like:
So I've instituted some simple best practices:
In particular, there is no such thing as a useful, false reference (unless you are overloading in a way that could return a false value -- and overloading is very rare in our code). So don't use defined (nor exists nor // nor //=) with references. Somewhat related, for string-valued database columns, if the empty string doesn't have special meaning, then declare the column "not null default ''", so you don't have to deal with a random mix of NULL and '' values. Similar to not using exists, don't check for missing positional parameters by checking the value of 0+@_ unless "missing", "undef", and "false" all have distinct meanings (which is usually a bad idea). - tye
In Section
Seekers of Perl Wisdom
|
|