Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I was teaching Perl again this week, and as part of the class I usually end up doing some sort of live programming demonstration. I started doing that around the year 2000, and it is as dangerous now as it was then.

I figured that too many students thought that I wrote code by starting at the top of the file and typed until I got the end of the file. Some people may come up with the entire program in their heads and type it out, but I generally program an outline, then circle around each main part as I add new features. If I don't do that, I start with one piece then program from the inside out.

If I do this in front of a bunch of people by having my typing projected onto a large screen, everyone gets to see all the stupid mistakes and typos that I make. It's generally good that they get to see that so they realize that although I'm exuding confidence in just about everything I tell them, I'm also a regular guy who makes all the same mistakes everyone else does. An expert is simply someone who has made every mistake, after all.

This isn't a list of problems with Perl, complaints about Perl, pleas for help, or anything else that merits attention. It's just documentation of my sometimes stupidity. It's the little details that cause more problems then program flow or big design issues. Go figure.

s/// at the end of a map()
No matter how often I remind myself that s/// does not return the string, my fingers still type the code as if it does. What do I really get? A list of the return values of s///, which are either 1s or empty strings.
my @array = map { s/regex/replacement/ } @input;
my %hash = { }; or my $hash = ( );
Once I got use to anonymous references, I started using them more than the named variables. If I have a data structure, I'm probably going to pass it around so I might as well start its life as a reference. A mistake that I've been making a lot this month is using the wrong constructor: the anonymous hash constructor when I want a named hash, and the list constructor when I want an anonymous hash. It's no one's fault but my own, really, but I still do it frequently, it seems.

print Dumper( $hash );
Can you see what's wrong with that? You'll find out quickly when you try to run that program: I didn't use Data::Dumper. Sometimes I wish that Perl was smart enough to figure that out and load the package for me. My favorite debugger is still print, and perl has to remind me to load Data::Dumper on the first go around for a new script.

( $year, $month, $day ) = ( localtime )[3,4,5];
No matter how many times I look it up, and how many times I use it, in moments of weakness I get it backwards. It's not that I think don't sync the left and right hand sides, but that I have it in my head that the year comes first. It's dumb that I think that because the size of the interval gets larger with higher indices.

my( $n, $m, $o ) = shift;
I start off with one variable and use shift to assign it a value, but then I need more variables, so I add them on the left-hand side, but I don't update the right-hand side. Sure it's dumb, but that doesn't mean that I don't do it.

--
brian d foy <brian@stonehenge.com>

In reply to Stupid mistakes I repeatedly make by brian_d_foy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (15)
    As of 2014-09-23 16:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (228 votes), past polls