Always use strict!

strict

Why should I always use strict?

It is simple. It can save you a lot of time and headache.

use strict; is basically a compiler flag that tells the Perl compiler to change its behaviour in 3 important ways.

You can turn on and off the three areas separately, but if you just write use strict; at the top of each perl file (both scripts and modules), then you turn on all 3 of them.

The 3 parts of use strict

use strict 'vars'; generates a compile-time error if you access a variable without declaration.

use strict 'refs'; generates a runtime error if you use symbolic references.

use strict 'subs'; compile-time error if you try to use a bareword identifier in an improper way.

Turning off strict

While in general it is a good thing to have strict in effect in all the code, there are cases when we would like to use the extra magic power we can have without strict. For such cases we would like to be able to turn it off.

Once you turned on with use strict;, you can selectively turn off some, or all of the 3 parts in a lexical scope. That is, you can turn off parts of the strictness within a pair of curly braces {}.

use strict;

if (...) {
   no strict 'refs';
   # do you trick here...
}

For examples, see the three articles above.

hidden strict

There are a number of modules that if you use them in a file, it will automatically and implicitly turn use strict on in that specific file.

Among the modules are Moose, Moo, Dancer, and Mojolicious, but there are more.

There is a list of such modules in the source of Test::Strict. If you find more such modules, please open a ticket for Test::Strict, or send a pull-request with the fix.

Perl 5.12 and newer

If a file requires perl 5.12 or later (eg. by having use 5.012; or use 5.12.0; in the code), this too implicitly load use strict;.

So when you read code, or when you copy examples, please make sure you notice the implicit use of strict.

Other pages

use diagnostic; or use splain
readline() on closed filehandle in Perl
our
our
my
Why does this code not work? (split, array slice) - Solution
Why does this code not work? (split, array slice) - Solution
Perl::Critic example - lint for Perl
Perl Critic reports
Packages, modules, distributions, and namespaces in Perl
Package variables and Lexical variables in Perl
Multi dimensional hashes in Perl
Length of an array in Perl
How to improve my Perl program?
Getting started - Create skeleton for the SCO clone application
Getting Started with Perl::Critic (the linter for Perl)
Constructor and accessors in classic Perl OOP
Can't use string (...) as an HASH ref while "strict refs" in use at ...
Bug in the for-loop of Perl? - B::Deparse to the rescue
Avoid (unwanted) bitwise operators
Always use warnings in your Perl code!
Always use warnings in your Perl code!

Author

Gabor Szabo (szabgab) Gabor Szabo