Created
January 23, 2010 06:38
-
-
Save ap/284465 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use 5.010; | |
use strict; | |
use warnings; | |
no warnings qw( once qw ); | |
use Parse::Perl qw( current_environment parse_perl ); | |
use PadWalker qw( peek_my peek_sub ); | |
use Devel::LexAlias qw( lexalias ); | |
my $empty_env = current_environment; | |
my @code = ( 'my $x = 1', 'say $x' ); | |
my @sub; | |
my $lexical; | |
## compile a scope | |
for ( @code ) { | |
my $decl = join ',', keys %$lexical; | |
$decl = "my ($decl);" if $decl; | |
my $sub = parse_perl $empty_env, $decl . $_; | |
$lexical = peek_sub $sub; | |
push @sub, $sub; | |
} | |
## run a scope | |
$lexical->{ $_ } = eval "\\my $_" for keys %$lexical; | |
for my $sub ( @sub ) { | |
my ( $name, $var ); | |
lexalias $sub, $name, $var while ( $name, $var ) = each %$lexical; | |
$sub->(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment