rmcgowan has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: scope of an autovivified variable?
by davido (Cardinal) on May 11, 2011 at 19:55 UTC | |
by rmcgowan (Sexton) on May 11, 2011 at 20:06 UTC | |
by davido (Cardinal) on May 11, 2011 at 20:23 UTC | |
Re: scope of an autovivified variable?
by ikegami (Patriarch) on May 11, 2011 at 20:03 UTC | |
by rmcgowan (Sexton) on May 11, 2011 at 20:37 UTC | |
by ikegami (Patriarch) on May 11, 2011 at 22:15 UTC | |
by rmcgowan (Sexton) on May 11, 2011 at 22:55 UTC | |
by Marshall (Canon) on May 12, 2011 at 15:03 UTC | |
by rmcgowan (Sexton) on May 11, 2011 at 23:05 UTC | |
Re: scope of an autovivified variable?
by TomDLux (Vicar) on May 12, 2011 at 23:10 UTC |