Automatically turn on warnings

This is a chapter in Perl New Features, a book from Perl School that you can buy on LeanPub or Amazon. Your support helps me to produce more content.



Perl v5.36 automatically turns on warnings when you specify the minimum Perl version with use:

use v5.36;  # use warnings for free

Since this form has been turning on strictures since v5.12 (Implicitly turn on strictures with Perl 5.12), you no longer have to specify warnings or strict at the top of my program.

If you don’t want warnings but still need to enforce a minimum version, use require (A<10-use-version>):

require v5.36;

However, with require, you miss out on all the other features that use would turn on automatically. Instead, you could turn off warnings explicitly:

use v5.36;
no warnings;

But beware of the modules that re-enable warnings for you. Although this is unrelated to what v5.36 is doing, I’m often caught out because I thought I turned off warnings (and I did), but I still get the warnings:

no warnings;
use Mojo::Base;  # warnings back on!

Part of M‘s job is to enable all the things that you probably want, including warnings. Since I tend to specify pragmas first followed by the modules, I end up with this ordering problem.

Breaking -X

The -X command-line switch should suppress all warnings, which makes it a good choice for running perl in production. In that case, you should be warning free, and if you aren’t, you don’t want to fill up your logs with messages you aren’t monitoring.

However, if you implicitly enable warnings as part of the feature bundle, you still get warnings (U):

% perl5.36.1 -Mwarnings -X -E 'say 1 + "a"'
1
% perl5.36.1 -Mv5.36 -X -E 'say 1 + "a"'
Argument "a" isn't numeric in addition (+) at -e line 1.
1

Updating the feature bundle

This release of Perl is the first update to the feature bundle since
v5.28. See what each feature bundle enables with feature::features_enabled(). Here’s what you get for free with use v5.28 (and up to v5.34):

% perl -Mv5.28 -e 'use feature qw(:5.28);⏎BEGIN { say join qq(\n), feature::features_enabled() }'
bareword_filehandles
bitwise
current_sub
evalbytes
fc
indirect
multidimensional
postderef_qq
say
state
switch
unicode_eval
unicode_strings

Here’s the list for v5.36:

% perl -Mv5.36 -e 'use feature qw(:5.36);⏎BEGIN { say join q(\n), feature::features_enabled() }'
bareword_filehandles
bitwise
current_sub
evalbytes
fc
isa
postderef_qq
say
signatures
state
unicode_eval
unicode_strings

Notice the differences: indirect is missing, isa is now enabled, multidimensional is missing, and switch is missing.

You might like a simple program I made to show in a spreadsheet all of the versions of Perl and which features their version enables.