Perl Boilerplate

Submitted by michael on Sun, 06/08/2014 - 12:55
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. [perl] sub import { my $target = caller; 'utf8'->import::into($target); 'strict'->import::into($target); 'warnings'->import::into($target, qw(FATAL utf8)); # uncoverable branch true # uncoverable branch false if ($] >= 5.017011) { 'warnings'->unimport::out_of('experimental::smartmatch'); } if($] >= 5.01908) { 'warnings'->unimport::out_of('experimental::autoderef'); } 'warnings'->unimport::out_of($target, 'qw'); 'charnames'->import::into($target, qw(:full :short)); 'Unicode::Normalize'->import::into($target); 'open'->import::into($target, qw(:std :utf8)); 'feature'->import::into($target, ':5.16.0'); 'namespace::sweep'->import::into($target); 'Data::Dumper'->import::into($target); return; } [/perl] 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.