Perl strict and warnings
last modified October 8, 2026 Perl 5.44
Enable strict and warnings in Perl programs.
strict requires safer, more explicit code in several areas;
warnings reports suspicious operations that may otherwise
produce incorrect results silently.
use strict; use warnings;
These pragmas apply lexically: they affect the scope where they are enabled and nested scopes within it. Put them near the start of each script or module.
Version declarations enable strict
A use v5.12 declaration or any later version enables
strict automatically, together with the feature bundle of
that version, such as say. Starting with
use v5.36, warnings is enabled as well. The
Perl articles on this site begin with use warnings and
use v5.34.0, so they need no separate
use strict.
#!/usr/bin/perl use warnings; use v5.34.0; my $total = 12; $totla += 3; say $total;
The program contains no use strict line, yet the
misspelt $totla is rejected at compile time. The version
declaration has turned strict mode on, so nothing is printed.
$ perl version.pl Global symbol "$totla" requires explicit package name (did you forget to declare "my $totla"?) at version.pl line 7. Execution of version.pl aborted due to compilation errors.
Strict catches undeclared variables
With strict 'vars' enabled by use strict,
variables must be declared or fully qualified. This catches many spelling
mistakes before the program runs.
use strict; use warnings; my $total = 12; $totla += 3; # compile-time error: undeclared variable
The likely intent is to use $total on the final line.
Declare local variables with my; use our when
intentionally referring to a package variable.
Strict checks references and barewords
strict also enables checks for symbolic references and
barewords. Symbolic references treat the contents of a string as a
variable name and are error-prone; use real references instead. Bareword
checks catch many misspelled names that Perl might otherwise interpret as
strings or indirect syntax.
use strict; use warnings; my $name = 'message'; my $value = $$name; # strict refs rejects symbolic references
If dynamic lookup is required, store values in a hash or use explicit references rather than turning strings into variable names.
Warnings reveal undefined values
Using an undefined value in a string or numeric operation often
indicates an uninitialized variable, missing input, or misspelled data.
warnings reports the issue so it can be handled
deliberately.
#!/usr/bin/perl use warnings; use v5.34.0; my $quantity; say $quantity + 1;
The program continues, but the warning signals that the undefined
value was treated as zero in this numeric operation. Initialize the
variable, for example with my $quantity = 0;, or handle the
missing case explicitly.
$ perl uninitialized.pl Use of uninitialized value $quantity in addition (+) at uninitialized.pl line 7. 1
Warnings catch suspicious conversions
Warnings can also identify unexpected numeric conversions. For example, text that does not begin with a valid number is not a safe substitute for a validated numeric input.
use strict; use warnings; my $input = 'many'; my $total = $input + 2; # warning: non-numeric value used in addition
Validate input before using it in arithmetic. A warning is diagnostic help, not a complete input-validation strategy.
Check a script without running it
Use Perl's syntax-check option -c to compile a script
without executing it. Warnings may be emitted during a syntax check, and
runtime warnings appear when the relevant code executes. Keep both
pragmas enabled while developing and in production code so mistakes
remain visible.
$ perl -c uninitialized.pl uninitialized.pl syntax OK
Use narrow exceptions
Do not disable all warnings just to hide a message. If a specific
operation is understood and necessarily triggers a warning, use a
narrowly scoped no warnings with an explanation, or adjust
the code to avoid the warning. Most application code should need no
warning suppression.
See Perl variables for declarations with
my and our, and Perl subroutines for lexical scope in reusable
code.
Source
Author
List all Perl tutorials.