如何在Perl中的switch语句中对案例进行分组

kus*_*szi 1 string perl switch-statement

给出一个代码

use Switch;

my $var1x = "one";

switch ($var1x) {
    case "one" { print "Why so small?\n"}
    case "two" { print "Why so small?\n"}
    case "three" { print "That is ok.\n"}
    case "four"  { print "That is ok.\n"}
}
Run Code Online (Sandbox Code Playgroud)

我想将类似案件的实施分组.有任何建议如何在Perl中正确编写它?

Akz*_*lin 5

请不要使用旧的慢速Switch模块.

通常它由CODEREF哈希解决.

my $var1x = "one";

my $is_ok     = sub { print "That is ok.\n"};
my $why_small = sub { print "Why so small?\n" };

my %switch = (
    one   => $why_small,
    two   => $why_small,
    three => $is_ok,
    four  => $is_ok,
    ten   => sub { print "Unbelievable!\n"; },
);

$switch{$var1x}->();
Run Code Online (Sandbox Code Playgroud)