I'd say start with the Modern Perl book. If you've been programming in Perl for years, then at first it seems a little frustrating when it devotes pages and pages to the difference between scalars, arrays and hashes. I was certainly tempted to skip a few chapters, but in the end I'm glad that I did not. Because even if it's telling you stuff you already know, reading through it forces you to think about that stuff, and perhaps re-evaluate them in your head.
Try to break code out into small logical chunks (usually modules) with clean interfaces between them. Object-oriented programming is often a good way of doing this, but it's not the only way, and sometimes not the best way.
Learn Larry Wall's virtues of a programmer and meditate upon how you can become more virtuous.
From the three virtues (especially from the first two), one central principle of DRY (don't repeat yourself) emerges. Avoid repetition like the plague.
If you have the following scattered around a module:
$self->foo;
$self->bar;
i.e. a call to foo immediately followed by a call to bar, then write a wrapper:
sub foo_bar {
my $self = shift;
$self->foo;
$self->bar;
}
... and use that instead. This may seem trivial, and perhaps like it's causing you more work than it's saving. But, it makes the case where you (either intentionally or unintentionally) call bar before foo stand out like a sore thumb.
Some people are uncomfortable doing the above. An extra sub call can make your program slower. But don't fall into the premature optimisation trap. Write your program how it makes most sense, not how you think will run the fastest. If, at the end, it runs slowly, then you can run it through a profiler and see what's really slowing it down. (It's often not what you think it will be.)
If you find you have several wrappers like foo_bar then you're repeating yourself again, and you can eliminate repetition at another level:
BEGIN {
no strict 'refs';
my %wrappers = (
foo_bar => [qw/foo bar/],
foo_baz => [qw/foo baz/],
quux_xyzzy => [qw/quux xyzzy/],
);
while (my ($wrapper, $methods) = each %wrappers)
{
*{$wrapper} = sub
{
my $self = shift;
$self->$_ foreach @$methods;
};
}
}
Here, you haven't written the wrapper subs; you've written a program to auto-generate the wrapper subs. This is often referred to as "meta-programming". One of the ultimate tools for meta-programming (in the Perl world, and probably generally) is Moose. Implement one or two projects using Moose, and it will start changing how you think about Perl. Once you're familiar with Moose, you'll find that even when you're writing non-Moose code, you can apply Moose and meta-programming ideas.
Almost as bad as repeating yourself is repeating other people. Don't write poorly-thought out code that does X and Y when there are great Text::X and Data::Y modules on CPAN. Stand on the shoulders of giants.
|