in reply to scope of an autovivified variable?

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.

Replies are listed 'Best First'.
Re^2: scope of an autovivified variable?
by rmcgowan (Sexton) on May 11, 2011 at 20:37 UTC

    OK, but I'll have to take your word regarding Devel::Peek and it's output. I have no idea what it means (for now).

    What I do know is this:

    #!/usr/local/bin/perl -w use strict; use Data::Dumper; sub f { my $x; print Dumper($x); $x="abc"; } f; f;

    And the output is:

    $VAR1 = undef; $VAR1 = undef;

    Whether the storage space is deallocated or not seems to be irrelevant with respect to general use. I can't get back to the original variable the second time around. Or at least, that's what it looks like. Perhaps some additional discussion about what you mean would help my feeble understanding.

      Perhaps some additional discussion about what you mean would help my feeble understanding.

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

      That's not true, because the array is lexical. Whatever you think is happening doesn't change the scope of the variable.

      I can't get back to the original variable the second time around.

      You say "variable" when you mean "value".

      Updated based on better understanding of the question.

        You're right, I meant value.

        Specifically, I don't understand what you mean by "You are mistaken in your belief that lexical variables get deallocated at the end of their scope." When a variable goes "out of scope", I thought it meant both the value it referred to and that specific variable name were no longer available to the program. Put another way:

        if($x) { my $y; ... } $y ...

        The second $y has nothing to do with the one inside the 'if'. Outside of the 'if', I have no way to get to the 'inside the if' $y or its value. At least, that was my understanding. So, I've been doing things like:

        use strict; my $c = 0; while(<>) { chomp; $c++; my $a; print "1: $a iteration $c\n" if $a; $a = $_; print "2: $a iteration $c\n" if $a; }

        And expecting that, every time around the loop, the value for $a from any prior loop would have been forgotten. Which means the first 'print' would never run. What you're saying makes me think this is not true, even though experience shows it works. Therefore, there's something about what you said that I'm not understanding.

        I'm also not at all familiar with 'Devel::Peek' and have no idea how to interpret the output you supplied, which just adds to the confusion.

        I just looked at the link supplied (static variable hack) and this looks like the root cause of my confusion.

        It will take a little time to be sure but it is a much better place to be than where I was ;)

        Many thanks for your patience and the reference.