Perl has its own unique set of gotchas and strengths. Some make it easy for beginners to hang themselves, and others could reduce the amount of code and heartache needed. (Perl is fairly unique among languages in that it often lets beginners and dabblers succeed despite their lack of formal training.)
Without further ado, here are Common Mistakes of Beginning Programmers (part one).
foreach ($i = 1; $i < 10; $i++) { ${"record$i"} = $i * $i; }
for (1 .. 10) { push @records, $_ * $_; } foreach my $record (@records) { print $record, "\n"; }
Yikes. At least he used CGI.pm, anyway. Here's another example:my $var1 = $query->param('var1'); my $var2 = $query->param('var2'); my $var3 = $query->param('var3'); my $var4 = $query->param('var4'); my $var5 = $query->param('var5'); my $var6 = $query->param('var6'); my $var7 = $query->param('var7'); my $var8 = $query->param('var8'); my $var9 = $query->param('var9'); my $var10 = $query->param('var10');
$report = generate_report($foo); open(FILE, ">>report.txt") or die "blah!"; print FILE "Report number: $i"; $i++; # # three lines to format header nicely # @report_lines = split(/\n/, $report); print FILE $report_lines[0]; print FILE $report_lines[1]; print FILE $report_lines[2]; print FILE $report_lines[3]; print FILE $report_lines[4]; close FILE or die "blergh!"; # skip a few $report = generate_report($bar); open(FILE, ">>report.txt") or die "blah!"; print FILE "Report number: $i"; $i++; # # format the header again # @report_lines = split(/\n/, $report); print FILE $report_lines[0]; # you see where this is going
Problems arise, as they always do, when a throwaway program becomes important enough to keep around. Those programs have a way of attracting deadlines -- you'll have to add something, but you won't have time to do it right. Repeat that another way. There's no better time to do a program right than before anyone's depending on it.
A literal translation of the first, somewhat better:
A whole lot better:my %vars; for (1 .. 10) { $vars{"var$_"} = $query->param("var$_"); }
The second is lots nicer:# change the form to use multiple selections with the same name my @vars = $query->param('var'); # valid if you have a combobox or checkbox group
Some items lend themselves to functions more than others. As you practice, you will improve at figuring out which things are important and which are variable.{ my $num_displayed = 0; sub display_report { my $data = shift; my $report = generate_report($data); open(FILE, ">>report.txt") or die "Can't append to report.txt: $!" +; print FILE "Report number: $num_displayed"; $num_displayed++; print FILE $report; } } # end of enclosing scope for $num_displayed
Update: Fixed an unintentional typo in the first beginner code, thanks to turnstep. chipmunk is right... I'm open to writing a second-parter, so send me your suggestions. Fixed a second unintentional typo in the second example, thanks to erix.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Common Beginner Mistakes
by Dominus (Parson) on Dec 15, 2000 at 09:38 UTC | |
Re: Common Beginner Mistakes
by japhy (Canon) on Dec 15, 2000 at 09:31 UTC | |
Re: Common Beginner Mistakes
by chipmunk (Parson) on Dec 15, 2000 at 04:54 UTC | |
Re: Common Beginner Mistakes
by Elias (Pilgrim) on Jan 27, 2001 at 16:15 UTC | |
Re: Common Beginner Mistakes
by mrmick (Curate) on Dec 18, 2000 at 21:26 UTC | |
Re: Common Beginner Mistakes
by merlyn (Sage) on Dec 18, 2000 at 01:26 UTC | |
Re: Common Beginner Mistakes
by royalanjr (Chaplain) on Dec 21, 2000 at 20:14 UTC | |
Re: Common Beginner Mistakes
by Mabooka-Mabooka (Sexton) on Apr 17, 2005 at 03:39 UTC | |
by Mabooka-Mabooka (Sexton) on Apr 17, 2005 at 04:03 UTC | |
by Mabooka-Mabooka (Sexton) on Apr 17, 2005 at 04:35 UTC | |
by Mabooka-Mabooka (Sexton) on Apr 17, 2005 at 16:43 UTC | |
Re: Common Beginner Mistakes
by Anonymous Monk on Jul 17, 2002 at 20:28 UTC | |
by cLive ;-) (Prior) on Aug 01, 2002 at 10:46 UTC | |
by Gnomon (Initiate) on Oct 14, 2010 at 18:28 UTC |