I am migrating an old toolchain to a new system and now I get plenty of notifications given is experimental
or when is experimental
.
$ perl -e 'use v5.10; given (12) { when (12) { print "Hello World" }}'
given is experimental at -e line 1.
when is experimental at -e line 1.
Hello World
I would like my new system to be fully compatible with the old one. By this I mean the exact same output.
Is there a way to mute these notifications without touching the oneliners nor the scripts?
Warnings can be disabled selectively by using 'no warning' pragma within a scope along with an argument list. If the argument list is not provided then this pragma will disable all the warnings within that scope. Example: Perl.
Ans: The -w Command-line option: It will display the list if warning messages regarding the code. strict pragma: It forces the user to declare all variables before they can be used using the my() function.
First of all, note that smartmatching will be removed or changed in a backwards incompatible manner. This may affect your given
statements.
To use given
+when
without warnings, one need the following:
# 5.10+
use feature qw( switch );
no if $] >= 5.018, warnings => qw( experimental::smartmatch );
or
# 5.18+
use feature qw( switch );
no warnings qw( experimental::smartmatch );
experimental provides a shortcut for those two statements.
use experimental qw( switch );
Finally, you ask how to add this to your programs without changing them (and presumably without changing Perl). That leaves monkeypatching.
I wouldn't recommend it. It's far easier to write a couple of one-liners to automatically fix up your programs that rewritting Perl's behaviour on the fly.
But if you want to go in that direction, the simplest solution is probably to write a $SIG{__WARN__}
handler that filters out the undesired warnings.
$SIG{__WARN__} = sub {
warn($_[0]) if $_[0] !~ /^(?:given|when) is experimental at /;
};
(Of course, that won't work if your program makes use of $SIG{__WARN__}
already.)
To get it loaded without changing your programs or one-liners, all you have to do is place the patch in a module, and tell Perl to load the module as follows:
export PERL5OPT=-MMonkey::SilenceSwitchWarning
$ cat Monkey/SilenceSwitchWarning.pm
package Monkey::SilenceSwitchWarning;
use strict;
use warnings;
$SIG{__WARN__} = sub {
warn($_[0]) if $_[0] !~ /^(?:given|when) is experimental at /;
};
1;
$ perl -e 'use v5.10; given (12) { when (12) { print "Hello World\n" }}'
given is experimental at -e line 1.
when is experimental at -e line 1.
Hello World
$ export PERL5OPT=-MMonkey::SilenceSwitchWarning
$ perl -e 'use v5.10; given (12) { when (12) { print "Hello World\n" }}'
Hello World
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With