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)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|