Perl strict and warnings
last modified October 8, 2026
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; use v5.30.0;
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.
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 strict; use warnings; use v5.30.0; my $quantity; say $quantity + 1;
Use of uninitialized value $quantity in addition (+) at uninitialized.pl line 8. 1
The program may continue, but the warning signals that the undefined value was treated as zero in this numeric operation. Initialize it or handle the missing case explicitly.
my $quantity = 0; say $quantity + 1; # no warning
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 to compile a script without executing it:
$ perl -c program.pl program.pl syntax OK
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.
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.