in reply to Re: Difference b/w my and local var?
in thread Difference b/w my and local var?

For clarification of what "doesn't create a variable" means, the following examples might be interesting/surprising (they were for me when I first realized what was happening).

This is rather OT to the OP's question and also rather esoteric. If one doesn't use aliases or tied variables or directly manipulate symbol tables (stashes) then the distinction/clarification may not be helpful/relevant.

After reading local, Temporary Values via local() and other descriptions of local I thought

"A local just gives temporary values to global (meaning package) variables. It does not create a local variable."

meant that the existing variable was used and a new value was assigned to it, as if I did an assignment myself. So that the effect would be somewhat like the following example.

#!/usr/bin/perl -w use strict; use warnings; use Devel::Peek; our $x = 10; print "\$x:\n"; Dump($x); *y = \$x; print "\$main::y:\n"; Dump($main::y); { my $tmp = $x; $x = 20; print "\nIn block with local \$x\n"; print "\$x:\n"; Dump($x); print "\$main::x (just to note that \$x and \$main::x are the same + thing):\n"; Dump($main::x); print "\$main::y:\n"; Dump($main::y); $x = $tmp; } print "\nAfter exiting the block with local \$x\n"; print "\$x:\n"; Dump($x); print "\$main::y:\n"; Dump($main::y);

Which produces

$x: SV = IV(0x9a32ac0) at 0x9a1775c REFCNT = 1 FLAGS = (IOK,pIOK) IV = 10 $main::y: SV = IV(0x9a32ac0) at 0x9a1775c REFCNT = 2 FLAGS = (IOK,pIOK) IV = 10 In block with local $x $x: SV = IV(0x9a32ac0) at 0x9a1775c REFCNT = 2 FLAGS = (IOK,pIOK) IV = 20 $main::x (just to note that $x and $main::x are the same thing): SV = IV(0x9a32ac0) at 0x9a1775c REFCNT = 2 FLAGS = (IOK,pIOK) IV = 20 $main::y: SV = IV(0x9a32ac0) at 0x9a1775c REFCNT = 2 FLAGS = (IOK,pIOK) IV = 20 After exiting the block with local $x $x: SV = IV(0x9a32ac0) at 0x9a1775c REFCNT = 2 FLAGS = (IOK,pIOK) IV = 10 $main::y: SV = IV(0x9a32ac0) at 0x9a1775c REFCNT = 2 FLAGS = (IOK,pIOK) IV = 10

Notice that in this example the SV holding the value of the variable is at address 0x86be75c. The value changes ("IV = 10" or "IV = 20") but the same SV (the data structure Perl uses to hold the value of a variable) is used. In this case I have manually saved the original value, set a new value and, before leaving the block, restored the original value.

Note also that I have created an alias to $x: $y, and that this alias refers to the same SV as $x throughout and therefore sees the same "value" as $x throughout.

Doing this manually is problematic if there are many ways to leave the block. Local ensures that the original value is restored when the block/scope is exited regardless of how it is exited.

Now see what local does.

#!/usr/bin/perl -w use strict; use warnings; use Devel::Peek; our $x = 10; print "\$x:\n"; Dump($x); *y = \$x; print "\$main::y:\n"; Dump($main::y); { local $x = 20; print "\nIn block with local \$x\n"; print "\$x:\n"; Dump($x); print "\$main::x (just to note that \$x and \$main::x are the same + thing):\n"; Dump($main::x); print "\$main::y:\n"; Dump($main::y); } print "\nAfter exiting the block with local \$x\n"; print "\$x:\n"; Dump($x); print "\$main::y:\n"; Dump($main::y);

Which produces

$x: SV = IV(0x9575ac0) at 0x955a75c REFCNT = 1 FLAGS = (IOK,pIOK) IV = 10 $main::y: SV = IV(0x9575ac0) at 0x955a75c REFCNT = 2 FLAGS = (IOK,pIOK) IV = 10 In block with local $x $x: SV = IV(0x9575ac4) at 0x9559c28 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 20 $main::x (just to note that $x and $main::x are the same thing): SV = IV(0x9575ac4) at 0x9559c28 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 20 $main::y: SV = IV(0x9575ac0) at 0x955a75c REFCNT = 3 FLAGS = (IOK,pIOK) IV = 10 After exiting the block with local $x $x: SV = IV(0x9575ac0) at 0x955a75c REFCNT = 2 FLAGS = (IOK,pIOK) IV = 10 $main::y: SV = IV(0x9575ac0) at 0x955a75c REFCNT = 2 FLAGS = (IOK,pIOK) IV = 10

Note that now while $x has the same new value as in the previous example (20), the new value is stored in a new SV at a different address from the original SV. The original SV still exists and the alias $y still refers to the original SV, which still has the original value (10).

Of course, if "value" refers to the SV itself and not the value within the SV (the value of the value???) then my terminology above is incorrect, the "variable" simply has a new "value" (i.e. SV) and there is indeed no new "variable". And here lies the potential for misunderstanding of the description

A local just gives temporary values to global (meaning package) variables. It does not create a local variable.

In the network of symbol tables (stashes), GVs, SVs, IVs, NVs, PVs, RVs, etc. maintained by perl, what exactly is the "variable" and what is the "value"? The documentation is often not explicit and it is easy to make assumptions that are inconsistent with those of the documentation or the behaviour of perl. Doing so may lead to confusion/surprise/consternation/errors.