in reply to Re^2: Scope surprise
in thread Scope surprise
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:
You don't know what value $foo had, and if you want to restore the value it originally had, local comes in very handy.$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 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); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Scope surprise
by ikegami (Patriarch) on Apr 11, 2009 at 20:53 UTC |