In this example, note how the references remain the same and the array contents are overwritten:
$ perl -wE 'for (0 .. 1) { my @x = $_ == 0 ? qw{a b} : qw{c d e}; say +\@x; say @x; }' ARRAY(0x100a1d40) ab ARRAY(0x100a1d40) cde
When you enter the loop, the array comes into existence and the reference count is set to 1. When you exit the loop, it goes out of scope and no longer exists; the reference count drops back to zero. With the reference count at zero, the reference can be reused on each subsequent iteration.
This seems to emulate what you are referring to in your description.
If you store the array reference in a variable that was created outside of the loop, the reference count is incremented again (i.e. to 2). When you exit the loop, the reference count drops down to 1. As the reference count is not zero, a new reference is created on each iteration. Here's another example showing that:
$ perl -wE 'my %h; for (0 .. 1) { my @x = $_ == 0 ? qw{a b} : qw{c d e +}; $h{$_} = \@x; say \@x; say $h{$_}; say @x; }' ARRAY(0x100a1d10) ARRAY(0x100a1d10) ab ARRAY(0x10053c88) ARRAY(0x10053c88) cde
Here the data is not overwritten: a new array is used each time.
This seems to emulate what is going on in your code.
It's difficult to be sure exactly what's going on here as you've shown insufficent code and no output.
Given that your description and your code appear to be at odds with each other, I wonder whether you updated one without updating the other.
-- Ken
In reply to Re: How to create an anonymous array for lines in a file and store a reference to that array in the hash
by kcott
in thread How to create an anonymous array for lines in a file and store a reference to that array in the hash
by iphone
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |