Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: v5.36 syntax error around given/when

by Anonymous Monk
on Jun 10, 2022 at 14:17 UTC ( [id://11144660]=note: print w/replies, xml ) Need Help??


in reply to v5.36 syntax error around given/when

In this application you can replace the given/when with a dispatch table. Other changes you can make since you use v5.36;:

  • You no longer need use strict; -- this is implied by use v5.12; or above;
  • You can use the experimental for-list syntax, though for the moment you will need to silence the "experimental" warning;
  • You can either eliminate the no if ... or just disable the warnings for smartmatch unconditionally, depending on whether you use them elsewhere in your code.

The each() built-in is discouraged because there is only a single iterator for a given hash, which can be reset in un-obvious ways -- see the docs for each() for details.

My take on your boilerplate, with all the above incorporated, is:

#!/usr/bin/env perl

use v5.36;     # was 'use 5.010;'
#use warnings; # unnecessary now!
#use strict;   # unnecessary since 5.12.
no warnings 'experimental::for_list';
#no if $] >= 5.018, warnings =< "experimental::smartmatch";
use Getopt::Std;
my %opts;
getopts( 'hvi:', \%opts ) or die "getopts failed\n";
my $verbose;
my $infile;
for my ( $k, $v ) ( %opts ) {
    state $dispatch = {
	h => sub { ...; },
	v => sub { ++$verbose; },
	i => sub { $infile = $_[1]; },
    };
    my $code = $dispatch->{$k}
	or die "*** BUG: no handler for -$k.\n";
    $code->( $k, $v );
}

Note that the subroutines in the dispatch table do not close over your loop variables, so you must pass them in explicitly.

Replies are listed 'Best First'.
Re^2: v5.36 syntax error around given/when
by ibm1620 (Hermit) on Jun 10, 2022 at 18:18 UTC
    Interesting!

    I made the following changes:

    #!/usr/bin/env perl use v5.36; no warnings 'experimental::for_list'; use Getopt::Std; my %opts; getopts( 'hvi:', \%opts ) or die "getopts failed\n"; my $verbose; my $infile; for my ( $k, $v ) ( %opts ) { state $dispatch = { h => sub { say 'HELP!'; exit }, v => sub { ++$verbose; }, i => sub ($file) { $infile = $file; }, }; my $code = $dispatch->{$k} or die "*** BUG: no handler for -$k.\n"; $code->( $v ); } say "infile = ", $infile // 'undefined';
    I couldn't see any reason to pass $k into the sub; also, made use of sub signature.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11144660]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-03-28 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found