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.


In reply to Re^2: Difference b/w my and local var? by ig
in thread Difference b/w my and local var? by anbutechie

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.