Here a meditation how to implement most of it in a pure functional and backwards compatible way:

NB:

the basic ideas are:

use v5.12; use warnings; use subs qw/break/; #use Data::Dump qw/pp dd/; #use diagnostics; say "========= IMPLEMENTATION"; { my $check; sub check(&) { $check = shift; } sub case { my $code; if (ref $_[-1] eq "my_break") { #warn "my break"; $code = pop @_; } my $do; for my $val (@_) { $do ||= $check->($val); } if ($do){ if ($code) { once($code) } else { return $do } } return; } no warnings 'exiting'; sub once (&) { (shift)->(); next; } sub break(&) { return bless shift, "my_break" } } say "========= TESTS"; say "--- with basic logic"; for ( 0 .. 5 ) { $_< 1 and once { print "$_ smaller 1"}; $_< 3 and once { print "$_ smaller 3"}; $_ > 3 && $_ < 5 and once { print "$_ between 3 and 5"}; print "$_ has no match"; # else } continue { print "\n"; } say "--- with some syntactic sugar"; for ( 0 .. 5 ) { check { $_ < shift }; case 1 and once { print "$_ smaller 1"}; case 3 and once { print "$_ smaller 3"}; case 4,5 and once { print "$_ smaller 4 or 5"}; print "$_ has no match"; # else } continue { print "\n"; } say "--- with even MORE syntactic sugar"; for ( 0 .. 5 ) { check { $_ < shift }; case 1 => break { print "$_ smaller 1"}; case 3 => break { print "$_ smaller 3"}; case 4,5 => break { print "$_ smaller 4 or 5"}; print "$_ has no match"; } continue { print "\n"; }

C:/Strawberry/perl/bin\perl.exe -w d:/tmp/pm/do_once.pl ========= IMPLEMENTATION ========= TESTS --- with basic logic 0 smaller 1 1 smaller 3 2 smaller 3 3 has no match 4 between 3 and 5 5 has no match --- with some syntactic sugar 0 smaller 1 1 smaller 3 2 smaller 3 3 smaller 4 or 5 4 smaller 4 or 5 5 has no match --- with even MORE syntactic sugar 0 smaller 1 1 smaller 3 2 smaller 3 3 smaller 4 or 5 4 smaller 4 or 5 5 has no match Compilation finished at Sat Jul 3 18:15:36

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery


In reply to Re: Modern exposition of "case" or Switch in perl 5.34? (RFC) by LanX
in thread Modern exposition of "case" or Switch in perl 5.34? by symgryph

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.