Perhaps I'm being too simple-minded, but please consider this variant:
use strict qw(vars subs);
my $name;
{
my $name = 'red';
*$name = sub { "<FONT COLOR='$name'>@_</FONT>" };
}
{
my $name = 'blue';
*$name = sub { "<FONT COLOR='$name'>@_</FONT>" };
}
$name = '<none>';
print red(), "\n";
print blue(), "\n";
__END__
<FONT COLOR='red'></FONT>
<FONT COLOR='blue'></FONT>
Without the additional block delimiters (
{}) and
my declarations, the variable
$name indeed always refers to the same storage (which, I admit, was not clear in my own mind when I wrote the above comment). Closures (
red() and
blue() here) only get a copy of a portion of their lexical environment (that is,
become "closures", properly so-called) when they escape a region where that portion is (lexically, of course) in scope. Without a block that the thread of execution leaves, no
closure is created and the code is indistinguishable from a regular sub.
Note that
red() and
blue() (i.e.
&red and
&blue) exist outside the blocks because they are created in the package's (here, main) symbol table -- as using a type glob (
*) always implies.
Here's the code with another wrinkle to illustrate:
use strict qw(vars subs);
{
my $name;
{ my $name = 'red';
*$name = sub { "<FONT COLOR='$name'>@_</FONT>" }; }
{ my $name = 'blue';
*$name = sub { "<FONT COLOR='$name'>@_</FONT>" }; }
*foo = sub {
print red($name), "\n";
print blue($name), "\n";
};
$name = 'another wrinkle';
}
foo();
print red('hi mom');
__END__
<FONT COLOR='red'>another wrinkle</FONT>
<FONT COLOR='blue'>another wrinkle</FONT>
<FONT COLOR='red'>hi mom</FONT>
-
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.