I also assume the scope of this autovivified array is global,

It's not autovivified. It is explicitly created by my. Note that my does this at compile-time.

my @headerElements = split /\s+/, $headerStr if $headerStr;

«my ... if ...;» is not valid code. One cannot use a variable declared using my without first executing that my. This example is specifically documented in perlsyn ("NOTE: The behaviour ...").

The documentation is a bit suboptimal, since it doesn't mention «... and my ...;», «... ? my ... : ...;», etc,

I also assume the scope of this autovivified array is global

No, it's lexically scoped. Scope refers to where a variable is visible, accessible. That's why state variables are said to be lexically scoped too.

data is getting carried over from one iteration of the outer loop to the next, when it wouldn't if the array were lexical.

You are mistaken in your belief that lexical variables get deallocated at the end of their scope.

In the following, in the second call of f(), you can see effects the first call of f() had on $x:

$ perl -MDevel::Peek -e'sub f { my $x; Dump($x); $x="abc"; } f;f;' SV = NULL(0x0) at 0x93fcee0 REFCNT = 1 FLAGS = (PADMY) SV = PV(0x93eb040) at 0x93fcee0 REFCNT = 1 FLAGS = (PADMY) PV = 0x93f8e00 "abc"\0 CUR = 3 LEN = 4

Update: Fleshed out some details.


In reply to Re: scope of an autovivified variable? by ikegami
in thread 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.