I am confused with the difference in the outputs of the following two snippets :
SNIPPET 1
use Data::Dumper;
my $q = (
{ q=>1, w=>2 },
{ i=>8 },
);
print "\nOUTPUT_1 $q=[".$q."] ";
print "\nOUTPUT_2 ".Data::Dumper::Dumper ($q);
$q[0]->{a} = "5";
print "\nOUTPUT_3 ".Data::Dumper::Dumper ($q);
OUTPUT
OUTPUT_1 HASH(0x872de10)=[HASH(0x872de10)]
OUTPUT_2 $VAR1 = {
'i' => 8
};
OUTPUT_3 $VAR1 = {
'i' => 8
};
SNIPPET 2
use Data::Dumper;
my $q = (
{ q=>1, w=>2 },
);
print "\nOUTPUT_1 $q=[".$q."] ";
print "\nOUTPUT_2 ".Data::Dumper::Dumper ($q);
$q[0]->{a} = "5";
print "\nOUTPUT_3 ".Data::Dumper::Dumper ($q);
OUTPUT
OUTPUT_1 HASH(0x81861bc)=[HASH(0x81861bc)]
OUTPUT_2 $VAR1 = {
'w' => 2,
'q' => 1
};
OUTPUT_3 $VAR1 = {
'w' => 2,
'q' => 1
};
I saw the second snippet being used in a production code. I tried to expand it in a way shown in Snippet 1 but failed.
MY UNDERSTANDING
When an array is assigned to a scalar, its count gets stored in that scalar. I believed that this holds good irrespective of the contents of the array.
I do not want a solution here but I want to correct my understanding.