Acme-Octarine
view release on metacpan or search on metacpan
Octarine.pm view on Meta::CPAN
use B::Deparse;
use PadWalker 'closed_over';
# Frustratingly, he uses a package lexical %r, which foils a simple re-eval of
# the deparsed method code.
my $deparse = B::Deparse->new("-sC");
my $body = $deparse->coderef2text(\&Acme::Colour::new);
my $r = closed_over(\&Acme::Colour::new)->{'%r'};
# Add a my $sub; declaration at the top level
$body =~ s/([ \t]+)(bless)/$1my \$sub;\n$1$2/ or die $body;
# If colour is defined, look it up in the specials hash
$body =~ s/
([ \t]+) # Must get the indent correct
(unless[ \t]*\(exists[ \t]*\$r)({\$colour})\)
/$1\$sub = \$Acme::Colour::specials{\$colour};
$1$2->$3 or defined \$sub)/sx or die $body;
# If a specials subroutine was found, call it instead of making a simple return
$body =~ s/
([ \t]+) # Most get the indent correct
( run in 0.782 second using v1.01-cache-2.11-cpan-de7293f3b23 )