strict(3pm)
π strict(3pm) β Perl Programmers Reference Guide π
πΈ NAME πΈ
strict β Perl pragma to restrict unsafe constructs. That's the name, no cap β it restricts unsafe constructs, periodt. π
β¨ SYNOPSIS β¨
Here's the whole vibe in code, bestie:
use strict;
use strict "vars";
use strict "refs";
use strict "subs";
use strict;
no strict "vars";
Slay. You can turn it all on, or just specific tags, and you can even no strict "vars" to switch one back off. π
π DESCRIPTION π
The "strict" pragma disables certain Perl expressions that could behave unexpectedly or are difficult to debug, turning them into errors. So it's lowkey making your bugs loud on purpose. The effect of this pragma is limited to the current file or scope block β highkey scoped, no leaking. π¦
If no import list is supplied, all possible restrictions are assumed. (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: "subs", "vars", and "refs". π
π¦ "strict refs" π¦
This generates a runtime error if you use symbolic references (see perlref). Runtime error, not compile-time β pls note the difference, bestie.
use strict 'refs';
$ref = \$foo;
print $$ref; # ok
$ref = "foo";
print $$ref; # runtime error; normally ok
$file = "STDOUT";
print $file "Hi!"; # error; note: no comma after $file
There is one exception to this rule:
$bar = \&{'foo'};
&$bar;
is allowed so that goto &$AUTOLOAD would not break under stricture. So yeah, this one tiny exception exists for goto &$AUTOLOAD β it's giving "exception to the rule, but make it iconic." π
π¦ "strict vars" π¦
This generates a compile-time error if you access a variable that was neither explicitly declared (using any of my, our, state, or use vars) nor fully qualified. (Because this is to avoid variable suicide problems and subtle dynamic scoping issues, a merely local variable isn't good enough.) See "my" in perlfunc, "our" in perlfunc, "state" in perlfunc, "local" in perlfunc, and vars. So local alone does not count β I know, devastating. π
use strict 'vars';
$X::foo = 1; # ok, fully qualified
my $foo = 10; # ok, my() var
local $baz = 9; # blows up, $baz not declared before
package Cinna;
our $bar; # Declares $bar in current package
$bar = 'HgS'; # ok, global declared via pragma
The local() generated a compile-time error because you just touched a global name without fully qualifying it. So no sneaky local loophole, okay? π
Because of their special use by sort(), the variables $a and $b are exempted from this check. Shoutout to $a and $b β main character variables for real. π
π¦ "strict subs" π¦
This disables the poetry optimization, generating a compile-time error if you try to use a bareword identifier that's not a subroutine, unless it is a simple identifier (no colons) and that it appears in curly braces, on the left hand side of the => symbol, or has the unary minus operator applied to it. Poetry optimization is such a cute name, and strict subs is like, "not today, babe." π
use strict 'subs';
$SIG{PIPE} = Plumber; # blows up
$SIG{PIPE} = "Plumber"; # fine: quoted string is always ok
$SIG{PIPE} = \&Plumber; # preferred form
See "Pragmatic Modules" in perlmodlib. Go read that when you're done here, iykyk. π¦
π HISTORY π
"strict 'subs'", with Perl 5.6.1, erroneously permitted to use an unquoted compound identifier (e.g. Foo::Bar) as a hash key (before => or inside curlies), but without forcing it always to a literal string. Ancient Perl lore, honestly. π
Starting with Perl 5.8.1 strict is strict about its restrictions: if unknown restrictions are used, the strict pragma will abort with
Unknown 'strict' tag(s) '...'
So don't try to invent new strict tags, bestie β it will literally abort. π
As of version 1.04 (Perl 5.10), strict verifies that it is used as "strict" to avoid the dreaded Strict trap on case insensitive file systems. Case insensitive file systems stay trying to cause chaos, so strict checks itself. Iconic behavior. β¨
Beginning with Perl 5.12, use of use VERSION (where VERSION >= 5.11.0) now lexically enables strictures just like use strict (in addition to the normal use VERSION effects and features.) In other words, use v5.011 or higher now implies use strict automatically, as noted in "Implicit strictures" in perl5120delta and "use VERSION". Automatic strictures? We love a convenience feature that keeps the vibes safe. π
Okay that's the whole strict(3pm) situation, bestie. Source/version: perl v5.42.3, date: 2026-11-03, manual: strict(3pm). Go forth and be strict (safely). πβ¨π«Ά
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms