Posted on

Using Common Sense With Perl

Since I was recently doing Perl work on the Boomerang project, I figured it would be a good time to finally try out common::sense, a CPAN module that, according to the documentation, will save a tree _and_ a kitten when you use it.  I like trees and cats, so Hell, why not try it then?

Long standing advice in the Perl world has been to use warnings and use strict in your code.  However, the style of modern Perl has changed in such a way that it conflicts with those two things.  It is common these days to write something along the line of

@{ $var->[0] }

but with ‘use strict’ enabled, you’re not going to get away with it.

You would instead have to write it as

@{ defined $var->[0] ? $var->[0] : [] }

No thanks.  That doesn’t sit well with my guns-a’blazing, cavalier attitude that I have when writting Perl.

Then there are the useful additions to recent Perl like ‘say’ and ‘given’, which you have to enable to use.  Perl 5.12 is fresh off the line, and 5.10 now has some age behind it.  So it seems valid to start setting 5.10 as the baseline implementation.  I think for the last eighty-seven years everyone has written everything targetting 5.8.8.

It’s time to move on.

Back to common::sense.  It is a collection of settings that assume you are a modern Perl kinda guy.  It’s for the man who uses Unicode, can deal with references, wants ‘state’ variables, and wants his program to die fatally on serious problems.  More specifically, ‘use common::sense’ is equivalent to

use utf8;
use strict qw(vars subs);
use feature qw(say state switch);
no warnings;
use warnings qw(FATAL closed threads internal debugging pack malloc
portable prototype inplace io pipe unpack regexp
deprecated exiting glob digit printf layer
reserved taint closure semicolon);
no warnings qw(exec newline unopened);

I think common::sense is a useful thing, much like common sense.

Using it hasn’t caused me to get any warnings or errors (that I wouldn’t already get…), and it lets me enable more recent features that I prefer to use.  I suggest trying it out sometime.

Note that unlike many pragmas, you cannot write ‘no common::sense’.

You’re already writing Perl, so that’s a given.  No need to spell it out.