Perl Boilerplate

Mike Friedman posted about cleaning up Perl boilerplate and switching to Import::Into. I like his ideas, and in particular I will be using parameters to my Setup packages from now on.

This is my import subroutine.

<br />
sub import {<br />
    my $target = caller;<br />
    'utf8'->import::into($target);<br />
    'strict'->import::into($target);<br />
    'warnings'->import::into($target, qw(FATAL utf8));</p>
<p>    # uncoverable branch true<br />
    # uncoverable branch false<br />
    if ($] >= 5.017011) {<br />
        'warnings'->unimport::out_of('experimental::smartmatch');<br />
    }<br />
    if($] >= 5.01908) {<br />
    	'warnings'->unimport::out_of('experimental::autoderef');<br />
    }<br />
    'warnings'->unimport::out_of($target, 'qw');<br />
    'charnames'->import::into($target, qw(:full :short));<br />
    'Unicode::Normalize'->import::into($target);<br />
    'open'->import::into($target, qw(:std :utf8));<br />
    'feature'->import::into($target, ':5.16.0');<br />
    'namespace::sweep'->import::into($target);<br />
    'Data::Dumper'->import::into($target);<br />
    return;<br />
}<br />

My import is a little different:

  • I need UTF8 everywhere for some text processing.
  • I do the Moose setup separately, but if I pass a parameter to the setup module then it won’t be necessary. Putting it all in one place will simplify things.
  • After enabling warnings I explicitly turn off warnings for the experimental features I know I’m using, although using the features module might be a better thing to do.
  • I check Perl’s version before disabling specific warnings with unimport::outof, because I was getting warnings on the older Perls I need to support.
  • I have a few different modules to import.

My Moose setup uses MooseX::StrictConstructor and MooseX::HasDefaults::RO. The code and ideas behind them came almost verbatim from one of Ovid’s blog posts.