Home / Alt manpages / perllexwarn(1)

  • perllexwarn(1)
  • User command
  • linux

Control Perl Warnings Without Silencing the Whole Program

You will finish with a Perl program that enables lexical warnings, turns off one known warning only where it is expected, and leaves the rest of the program checked. The examples use Perl v5.38.2 and the matching perl-doc package installed on this machine.

Allow about fifteen minutes. You need a shell and Perl. Nothing in this guide needs elevated privileges, writes outside the temporary files you create, or changes Perl's installation.

1. Check the installed Perl

Start by recording the interpreter that will run the examples:

$ perl -v | sed -n '1,4p'
This is perl 5, version 38, subversion 2 (v5.38.2) built for x86_64-linux-gnu-thread-multi

$ dpkg-query -W -f='${Package} ${Version}\n' perl-doc
perl-doc 5.38.2-3.2ubuntu0.6

Package names and version suffixes differ between distributions. The relevant fact here is the interpreter version, because warning categories and defaults are version-specific. The perllexwarn manual page installed with this package is dated 14 September 2026 and identifies itself as part of Perl v5.38.2 documentation. It also says that the detailed documentation has moved to the warnings documentation.

Checkpoint: run perl -e 'print "$^V\n"'. It should print v5.38.2 on this machine.

2. Enable warnings in the file

Put the pragma near the top of a program. It applies lexically, so its setting belongs to the enclosing file, block or other lexical scope:

use strict;
use warnings;

my $value = @ARGV[0];
print "value: $value\n";

Run a small equivalent without creating a file:

$ perl -e 'use warnings; my $value = @ARGV[0];'
Scalar value @ARGV[0] better written as $ARGV[0] at -e line 1.

The process still exits successfully here, so a warning is diagnostic output, not automatically a fatal error. The message identifies a likely mistake: indexing an array expression in scalar context is usually better written as a scalar element access. Fix the code when the warning represents a real defect:

my $value = $ARGV[0];

Do not use a warning-free run as proof that a program is correct. It only tells you that this execution did not trigger the enabled warning checks.

3. Suppress one category in a narrow block

Sometimes a warning is expected at a particular boundary. Disable its category inside the smallest block that contains that boundary, then let the surrounding code keep its normal checks:

use strict;
use warnings;

my $display_name;
{
    no warnings 'uninitialized';
    print "name: $display_name\n";
}

my $count;
print "count: $count\n";

The first print is an intentional use of an undefined value, so the uninitialized category is disabled for that block. The second print is outside it and remains covered by use warnings. Keep a short comment beside a suppression in real code explaining the data contract that makes it safe. If you cannot explain why the value may be undefined, fix the input or initialise the variable instead.

Verify the distinction with a command that shows the outer warning:

$ perl -e 'use warnings; { no warnings "uninitialized"; my $inside; print $inside; } my $outside; print $outside;'
Use of uninitialized value $outside in print at -e line 1.

Exact line numbers vary with the file and Perl invocation. The useful result is that only the use outside the block produces the warning.

4. Enable all warnings, then make a precise exception

use warnings; enables all ordinary warning categories for its scope. You can write use warnings 'all'; when making that intention explicit. A combined import can enable and disable categories in one statement:

use strict;
use warnings qw(all -uninitialized);

my $optional_value;
print "value: $optional_value\n";

Negative categories use a leading hyphen. The list is processed from left to right, so later entries can refine earlier ones. For example, use warnings qw(all -experimental experimental::somefeature); broadly disables experimental warnings and then re-enables the named subcategory. Use this form only when the category names describe a deliberate compatibility boundary. Otherwise, a small local block is easier to review.

Perl v5.34 and later support these negative warning arguments. This guide uses v5.38.2, so the syntax is available in the installed interpreter. If a program must run on older Perl releases, check the minimum supported version before adopting it.

5. Understand command-line warning switches

The older -w switch enables warnings globally, including code in modules that your program loads. That broad scope is the reason the lexical pragma is normally preferable for new code and maintained modules. The warnings manual also documents -W, which enables all warnings throughout the program, and -X, which disables all warnings.

$ perl -w -e 'my $value = @ARGV[0];'
Scalar value @ARGV[0] better written as $ARGV[0] at -e line 1.

$ perl -X -e 'my $value = @ARGV[0];'

These switches are useful for diagnosing an existing program, but do not treat -X as a fix. It can hide useful diagnostics, including ones emitted by modules. Likewise, a local no warnings does not make unsafe code safe; it only changes reporting in that lexical scope. The -W and -X switches are command-line overrides, so record them in a wrapper or service definition if you use them deliberately.

6. Check a real script without changing it

For a script you are reviewing, first ask Perl to compile it without running its normal top-level actions:

$ perl -c path/to/program.pl
path/to/program.pl syntax OK

-c checks syntax and performs the compile-time parts of the file, but it is not a complete runtime test. Warnings triggered only by input processing or later function calls still need a safe test invocation. Keep the warning pragma in the script and pass representative, non-sensitive fixture data rather than disabling warnings just to obtain a quiet output.

If a warning is noisy, capture the smallest reproducer. Confirm the category, narrow the scope of any suppression, and rerun the original test. Do not edit a system-installed module in place to silence its warnings. Put a local compatibility wrapper around the call, or fix the module through the normal package or source-management process.

Done means

  • perl -v identifies the interpreter version used for testing.
  • The program uses use warnings; or an explicit equivalent in its lexical scope.
  • Any exception names a warning category and is limited to the smallest justified block.
  • Warnings are distinguished from fatal errors, and a quiet run has not been mistaken for proof of correctness.
  • Command-line overrides such as -w, -W or -X are documented when used.