Why does Perl autovivify in this case?

前端 未结 4 1279
清酒与你
清酒与你 2020-12-10 19:45

Why does $a become an arrayref? I\'m not pushing anything to it.

perl -MData::Dumper -e \'use strict; 1 for @$a; print Dumper $a\'
$VAR1 = [];
<         


        
4条回答
  •  伪装坚强ぢ
    2020-12-10 20:34

    When you treat a scalar variable whose value is undef as any sort of reference, Perl makes the value the reference type you tried to use. In this case, $a has the value undef, and when you use @$a, it has to autovivify an array reference in $a so you can dereference it as an array reference.

提交回复
热议问题