如果两个字符串都有空格或都没有空格,那么就执行一些操作。
my $with_spaces = $a =~ / / and $b =~ / /;
my $no_spaces = $a !~ / / and $b !~ / /;
if ($with_spaces or $no_spaces) {
dosomething();
}
但是这个代码给出了一个错误:
在void上下文中使用负模式绑定(!~(是无用的。
我这里做错了什么吗?
行:
my $with_spaces = $a =~ / / and $b =~ / /;
my $no_spaces = $a !~ / / and $b !~ / /;
相当于:
(my $with_spaces = $a =~ / /) and ($b =~ / /);
(my $no_spaces = $a !~ / /) and ($b !~ / /);
使用&&
而不是and
,或者添加括号来更改优先级:
my $with_spaces = $a =~ / / && $b =~ / /;
my $no_spaces = ($a !~ / / and $b !~ / /);