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 = [];
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is because the
forloop treats contents of@$aas lvalues–something that you can assign to. Remember thatforaliases the contents of the array to$_. It appears that the act of looking for aliasable contents in@$a, is sufficient to cause autovivification, even when there are no contents to alias.This effect of aliasing is consistent, too. The following also lead to autovivification:
map {stuff} @$a;grep {stuff} @$a;a_subroutine( @$a);If you want to manage autovivification, you can use the eponymous pragma to effect lexical controls.