空リストの評価が変だとか&&の意味とか

会社で、こんな挙動を見つけてる人がいた。

% perl -MData::Dumper -le 'my @x = (); @x = () && (); print Dumper \@x;'
$VAR1 = [
          undef
        ];

% perl -MData::Dumper -le 'my @x = (); @x = @x && (); print Dumper \@x;'
$VAR1 = [
          0
        ];


配列がスカラーで評価された?

&&

今まで&&をcのそれと同じだと思っていた。boolを返すと。

% perldoc perlop
       C-style Logical And

       Binary "&&" performs a short-circuit logical AND operation.  That is, if the left operand is false, the right operand is not
       even evaluated.  Scalar or list context propagates down to the right operand if it is evaluated.

       C-style Logical Or

       Binary "││" performs a short-circuit logical OR operation.  That is, if the left operand is true, the right operand is not even
       evaluated.  Scalar or list context propagates down to the right operand if it is evaluated.

       The "││" and "&&" operators return the last value evaluated (unlike C’s "││" and "&&", which return 0 or 1). 

&&は論理積だよ。左辺値が偽なら右辺値は評価されないよ。右辺値が評価されたらスカラーかリストを(以下わかんね)

論理和だよ。左辺値が真なら右辺値は評価されないよ。右辺値が評価されたらスカラーかリストを(以下わかんね)
と&&は最後に評価した値を返すよ(C言語のそれとは違うよ。あっちは0か1を返す)。


へえ。
というわけでえらい人に
「お前がProgramming Perl読んでないことは分かった」
と看破されてきた。


OK理解した、と言ったところで柔道家がtipsを。

if (1,2,0) {print 'hoge'} 表示されません。

なんと最後に評価されるのは0だと。


続き。

(1,2,0) && (1,2,3); は何が返るでしょう?

undefと答える分かってない俺。
「最後に評価した値」を返すのだから、答えは0である。


OK、今度こそ分かりました。
・・・はず。