start page | rating of books | rating of authors | reviews | copyrights

Programming Perl

Programming PerlSearch this book
Previous: 7.2.58 Socket - Load the C socket.h Defines and Structure Manipulators Chapter 7
The Standard Perl Library
Next: 7.2.60 subs - Predeclare Subroutine Names
 

7.2.59 strict - Restrict Unsafe Constructs

use strict;        # apply all possible restrictions  use strict 'vars'; # restrict unsafe use of variables for rest of block use strict 'refs'; # restrict unsafe use of references for rest of block use strict 'subs'; # restrict unsafe use of barewords for rest of block  no strict 'vars';  # relax restrictions on variables for rest of block no strict 'refs';  # relax restrictions on references for rest of block no strict 'subs';  # relax restrictions on barewords for rest of block

If no import list is given to use strict , all possible restrictions upon unsafe Perl constructs are imposed. (This is the safest mode to operate in, but is sometimes too strict for casual programming.) Currently, there are three possible things to be strict about: refs , vars , and subs .

In all cases the restrictions apply only until the end of the immediately enclosing block.

strict 'refs'

This generates a run-time error if you use symbolic references.

use strict 'refs'; $ref = \$foo; print $$ref;        # ok $ref = "foo"; print $$ref;        # run-time error; normally ok
strict 'vars'

This generates a compile-time error if you access a variable that wasn't declared via my , or fully qualified, or imported.

use strict 'vars'; use vars '$foe'; $SomePack::fee = 1;  # ok, fully qualified my $fie = 10;        # ok, my() var $foe = 7;            # ok, pseudo-imported by 'use vars' $foo = 9;            # blows up--did you mistype $foe maybe?

The last line generates a compile-time error because you're touching a global name without fully qualifying it. Since the purpose of this pragma is to encourage use of my variables, using local on a variable isn't good enough to declare it. You can, however, use local on a variable that you declared with use vars .

strict 'subs'

This generates a compile-time error if you try to use a bareword identifier that's not a predeclared subroutine.

use strict 'subs';  $SIG{PIPE} = Plumber;     # blows up (assuming Plumber sub not declared yet) $SIG{PIPE} = "Plumber";   # okay, means "main::Plumber" really $SIG{PIPE} = \&Plumber;   # preferred form

The no strict 'vars' statement negates any preceding use strict vars for the remainder of the innermost enclosing block. Likewise, no strict 'refs' negates any preceding invocation of use strict refs , and no strict 'subs' negates use strict 'subs' .

The arguments to use strict are sometimes given as barewords - that is, without surrounding quotes. Be aware, however, that the following sequence will not work:

use strict;      # or just:  use strict subs; ... no strict subs;  # WRONG!  Should be:  no strict 'subs'; ...

The problem here is that giving subs as a bareword is no longer allowed after the use strict statement. :-)


Previous: 7.2.58 Socket - Load the C socket.h Defines and Structure Manipulators Programming Perl Next: 7.2.60 subs - Predeclare Subroutine Names
7.2.58 Socket - Load the C socket.h Defines and Structure Manipulators Book Index 7.2.60 subs - Predeclare Subroutine Names