Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Common Beginner Mistakes

by Mabooka-Mabooka (Sexton)
on Apr 17, 2005 at 03:39 UTC ( [id://448568]=note: print w/replies, xml ) Need Help??


in reply to Common Beginner Mistakes

I think this article is an excellent *stub* for the theme:-). "Part one" sounds very encouraging though.
I'd be pleased to see a much bigger (and preferably categorized) list of common beginner's pitfalls. It would be only fare to point novice Perl developers to such list *before* they even start developing (maybe it'd force some not to start with Perl at all afterwords; but it's fair, isnt' it?..:-)). It's hard to overestimate a value of such a list, if it were comprehensive and systematical.

Please don't misinterpret me: although i don't normally use Perl for developing and it usually makes me very unhappy, I am not trying to start yet another "Horses Vs. Cows" war here:). My point is: all beginners make and will make tons of same mistakes; people should know what they are about to face and be aware that learning Perl to a dergee of not making these mistakes anymore is a comparatively big learning curve.

Out of the three mistakes described here in the article, I think only 1st one really belongs to the "Common *Perl* mistakes" category.
2) Repetition without Abstraction: it's a generic ugly coding practice problem; I don't see what it has to do with Perl. I mean, people who write like this will write like this in any language; and vice versa: people who don't in other langauage, won't in Perl either.
3) is common sense.

On the web, I also often see Perl idioms / general good practices mixed with "beginner's mistakes". Things like "Use "open or die" instead of "open"", "don't forget to print Content-type..." and xillions of others that have nothing to do with Perl.

But enough philosophyzing. I will now add a couple examples -- things that I stepped onto in my own practice -- as separate posts. That is, if I'm not moderated out of here...:-)

Replies are listed 'Best First'.
Re^2: Example 2: incrementing "something"
by Mabooka-Mabooka (Sexton) on Apr 17, 2005 at 04:35 UTC

    Perl allows to use arithmetics on string

    Here's the code:
    #!/usr/local/bin/perl -w use strict; sub step_over_desert # () I won't repeat this mistake anymore!!! { print "Tsock...\n"; } sub cover_the_distance # () I won't repeat this mistake anymore!!! { my ($me, $desert_width, $max_spit_distance, $num_legs, $last_name, + $day_phone, $days_without_water) = @_; my $num_steps=0; while($desert_width) { my $todo = $desert_width * $num_legs; print "$desert_width miles remaining; $todo steps yet to do.\n" +; step_over_desert(); $desert_width--; $num_steps += $num_legs; } print "Steps made: $num_steps\n"; } #cover_the_distance("strong animal", 5, 15, 4, "Mabooka", "(123)-456-7 +890", -1); cover_the_distance("strong animal", "five thousand miles", 15, 4, "Mab +ooka", "(123)-456-7890", -1); 1;

    Don't try it at home

    Well, I recommend at the very least to redirect the output to a file:-).

    Why people do this?

    Again, the main reason: because it allows you. One may expect either a compile-time error or a run-time exception. But Perl allows you to shoot your own foot.
    Actually, this is an obfuscated (starting to love the word) code from a real-life app. The real application would send a query over the wire to the server,
    get a responce line --
    -- that should look smth. like this:
    200 OK 578
    -- meaning " there're 578 lines yet to read from the socket,
    parse this line and create a loop:
    while($numlines) { get_next_one(...); $numlines--; ... }
    Well guess what? Once in 10000 times the server would get tired and send a garbled status line -- smth. like:
    200 OK whatever,man
    and
    (while "whatever,man"--) {....}
    will start the almost infinite loop. (Try the code above).

    Solution

    Be aware.
    use int($n).
    Ask your new hires during the inteview: "What is the result of the following operation:
    "Larry"--;
    -?.
Re^2: Example 1
by Mabooka-Mabooka (Sexton) on Apr 17, 2005 at 04:03 UTC

    sub()

    This one was the reason I came here, details are in the Seekers of Perl Wisdom thread. (THANKS again BUU!!!).
    In short: if you write
    sub foo() { ... },
    you're in trouble.

    Why people do this?

    Mainly (I think) because it's counter-intuitive to do otherwise. I personally automatically without thinking add "()" to a function declaration. Besides, I call it as "foo()".

    Another reasons: Perl allows you to do that. What I encountered is, depending on where one calls sush sub, -- before or after it's defInition(!!!), there'll be a syntax error or no errors. (In fact, after fixing my small problem today, I looked closer at my colleague's module and found several dozen subs declared like this. No complaints from Perl).

    Solution

    Read all books, spend several years practicing. I leave to the experts the explanation of what "sub name()" really means (it's an array of subs or smth. like that I guess).
    -- Find out.
Re^2:Example 3: expect the unexpected
by Mabooka-Mabooka (Sexton) on Apr 17, 2005 at 16:43 UTC

    One would believe it fails... it doesn't

    It tries to do the right thing. The result in general is unpredictable. (That is, for a beginner).
    The snippet is:
    #! /usr/local/bin/perl use strict; sub print_percentage { my ($total, $part) = @_; if (int($total) != 0){ printf ("It's about %.2f%%\n", $part/$total *100.); } else{ print "Bad usage: $total == 0!!! Cannot divide by '$total'.\n" +; } } print_percentage(100, 50); print_percentage("oops", 50); ;1
    One could write many variations on the theme:
    #! /usr/local/bin/perl use strict; if ("38" == 38){ print "hit the jackpot!\n"; } else { print "hit elsewhere....\n"; } ;1

    Why people do this?

    People do mistakes, and they are spoiled by getting used to compilers' help. Many who come to Perl from other languages are used to "it doesn't make any sense" compiler's error. Or run-time exceptions. They are NOT used to the machine "doing the right thing" for them (of course! what do you mean by doing the right thing?) And that's the problem for beginners.

    Solution

    Set up your mind not to guess what it'll do. Learn, find out, know.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://448568]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-20 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found