<?xml version="1.0" encoding="windows-1252"?>
<node id="788301" title="Re^3: Local for lexicals" created="2009-08-13 10:10:26" updated="2009-08-13 10:10:26">
<type id="11">
note</type>
<author id="300037">
Roy Johnson</author>
<data>
<field name="doctext">
Well, you could declare $x &lt;i&gt;within&lt;/i&gt; the call to lambda, instead of surrounding it:
&lt;c&gt;
#!perl
use strict;
use warnings;

sub lambda {
  my $fn = pop;
  my @foo = @_; # foo references $x, etc.
  sub {
    # Put values into referenced params
    ${$foo[$_]} = $_[$_] for 0..$#_;
    &amp;$fn;
  }
}

my $x = 3;

my $closure = lambda do {
  my ($x, $y);
  \($x, $y) =&gt; sub { print "Got $x and $y\n" }
} ;

$closure-&gt;($_, $_+1) for (1..4);

print "And my X is $x\n";
&lt;/c&gt;
Though I suspect that's a distinction that won't be seen as advantageous. It's just as easy to write
&lt;c&gt;
my $closure = sub {
  my ($x, $y) = @_;
  print "Got $x and $y\n";
};
&lt;/c&gt;
which is the Perlish way to do what the OP wants to do.
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-300037"&gt;
&lt;hr&gt;
&lt;small&gt;&lt;b&gt;Caution:&lt;/b&gt; Contents may have been coded under pressure.&lt;/small&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
787348</field>
<field name="parent_node">
788212</field>
</data>
</node>
