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