G'day haukex,

What you've described here, with respect to local, is pretty much how I've always understood this to work: no dark arts or black magic; effectively, just a simple pushing and popping of a stack.

In a subsequent reply, you referenced "perlsub: Temporary Values via local()" and then wrote: "Without some reassurance from the docs, one might worry that the temporary variables might somehow "go away" when the scope exits ...". The following code tracks %h through a do block (similar to your code); in addition, do contains an anonymous block which creates new temporary values. It also tracks the lexical $r: declared but uninitialised before do; assigned a value within the anonymous block; and shown to retain that value after do.

#!/usr/bin/env perl use strict; use warnings; use Data::Dump; my $r; print "\$r UNDEF\t"; dd $r; our %h = (z => 26); print "%h BEFORE do\t"; dd \%h; my $x = do { print "PRE local\t"; dd \%h; local %h; print "POST local\t"; dd \%h; %h = (a => 1); print "POST assign\t"; dd \%h; print "%h BEFORE anon\t"; dd \%h; { print "PRE local2\t"; dd \%h; local %h; print "POST local2\t"; dd \%h; %h = (b => 2); print "POST assign2\t"; dd \%h; $r = \%h; print "\$r ASSIGNED\t"; dd $r; } print "%h AFTER anon\t"; dd \%h; \%h; }; print "%h AFTER do\t"; dd \%h; print "\$x AFTER do\t"; dd $x; print "\$r FINAL\t"; dd $r;

Output:

$r UNDEF undef %h BEFORE do { z => 26 } PRE local { z => 26 } POST local {} POST assign { a => 1 } %h BEFORE anon { a => 1 } PRE local2 { a => 1 } POST local2 {} POST assign2 { b => 2 } $r ASSIGNED { b => 2 } %h AFTER anon { a => 1 } %h AFTER do { z => 26 } $x AFTER do { a => 1 } $r FINAL { b => 2 }

Hopefully, that's a bit more reassuring. :-)

— Ken


In reply to Re: Referencing localized variables, and typeglobs by kcott
in thread Referencing localized variables, and typeglobs by haukex

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.