In your case, the difference is that local
addressed a variable in the symbol table and my
does not. This is important because of how to use it:
local @array = @$vname;
That is, you're using $vname
as a symbolic reference (a questionable practice absent no strict 'refs'
to tell us you know what you're doing). Quotha:
Only package variables (globals, even if localized) are visible to
symbolic references. Lexical variables (declared with my()) aren't in
a symbol table, and thus are invisible to this mechanism.
So symbolic references can only refer to variables in the symbol table. Whether you declare @a
as lexical with my
or as global with local
, @$vname
only ever refers to @main::a
. When you say
local @a = qw(1 2 3);
, you are giving a new value to @main::a
. When you say
my @a = qw(1 2 3);
, you are creating a new lexical variable @a
and giving it a value, but leaving @main::a
undefined. When
local @array = @$vname;
then accesses the value of @main::a
, if finds it to be undefined and sets the value of @array
to it.
If all that seems confusing, that's because it is. This is why you are strongly encouraged to use strict
and warnings
(which would have exploded prettily on this code) and discouraged from using symbolic references unless you really know what you're doing.