Skip to content

Instantly share code, notes, and snippets.

@regnarg
Created December 28, 2016 14:15
Show Gist options
  • Save regnarg/1096ad4d85e430ef2fc3c57a0e85600a to your computer and use it in GitHub Desktop.
Save regnarg/1096ad4d85e430ef2fc3c57a0e85600a to your computer and use it in GitHub Desktop.
Perl 6: Invoke a REPL in the context of the caller (debugging tool)
#!/usr/bin/perl6
use Debug::REPLHere;
sub f { my $secret = 42; repl-here; say "The secret is $secret"; }
f
# Example session:
# $ ./example.p6
# > $secret
# 42
# > $secret=5
# 5
# > ^D
# The secret is 5
unit package Debug::REPLHere;
use nqp;
our sub repl-here($context=Nil) is export {
my $comp = nqp::getcomp('perl6');
my $repl = REPL.new($comp, {});
my $nqpctx := nqp::getattr(nqp::decont($context // CALLER::), PseudoStash, '$!ctx');
$repl.^attributes.first('$!save_ctx').set_value($repl, $nqpctx);
$repl.repl-loop;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment