1strict(3pm)            Perl Programmers Reference Guide            strict(3pm)
2
3
4

NAME

6       strict - Perl pragma to restrict unsafe constructs
7

SYNOPSIS

9           use strict;
10
11           use strict "vars";
12           use strict "refs";
13           use strict "subs";
14
15           use strict;
16           no strict "vars";
17

DESCRIPTION

19       The "strict" pragma disables certain Perl expressions that could behave
20       unexpectedly or are difficult to debug, turning them into errors. The
21       effect of this pragma is limited to the current file or scope block.
22
23       If no import list is supplied, all possible restrictions are assumed.
24       (This is the safest mode to operate in, but is sometimes too strict for
25       casual programming.)  Currently, there are three possible things to be
26       strict about:  "subs", "vars", and "refs".
27
28       "strict refs"
29             This generates a runtime error if you use symbolic references
30             (see perlref).
31
32                 use strict 'refs';
33                 $ref = \$foo;
34                 print $$ref;        # ok
35                 $ref = "foo";
36                 print $$ref;        # runtime error; normally ok
37                 $file = "STDOUT";
38                 print $file "Hi!";  # error; note: no comma after $file
39
40             There is one exception to this rule:
41
42                 $bar = \&{'foo'};
43                 &$bar;
44
45             is allowed so that "goto &$AUTOLOAD" would not break under
46             stricture.
47
48       "strict vars"
49             This generates a compile-time error if you access a variable that
50             was neither explicitly declared (using any of "my", "our",
51             "state", or "use vars") nor fully qualified.  (Because this is to
52             avoid variable suicide problems and subtle dynamic scoping
53             issues, a merely "local" variable isn't good enough.)  See "my"
54             in perlfunc, "our" in perlfunc, "state" in perlfunc, "local" in
55             perlfunc, and vars.
56
57                 use strict 'vars';
58                 $X::foo = 1;         # ok, fully qualified
59                 my $foo = 10;        # ok, my() var
60                 local $baz = 9;      # blows up, $baz not declared before
61
62                 package Cinna;
63                 our $bar;                   # Declares $bar in current package
64                 $bar = 'HgS';               # ok, global declared via pragma
65
66             The local() generated a compile-time error because you just
67             touched a global name without fully qualifying it.
68
69             Because of their special use by sort(), the variables $a and $b
70             are exempted from this check.
71
72       "strict subs"
73             This disables the poetry optimization, generating a compile-time
74             error if you try to use a bareword identifier that's not a
75             subroutine, unless it is a simple identifier (no colons) and that
76             it appears in curly braces, on the left hand side of the "=>"
77             symbol, or has the unary minus operator applied to it.
78
79                 use strict 'subs';
80                 $SIG{PIPE} = Plumber;   # blows up
81                 $SIG{PIPE} = "Plumber"; # fine: quoted string is always ok
82                 $SIG{PIPE} = \&Plumber; # preferred form
83
84       See "Pragmatic Modules" in perlmodlib.
85

HISTORY

87       "strict 'subs'", with Perl 5.6.1, erroneously permitted to use an
88       unquoted compound identifier (e.g. "Foo::Bar") as a hash key (before
89       "=>" or inside curlies), but without forcing it always to a literal
90       string.
91
92       Starting with Perl 5.8.1 strict is strict about its restrictions: if
93       unknown restrictions are used, the strict pragma will abort with
94
95           Unknown 'strict' tag(s) '...'
96
97       As of version 1.04 (Perl 5.10), strict verifies that it is used as
98       "strict" to avoid the dreaded Strict trap on case insensitive file
99       systems.
100
101
102
103perl v5.34.1                      2022-03-15                       strict(3pm)
Impressum