我很高兴得知 Perl 可以在 5.32 版本中处理链式比较
但是,我正在尝试进行链式正则表达式比较,以使我的代码更短、更清晰
#!/usr/bin/env perl
use 5.032;
use strict;
use warnings FATAL => 'all';
use feature 'say';
use autodie ':all';
if (9 > 2 < 3 < 4 > 0) {
say 'chained expressions work.'
} else {
say 'chained expressions do not work.'
}
my $x = 4;
my $z = 4;
if ($x == 4 == $z) {
say 'chained equality works';
}
$x = 'x';
$z = 'x';
if ($x eq 'x' eq $z) {
say 'chained string comparisons work.';
}
$x = '.';
$z = './.';
if ($x =~ m/\./ =~ $z) { # unfortunately this doesn't work
# equivalent of `if ( ($x =~ m/\./) && ($z =~ m/\./)) {
say 'chained regex works.';
} else {
say 'no chained regex.';
}
如何正确使用正则表达式进行链式比较?