
Typing “use strict” at the top of all of my perl scripts and modules has become second nature to me. I never skip the strict directive, and am confident that my code meets a particular level of reliability because of it. The purpose of this post is is to delve into the what’s going on behind the two word statement, and to convince those new to the Perl game, that they should also make the inclusive of “use strict” a never missed habit.
The Perl documentation for strict defines “Perl pragma to restrict unsafe constructs”.
There are three difference specifications
strict refs
Using strict refs prevents you from using symbolic references in your code. Symbolic references can be very powerful, and their use can lead to some pretty neat one lines, but they are dangerous. It is very easy to make a mess of things when using symbolic references. Since there are always multiple ways of doing things in Perl, you pretty much never have to use symbolic references. At least I have never had to use them. To save headaches, use strict refs to prevent the misuse of symbolic references!
use strict 'refs'; my $foo = "List Central kicks ass!"; my $ref = \$foo; print "A string - \$foo: $foo\"; print "Reference to a string - \$ref: $ref\n"; print "Dereferenced reference to a string - \$\$ref: $$ref\n"; print "Dereferenced string - \$\$foo: $$foo\n";<
Outputs:
Reference to a string – $ref: SCALAR(0×814f558)
Dereferenced reference to a string – $$ref: List Central kicks ass!
Can’t use string ("List Central kicks ass!") as a SCALAR ref while "strict refs" in use at strict.pl line 11.
strict vars
Using strict vars prevents you from trying to access variables that have not been defined within your scope with either user vars, our or my. This prevents you from trying to access a variable you think is within your scope, but actually isn’t This is particularly helpful for those starting out in Perl, or programming in general, as the whole scope thing takes a little time to get used to.
use strict 'vars'; my $neato = "List Central is neat!"; print "\$neato: $neato\n"; our $coolio = "List Central is cool!"; print "\$coolio: $coolio\n"; $fabulous = "List Central is fab"; print "\$fabulous: $fabulous\n"; local $hotness = "List Central is hot"; print "\$hotness: $hotness\n";
Outputs:
$neato: List Central is neat!
$coolio: List Central is cool!
The second two fail at compile time:
Global symbol "$fabulous" requires explicit package name at strict.pl line 10.
Global symbol "$fabulous" requires explicit package name at strict.pl line 11.
Global symbol "$hotness" requires explicit package name at strict.pl line 13.
Global symbol "$hotness" requires explicit package name at strict.pl line 14.
strict subs
Using the strict subs pragma enables compile time errors if you include any bareword (word with no variable indication in the form of preceding symbol, with the exception of the word occurring between curly braces, or on the left ahnd side of ‘=>’) that is not the name of a subroutine. The restrictions on barewords can save you from a lot of careless errors caused by coding at a feverish pace, or your run of the mill typo errors. Without the strict subs pragma, these sorts of errors can be difficult to track down.
use strict 'subs';
my $foo = ListCentral; # Compile time error
my $bar = "List Central kicks ass!"; # A-OK, a string
my %foobar = (ListCentral => "Kick ass!"); # Hash declaration is ok
print "\$foobar{ListCentral}: $foobar{ListCentral}\n";
Outputs:
Bareword "ListCentral" not allowed while "strict subs" in use at strict.pl line 6.
The remaining lines run fine and yield the following output:
$foobar{ListCentral}: Kick ass!
The pragma “use strict” incorporates all three of the above (refs, vars & subs). Using strict can save you a lot of headaches. I strongly suggest always using strict, unless you are wanting to play around to find out what sorts of trouble you can get into without it!


One Response
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
I’ve never had cause to disable strict vars or strict subs, but I often find myself setting “no strict ‘refs’” within a small block when I need to manipulate the symbol table, usually to dynamically create new named subs. But that’s the only time I find myself not coding under full strictures.