It appears to be possible by messing with the opcode tree (based on what Data::Alias is capable of doing), but it's not possible in Perl.
Update: The following would also do if you could localise through a reference:
sub lambda {
my $f = $_[1];
my $xr = \$_[0];
return sub {
local $$xr = $_[0];
return $f->();
}
}
So how about emulating local using alias:
use Data::Alias qw( alias );
use Object::Destroyer qw( );
sub lambda {
my $f = $_[1];
my $xr = \$_[0];
return sub {
alias my $temp = $$xr;
alias $$xr = $_[0];
my $sentry = Object::Destroyer->new(
sub { alias $$xr = $temp; }
);
return $f->();
}
}
This should work whether $x is a lexical or a package variable. This should work whether $x is tied or not. In fact, it works better than local with tied variables and creates an alias unlike local.
Untested.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.