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>
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|