I caused a bit of a problem for myself in some code when I did this:

use strict; # forgot this in the original ;( outer_loop { my $headerStr; ... # some code that may or may not initialize $headerStr my @headerElements = split /\s+/, $headerStr if $headerStr; if(! @headerElements) { # Create content for use later. $headerElements[0] = 'some value'; $headerElements[1] = 'some value'; $headerElements[2] = 'some value'; } $headerElements[2] =~ s/[<>,]//g; ... # other checks and tests of the array content follow. }

Of course, there were problems ;), since my @headerElements only processes if $headerStr is set.

When it's not set, the lexical scoping of the array does not happen. So the assignments to the array need to autovivify the array. I assume this is what happens, since there is no complaint from Perl and something does appear in the array.

I also assume the scope of this autovivified array is global, since data is getting carried over from one iteration of the outer loop to the next, when it wouldn't if the array were lexical.

I can't find any reference in the Perl docs I've searched to support the global scope conjecture. Can anyone point me to anything about this?

Thanks.


In reply to scope of an autovivified variable? by rmcgowan

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.