Dear monks,
I'm playing with perl-5.38.0-RC2 and my given / when statements are all receiving deprecation warnings. As explained in the perldelta :
The "switch" feature and the smartmatch operator, ~~, were introduced in v5.10. Their behavior was significantly changed in v5.10.1. When the "experiment" system was added in v5.18.0, switch and smartmatch were retroactively declared experimental. Over the years, proposals to fix or supplement the features have come and gone.
In v5.38.0, we are declaring the experiment a failure. Some future system may take the conceptual place of smartmatch, but it has not yet been designed or built.
These features will be entirely removed from perl in v5.42.0.
I use feature 'switch' and given / when in my program template for processing Getopt::Std options, which means it's everywhere.
#!/usr/bin/env perl
use v5.36;
use Getopt::Std;
use feature qw/switch/;
no warnings q/experimental::for_list/;
sub HELP_MESSAGE { ...; exit }
my $verbose;
my $force;
my $table;
my %opts;
HELP_MESSAGE() if !getopts( 'hvft:', \%opts );
for my ( $k, $v ) (%opts) {
given ($k) {
when ('h') { HELP_MESSAGE(); }
when ('v') { ++$verbose; }
when ('f') { ++$force; }
when ('t') { $table = $v; }
default { die "*** BUG: no handler for -$k.\n"; }
}
}
It looks like I can safely replace "given" with "for" to bind the topic variable. The "when" statement provided a concise way to test the topic, but since that's going away, is this the most concise way to write it?
for my ( $k, $v ) (%opts) {
if ( $k eq 'h' ) { HELP_MESSAGE(); }
elsif ( $k eq 'v') { ++$verbose; }
elsif ( $k eq 'f') { ++$force; }
elsif ( $k eq 't') { $table = $v; }
else { die "*** BUG: no handler for -$k.\n"; }
}
Thanks!