Greetings bretheren and sisteren,

I was just sitting there, writing some Perl, when I was slapped by confussion. I have this array, I'll call it

my @array = ();
which I want to build into a linked list sort of structure. For the sake of a simple posting I'll make it more of a linked shrub like this.
$array[1] = { val => "The initial value for 1", one => 'undef' }; $array[0] = { val => "The initial value for 0", one => \$array[1]{val} + }; $array[2] = { val => "The initial value for 2", one => \$array[1]{val} + }; $array[3] = { val => "The initial value for 3", one => \$array[1]{val} + };
So that all of the links just point to the '1' element of my array. This seems to work the way I wanted. If I print it out with this code
for my $i (0 .. 3) { print "# $array[$i]{one}\n"; if (ref $array[$i]{one} eq 'SCALAR') { print "#\t${$array[$i]{one}}\n"; } }
I get
# SCALAR(0x8114938) # The initial value for 1 # undef # SCALAR(0x8114938) # The initial value for 1 # SCALAR(0x8114938) # The initial value for 1
So all the references have the same address and value. Great.
But no one would initialize an array like this, so I do it in a loop
for my $i (0 .. 3) { $array[$i] = { val => rand(time)%3, one => \$array[1]{val} }; }
D'oh! Now things seem to be broken. The same section of code for printing
print "\n"; for my $i (0 .. 3) { print "# $array[$i]{one}\n"; if (ref $array[$i]{one} eq 'SCALAR') { print "# \t${$array[$i]{one}}\n"; } }
gives me the irritating output of
# SCALAR(0x8114938) # The initial value for 1 # SCALAR(0x8114938) # The initial value for 1 # SCALAR(0x81adcd8) # 0 # SCALAR(0x81adcd8) # 0
Notice that the references have changed after reseting the value for @array[ 1 ]. So now $array[ 0 ]{one} points to the old value and the last 2 elements of the array point to the new value. The whole idea here was to have a reference to the value of $array[ 1 ]{val} so that other nodes would always have the current value, but that's broken.

What is happening here?

I suspect that this has to do with scoping inside the loop's block and that I'm creating something like a closure. Can anyone confirm or deny this?

I don't have any great project hanging on this, in fact I have a working version thanks to merlyn's node on using push for this, but it's bugging me that I don't understand why this doesn't work.

Thanks for your time,
Peter C.,

"It's not the fact that I have the monkeys that gives me the power, it's that I'll turn the monkeys loose that gives me the power."
~Kids in the Hall


In reply to Scope when initializing with references in a loop by IraTarball

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.