What I want to do is write a wrapper around B::Deparse::coderef2text
that will properly resolve closures, so that
$newcoderef = eval 'sub '.ImprovedCoderef2text($oldcoderef);
will produce equivalent code in more circumstances. coderef2text
greatly simplifies source code filtering by providing a standardized
and more predictable syntax that raw perl code.
In the specific case, I'm debugging a tailrecursion optimizer that
rewrites "return(f(@args))" into "shift while @_; @_=@args; goto &f"
and I would like it to not break due to lexical scoping issues.
adding ourvars to the result of closed_over would be the easiest
for _me_ but that clearly would not be backward-compatible in case
someone is relying on closed_over leaving ourvars out. Listing them
both, as is done with peek_*, would work as well.
I'm busy and lazy and haven't cracked the source on PadWalker --
a "with" pragma that explicitly creates a pad loaded with the
variables in a hash was one of my perl6 RFCs -- a comprehensive
closed_over function (closed_over_all()?) -- the comprehensive
closed_over would be able to provide a way to carry around a
lexical environment for use with the hypothetical C<with> construct.
If you have enough coffee in you to implement "with" while wearing
your public service volunteer hero hacker hat, that would be
really really cool from the point of view of my function rewriter,
as I could just do something like
use B::Deparse 'coderef2text';
my $deparse = B::Deparse->new(... flags go here);
use PadWalker 1.1 qw/outer_lexicals/; #instead of "closed_over"
sub CodeFilter {
my ($cr,$filter) = @_;
my $initialtext = $deparse->coderef2text($cr);
my $lexicalenvironment = outer_lexicals($cr);
my $filteredtext = $filter->($initialtext);
dowith { return eval "sub $filteredtext" } $lexicalenvironment ;
}
instead of having to go through the filtered text and identify all
symbols that exist as keys in %$lexicalenvironment and replace
each with a hash lookup expression, which is what I'm currently
considering.
Could outer_lexicals present lexicals that are not actually captured?
I have been under the impression that closures defer destruction of
the whole lexical environment, but that may have been addressed.
On 9/14/06, via RT <bug-PadWalker@rt.cpan.org> wrote:
Show quoted text>
> <URL:
http://rt.cpan.org/Ticket/Display.html?id=21475 >
>
> On Wed Sep 13 18:21:23 2006, DAVIDNICO wrote:
> > it seems clear from the above that our variables are included
> > in closures too. How difficult would it be to include them
> > in the return value of closed_over?
>
> It wouldn't be at all difficult. They aren't included because I couldn't
> think of a case where it would be useful to have them!
>
> I'll be happy to add this feature in some form, if you can convince me
> it's useful. Perhaps you could explain what you want to do with it.
>
> Robin
--
Although efforts are under way to mitigate the problem, this message
may contain flippancy, hyperbole and/or confusing assertions. Please
reply directly to fall2006sigfile@davidnicol.com for clarification of any points
appearing unclear, vague, cruel, frustrating, threatening, negative,
dilletantish or otherwise unprofessional before taking action based on
misintepretation or misconstruction of such points.