Fluent Perl

We've touched on a few idioms in the preceding sections (not to mention the preceding chapters), but there are many other idioms you'll commonly see if you read programs by accomplished Perl programmers. When we speak of idiomatic Perl in this context, we don't just mean a set of arbitrary Perl expressions with fossilized meanings. Rather, we mean Perl code that shows an understanding of the flow of the language, what you can get away with when, and what that buys you. And when to buy it.

We can't hope to list all the idioms you might see--that would take a book as big as this one. Maybe two. (See the Perl Cookbook, for instance.) But here are some of the important idioms, where "important" might be defined as "that which induces hissy fits in people who think they already know just how computer languages ought to work".

  • Use => in place of a comma anywhere you think it improves readability:

    return bless $mess => $class;

    This reads, "Bless this mess into the specified class." Just be careful not to use it after a word that you don't want autoquoted:

    sub foo () { "FOO" }
    sub bar () { "BAR" }
    print foo => bar;   # prints fooBAR, not FOOBAR;

    Another good place to use => is near a literal comma that might get confused visually:

    join(", " => @array);

    Perl provides you with more than one way to do things so that you can exercise your ability to be creative. Exercise it!

  • Use the singular pronoun to increase readability:

    for (@lines) {
        $_ .= "\n";
    }

    The $_ variable is Perl's version of ...

Get Programming Perl, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.