Implicitly turn on strictures with Perl 5.12

Perl 5.12 can turn on strict for you automatically, stealing a feature from Modern::Perl that takes away one line of boilerplate in your Perl programs and modules. We talk about strict in Item 3: Enable strictures to promote better coding. Similar to what we show in Item 2: Enable new Perl features when you need them, to turn strictures on automatically, you have to use use with a version of Perl 5.11.0 or later:

use 5.012;

This is really just the same thing as explicitly turning on strict and importing the 5.12 feature bundle:

use strict;
use feature ':5.12';

Note that this means merely importing the feature bundle does not include this implicit strictures benefit. Don’t think you’re safe merely because you are using a perl5.12 interpreter.

Similarly, using the -E switch, which imports the feature bundle for that version of Perl, does not turn on strictures. You can import strict explicitly (although you are probably not interested in strictures on the command line):

% perl5.12.1 -E '$sum = 0;'
% perl5.12.1 -Mstrict -E '$sum = 0;'
Global symbol "$sum" requires explicit package name at -e line 1.
Execution of -e aborted due to compilation errors.

Remember, however, that strict is lexically scoped, and that a file is a scope (Know what creates a scope). That is, the use 5.012 only affects the file that it is in, but any files it loads do not have to be strict-clean.

Consider this strict-dirty module:

# NoStrict.pm
package NoStrict;

$sum = 0;

1;

When you load it in a program, any strict settings do not descend into NoStrict.pm:

use 5.012;

use lib qw(.);
use NoStrict; # strict dirty module

my $sum = 0;

When you run this program, it compiles just fine:

perl5.12.1 strict.pl

To get around this, you have to set strict in every file you want to apply it to.

Things to remember

  • use 5.012 turns on strictures in it’s file
  • The implicit strictures feature is not part of the 5.12 feature bundle
  • The -E command-line switch does not turn on strictures.