为什么 `=~` 在列表上下文中的行为不同于 `eq` 和 `==`?

Why does `=~` behave differently than `eq` and `==` in list context?

考虑

my @array = ( 'x' =~ /y/, 'x' eq 'y', 1 == 2 );

my %hash = ( 'a', 'x' =~ /y/, 'b', 'x' eq 'y', 'c', 1 == 2 );

使用 Data::Dumper,我们看到 =~ 的行为不同于 eq==

\@array = [
            '',
            ''
          ];
\%hash = {
           'a' => 'b',
           '' => undef
         };

具体来说,上面的代码片段似乎被解释为

my @array = ( 'x' eq 'y', 1 == 2 );

my %hash = ( 'a', 'b', 'x' eq 'y', 'c', 1 == 2 );

有人可以为这种可以说是意外的行为提供解释吗?

您使用的匹配操作不仅是找出是否匹配(标量上下文),而且还给出匹配结果(列表上下文)。引用自 the documentation:

Matching in list context
m// in list context returns a list consisting of the subexpressions matched by the parentheses in the pattern, that is, (, , ...) (Note that here etc. are also set). When there are no parentheses in the pattern, the return value is the list (1) for success. With or without parentheses, an empty list is returned upon failure