http://qs321.pair.com?node_id=728591


in reply to Little annoying mistakes ... of others

As part of my job, I review code by people that are less then experienced with Perl. I've seen some bad code such as:
open(F, "grep 'WARNING<\/font>' $mydir/$myfile".".|wc|awk '{print \$1} +'| ") ; while (<F>) { chomp(); $received_warn=$_; } close F;

Which is part of a larger script where the same file was opened and closed several times and processed as shown by scanning the entire file, looking for specific html, then counting rows in the output file for the condition. Meanwhile, all of the information needed for all of the processing was available from a single pass of the file head.

However, I think the most common mistake people make is confusing numeric comparisons with string comparisons. This kind of mistake often passes simple manual tests but breaks if it gets to production.

Example:
$a = 'a'; if ($a == 'b' or $a == 'c') { print "True\n"; }
...or even worse:
if ($a = 'b') { print "True\n"; }
Both examples print 'True', but the last example has corrupted your data.

Replies are listed 'Best First'.
Re^2: Little annoying mistakes ... of others
by GrandFather (Saint) on Dec 06, 2008 at 21:08 UTC

    It looks to me like the thing most of the people you are complaining about forget to always use strictures (use strict; use warnings; - see The strictures, according to Seuss).

    They should also learn not to use $a and $b in general use ($foo and $bar are good for example code).

    Learning to use the three parameter form of open, lexical variables for file handles and checking the result of the open would seem to be a higher level of sophistication. However using comprehensible and consistent indentation should be attainable for them, even if just by using Perl tidy.


    Perl's payment curve coincides with its learning curve.