Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Little annoying mistakes ... of others

by tprocter (Sexton)
on Dec 06, 2008 at 19:41 UTC ( #728591=note: print w/replies, xml ) Need Help??


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 (Sage) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://728591]
help
Chatterbox?
shmem doesn't have mp3info, but mp3info2 - which is a perl script ;-)
[marioroy]: my @music_times = qx(find \$HOME/Music/ Albums/ -type f -iname '*.mp3' -exec mp3info -p "%S\\\n" {} \\;);
[marioroy]: might need 2 or 3 backslash for \n. Also \\;
[Lady_Aleena]: shmem, there is MP3::Info/
[Discipulus]: too much complex qx when you know perl are better to avoid
[marioroy]: might need to backslash \'*.mp3\'
[marioroy]: It's a quoting issue inside qx. It's File::Find or qx. LA chooses qx. Almost there.
[marioroy]: Also \"%S\\\n\" oh my, not fun.
[Discipulus]: good night folks
[marioroy]: I will make a mp3info script that takes the -p "%S\n" args and test qx. I'm on the road, so will come back.

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2017-04-23 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (432 votes). Check out past polls.