My Favorite Warnings: redundant
and missing
The redundant
and missing
warnings were added in Perl 5.22 to cover the case where a call to the printf
or sprintf
had more (redundant
) or fewer (missing
) arguments than the format calls for. The documentation says that they may be extended to other built-ins (pack
and unpack
being named specifically) but as of Perl 5.34.0 only the printf() built-ins are covered.
I have (very occasionally) found myself writing a subroutine taking a printf-style format and some arguments, and letting the format specify which (if any) of the arguments actually appear in the output. If I just throw all the arguments after the format into the printf(), one of these warnings is very likely to be thrown, starting with 5.22, since use warnings;
enables them by default.
Getting such code to work silently under versions of Perl both before and after the warnings were introduced puzzled me for a bit. Eventially I realized the solution was another pragma: if
, which has been in core since Perl 5.6.2. To wit:
sub my_printf { no if "$]" >= 5.022, qw{ warnings redundant missing }; return printf @_; }
This is the second entry in a desultory series of blogs on the warnings pragma.
I had no idea that 'no' could take an 'if'. And I've been Perling since 1996.
Can we get a link to the first desultory entry? :-)
It’s not an actual
if
, it’s just a module calledif
. That’s why you need a comma after the condition, and why the name of the module you want to load and the arguments you want to pass it come after the condition rather than near theuse
/no
like normal.Handy though.
First desultory entry: My Favorite Warnings: once.