App-AnteREPL
view release on metacpan or search on metacpan
lib/App/AnteREPL.pm view on Meta::CPAN
sub inspect_value {
my ($self, $command, $expression) = @_;
die "$command needs a Perl expression.\n" unless $expression =~ /\S/;
if ($command eq 'DUMP') {
local $Data::Dumper::Sortkeys = 1;
print $self->evaluate("Data::Dumper::Dumper(do { $expression\n })", 0);
return;
}
require Devel::Peek;
require POSIX;
# Devel::Peek writes to the native stderr descriptor, bypassing tied Perl
# handles. Redirect that descriptor temporarily so its output joins VRAM.
my $temporary = File::Temp->new(UNLINK => 1);
my $saved = POSIX::dup(2);
die "Cannot preserve stderr: $!\n" unless defined $saved && $saved >= 0;
unless (defined POSIX::dup2(fileno($temporary), 2)) {
POSIX::close($saved);
die "Cannot capture Devel::Peek output: $!\n";
}
$expression =~ s/;\s*\z//;
my $ok = eval { $self->evaluate("Devel::Peek::Dump($expression\n)", 0); 1 };
my $error = $@;
my $restored = POSIX::dup2($saved, 2);
POSIX::close($saved);
die "Cannot restore stderr: $!\n" unless defined $restored;
seek($temporary, 0, 0) or die "Cannot rewind peek output: $!\n";
local $/;
my $bytes = <$temporary>;
print decode('UTF-8', $bytes || '');
die $error unless $ok;
}
sub needs_more {
my ($source) = @_;
$source = _expand_short_command($source);
lib/App/AnteREPL.pm view on Meta::CPAN
$FOO = {
bar => 1,
names => [qw(Alice Bob)],
};
DUMP $FOO
DUMP \%ENV
PEEK expression calls the core Devel::Peek::Dump directly on the expression,
including an optional limit, e.g. C<PEEK $FOO> or C<PEEK $FOO, 10>. Its native
stderr output is captured into the editable display. These are prompt commands;
in stored Perl use the module functions explicitly. They do not redefine Perl's
CORE::dump keyword.
Perl print/say/warn output and simple C<E<lt>STDINE<gt>> line input use the screen.
C<! command> captures external stdout/stderr into screen rows when it finishes,
with closed input. Use C<!! command> for interactive external programs; it lends
them the real terminal and waits for Enter before returning to the screen.
EDIT likewise restores normal terminal settings while your editor runs.
Use these escapes for external programs: direct system/exec calls and arbitrary
XS terminal output bypass the screen model.
Screen mode needs an ANSI-capable terminal. On Unix, it uses core POSIX terminal
support, or Term::ReadKey if installed. Native Windows requires Term::ReadKey
(C<cpanm Term::ReadKey>); its Windows backend has not been validated here.
C<--plain> disables screen editing and uses plain input with no prompt or history.
}
sub cli {
my ($input, @args) = @_;
my $err = gensym;
my $pid = open3(my $in, my $out, $err, $^X, "-I$FindBin::Bin/../lib", $script, '--quiet', @args);
print {$in} $input;
close $in;
local $/;
my $stdout = <$out>;
my $stderr = <$err>;
waitpid $pid, 0;
return ($stdout, $stderr, $? >> 8);
}
my $repl = App::AnteREPL->new;
ok !$repl->dirty, 'new program is clean';
$repl->set_line(90, 'say $x;');
$repl->set_line(10, '$x += 2;');
is $repl->listing, "10 \$x += 2;\n90 say \$x;\n", 'LIST sorts numerically';
is $repl->listing('11-'), "90 say \$x;\n", 'open-ended range';
is $repl->listing('-10'), "10 \$x += 2;\n", 'range to a number';
is $repl->listing('90'), "90 say \$x;\n", 'one line';
t/features.t view on Meta::CPAN
is_deeply \%ENV, {EDITED=>'from_editor'}, 'EDIT ENV imports editor file';
$ENV{EDITOR} = qq{"$^X" "$editor" change};
capture { $env->dispatch('ENV EDIT'); };
is_deeply \%ENV, {EDITED=>'from_editor'}, 'ENV EDIT aliases EDIT ENV';
}
my $err = gensym;
my $pid = open3(my $in, my $out, $err, $^X, "-I$FindBin::Bin/../lib", "$FindBin::Bin/../script/anterepl", '--quiet');
print {$in} "AUTO\n\$FOO = {\nbar => 1,\n};\nAUTO OFF\nRUN\n? \$FOO->{bar}\nQUIT!\n";
close $in;
my ($stdout, $stderr);
{ local $/; $stdout = <$out>; $stderr = <$err>; }
waitpid $pid, 0;
is $? >> 8, 0, 'anterepl CLI accepts AUTO multiline input';
is $stderr, '', 'AUTO CLI has no errors';
like $stdout, qr/AUTO off.\n1\n/, 'quiet AUTO CLI stores then runs multiline code';
unlike $stdout, qr/\$FOO =|bar =>/, 'AUTO CLI does not echo stored source';
done_testing;
t/semicolons.t view on Meta::CPAN
my $safe = App::AnteREPL->new;
$safe->dispatch('10 BEGIN { $ENV{PERL_ANTE_REPL_SEMI_EXECUTED} = 1 }');
ok !exists $ENV{PERL_ANTE_REPL_SEMI_EXECUTED}, 'normalization never executes BEGIN blocks';
}
my $err = gensym;
my $pid = open3(my $in, my $out, $err, $^X, "-I$FindBin::Bin/../lib", $script, '--quiet');
print {$in} "10 say \"hi\"\n5 say \"what is your name?\"\nLIST\nRUN\nQUIT!\n";
close $in;
my $stdout = do { local $/; <$out> };
my $stderr = do { local $/; <$err> };
waitpid $pid, 0;
is $? >> 8, 0, 'reported CLI session exits successfully';
is $stderr, '', 'reported CLI session has no syntax errors';
is $stdout, "5 say \"what is your name?\";\n10 say \"hi\";\nwhat is your name?\nhi\n\n", 'reported CLI transcript corrected and aligned';
done_testing;
( run in 1.017 second using v1.01-cache-2.11-cpan-60884a8df76 )