Enable strict and warnings in Perl 5 scripts and modules
Add Perl 5's core strict and warnings pragmas to catch typos, undefined values, and unsafe references before they reach production.
18 Aug 2025, 05:57 UTC

Problem and takeaway
Perl 5 will run a script that references a misspelled variable, calls an undeclared subroutine as a bareword, or uses a symbolic reference without stopping. These mistakes are not fatal by default, so they often appear as incorrect output or difficult runtime failures in production.
The practical takeaway is to enable Perl 5's two core lexically scoped pragmas at the top of every script or module: use strict; and use warnings;. They require no CPAN modules and have been included with Perl 5 since the late 1990s.
Desired outcome
After this procedure, Perl requires lexical variable declarations, rejects undeclared bareword subroutine calls, disallows symbolic references by default, and prints warnings for dubious syntax such as uninitialized values.
Prerequisites
- A Perl 5 interpreter available on
PATH. - Read and write access to the file you plan to edit.
- A terminal or command prompt for running
perl.
Procedure
1. Open the target file
Open the Perl script or module in a text editor. This guide uses script.pl as a placeholder; replace it with your actual path.
2. Add the pragmas near the top
Insert these lines immediately after the shebang line, if one exists, and before any other code:
#!/usr/bin/env perl
use strict;
use warnings;
Both pragmas are lexically scoped, meaning they apply from the declaration point to the end of the enclosing block or file.
3. Run a compile check
perl -c script.pl
Where to run: your terminal, in the same directory as the file. Required permissions: read access to the file; no elevated privileges. Expected result: perl -c compiles without executing and reports either success or lists errors. This command does not modify the file.
Typical strict failures include:
Global symbol "$foo" requires explicit package name— declare the variable withmy,our, orstate.Bareword "foo" not allowed while "strict subs"— declare the subroutine or adjust the call syntax.
4. Execute and review warnings
perl script.pl
Warnings are written to standard error and do not halt execution unless you escalate them. Inspect each warning in context; an uninitialized value warning usually means a variable was read before assignment.
5. Disable constraints only in the smallest scope
If a block legitimately needs a feature that strict prohibits, disable only that constraint locally:
use strict;
use warnings;
my $hash = { name => 'value' };
{
no strict 'refs';
# Isolated symbolic-reference usage
my $key = 'name';
my $value = $hash->{$key};
}
Prefer fixing the underlying issue over silencing warnings with no warnings;.
Expected checks
perl -c script.plcompletes with no strict-related compile errors.- Running the script produces no new warnings about uninitialized variables, redefined subroutines, or deprecated features.
- Any disabled pragma is scoped to the smallest possible block.
Recovery
Adding these pragmas changes source files only. If a legacy script breaks, remove or comment out the two lines, then re-enable them incrementally while fixing violations. Avoid warnings FATAL => qw(all) in long-running services without thorough testing, because it can convert unexpected runtime conditions into fatal exceptions.
Limitations
- This behavior applies to Perl 5. Raku, formerly Perl 6, is a separate language and does not share these pragmas.
- Warnings catch likely mistakes, not logic errors or incorrect output.
- Enabling warnings on a large legacy codebase can produce many messages; address them in batches.
Practical verification
Confirm the interpreter version:
perl -V
Review the version summary for the Perl 5 release. To observe warning behavior without editing a file:
perl -Mwarnings -e 'my $value; print $value'
This compiles and runs, but Perl emits a warning about the uninitialized value. Exact wording varies by Perl 5 version. To observe strict variable enforcement:
perl -Mstrict -e 'print $undeclared'
This produces a compile-time error because the variable was not declared. Neither command writes to disk.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.