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.


In reply to Re: Learn better Perl Coding by tobyink
in thread Learn better Perl Coding by Anonymous Monk

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.