App-REPL
view release on metacpan or search on metacpan
It uses PPI to find a good place within the given Perl to insert an
assignment. It isn't perfect, and probably you can find places
where it doesn't print anything useful.
You can type arbitrarily complex Perl, though -- with 'package'
statements, subroutine definitions, &c.
4. ... all on a single line? :-/
No. Again, it uses PPI to try and determine if your Perl is
complete, and if not it will continue to prompt you for the
rest.
5. Tries to?
Well, Perl isn't Python :-) Without imposing annoying restrictions,
App-REPL can't know that you meant for the second line to continue
on from the first, in:
print $a, $b,
$c, $d;
use Data::Dumper;
use Symbol;
use Term::ReadLine;
$App::REPL::DEBUG = 0;
{ my $in_package = 'App::REPL';
sub in_package { @_ ? $in_package = shift : $in_package }
}
# ----------------------------------------------------------------------
# Added RESET as the color somehow bleeds into the prompt
# -- when we use Term::ReadLine
{ my $prompt;
my $term = Term::ReadLine->new('iperl');
sub pnew { $prompt = RESET . in_package . ' _ ' }
sub pcont { $prompt = RESET . in_package . '. ' }
sub prompt {
my $s = $term->readline($prompt);
$term->addhistory($s) if defined($s) and $s =~ /\S/;
$s
}
pnew;
$term->ornaments(0)
}
sub eek { print STDERR BOLD RED @_, "\n"; goto REPL }
# The PPI here handles the rest of the magic: it detects unfinished
# blocks and such so that the repl can request more lines until they
# complete. Note that this does -not- handle e.g. qw(
#--
{ my $f = PPI::Find->new(sub {
my %h = %{+shift};
(exists $h{start} and !exists $h{finish}) ? 1 : 0
});
sub repl {
my $s = '';
REPL: while (defined($_ = prompt)) {
$s .= "\n" . $_;
my $d = PPI::Document->new(\$s);
if ($f->in($d)) {
pcont
}
else {
scoped_eval PRO . save_ret($d) . EPI;
dump_ret;
$App::REPL::ret = '';
$s = '';
( run in 0.978 second using v1.01-cache-2.11-cpan-6aa56a78535 )