Did you just recommend ... over ...
Yes I did. Not for this particular example, but examples (made up in just a few minutes) are always simpler than real life situations. Anyway, the basic idea here was that the OP could get exactly the result he was hoping for with almost the exact code he originally used, and understands so well. That is remarkable enough to warrant pointing it out.

A more complex, and likely more true to life example could be this:

$foo = bar(@params); # You don't know what value $foo has { local $foo = 1; foo(); } sub foo { print "foo: $foo\n"; } # now $foo is restored!
You don't know what value $foo had, and if you want to restore the value it originally had, local comes in very handy.

You may not even know if it matters whether $foo is restored, and you don't even have to know. You can just use local anyway, just to be on the safe side. That kind of localization of side effects is a very good tool to have in the toolbelt.

Either way, sounds like an awfully complicated way of avoiding passing a parameter.
Not a parameter. A setting. For example, suppose you temporarily want to change the settings for a module like Data::Dumper, and not disturb its value for everywhere else besides this piece of code:
{ local $Data::Dumper::Terse = 1; $baz = Dumper($data); }

In reply to Re^3: Scope surprise by bart
in thread Scope surprise by smcdonald

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.