DEV Community

Discussion on: Yet Another Perl Switch Statement

Collapse
 
yukikimoto profile image
Yuki Kimoto

Do you know Syntax::Keyword::Match? If you are interested in switch statement, you can get inspiration from this module.

use v5.14;
use Syntax::Keyword::Match;

my $n = ...;

match($n : ==) {
   case(1) { say "It's one" }
   case(2) { say "It's two" }
   case(3) { say "It's three" }
   case(4), case(5)
           { say "It's four or five" }
   default { say "It's something else" }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
matthewpersico profile image
Matthew O. Persico

No I did not, but I will investigate. Thank you.

Collapse
 
matthewpersico profile image
Matthew O. Persico

Not exactly what I'd want. I'd rather see:

match($n) {
   case(1 : ==) { say "It's one" }
   case(2 : ==) { say "It's two" }
   case(3 : ==) { say "It's three" }
   case(4 : >=) { say "It's four or greater" }
   default { say "It's something else" }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
yukikimoto profile image
Yuki Kimoto

If you want to write so, Syntax::Keyword::Match is insufficient.