Musing on Mark Jason Dominus's Seven Useful Uses of local, I think I've come up with another one.
The most common use I have for local is with %ENV. The article doesn't specifically mention environment variables. There are instances where I am doing one of the following:
Forking several times with %ENV{FOO} set to different values, and I need to keep its original or unset value in the parent.
Writing tests for something that reads an environment variable (see Module::Optional for an example)
Opening Tk top level windows on multiple displays (see Re: Tk - is MainWindow robust?)
Thinking about it more, I would suggest that @ARGV belongs in the same category, as used in the file slurp idiom:
my $slurp = do { local (@ARGV, $/) = 'foo.txt'; <>};
There's also localising @ARGV for handling INCLUDE directives in the input stream.
These uses of local could be thought of as a special case of number 6 (Dynamic Scope Revisited), or possibly number 1 (Special Variables), but I think they are a new category.
Update:Looks like there might be a problem with plover.com - try this link as an alernative for MJD's article.
--
Oh Lord, won’t you burn me a Knoppix CD ?
My friends all rate Windows, I must disagree.
Your powers of persuasion will set them all free,
So oh Lord, won’t you burn me a Knoppix CD ?
(Missquoting Janis Joplin)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: An eighth use of local
by Ovid (Cardinal) on Dec 01, 2005 at 19:27 UTC | |
Re: An eighth use of local
by Perl Mouse (Chaplain) on Dec 01, 2005 at 15:00 UTC | |
Re: An eighth use of local
by itub (Priest) on Dec 01, 2005 at 16:06 UTC | |
by Perl Mouse (Chaplain) on Dec 01, 2005 at 16:20 UTC | |
by xdg (Monsignor) on Dec 01, 2005 at 16:51 UTC | |
by ikegami (Patriarch) on May 19, 2006 at 16:33 UTC | |
by itub (Priest) on Dec 02, 2005 at 02:04 UTC | |
Re: An eighth use of local
by jkeenan1 (Deacon) on Dec 02, 2005 at 13:00 UTC |