summaryrefslogtreecommitdiffstats
path: root/lib/Reply/Plugin/Defaults.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Reply/Plugin/Defaults.pm')
-rw-r--r--lib/Reply/Plugin/Defaults.pm64
1 files changed, 64 insertions, 0 deletions
diff --git a/lib/Reply/Plugin/Defaults.pm b/lib/Reply/Plugin/Defaults.pm
new file mode 100644
index 0000000..507c830
--- /dev/null
+++ b/lib/Reply/Plugin/Defaults.pm
@@ -0,0 +1,64 @@
+package Reply::Plugin::Defaults;
+
+# XXX Eval::Closure imposes its own hints on things that are eval'ed at the
+# moment, but this may be fixed in the future
+BEGIN {
+ our $default_hints = $^H;
+ our $default_hinthash = { %^H };
+ our $default_warning_bits = ${^WARNING_BITS};
+}
+
+use strict;
+use warnings;
+
+use base 'Reply::Plugin';
+
+use Eval::Closure;
+
+sub prompt { "> " }
+
+sub read_line {
+ my $self = shift;
+ my ($next, $prompt) = @_;
+
+ print $prompt;
+ return scalar <>;
+}
+
+my $PREFIX = "package main; BEGIN { \$^H = \$" . __PACKAGE__ . "::default_hints; \%^H = \%\$" . __PACKAGE__ . "::default_hinthash; \${^WARNING_BITS} = \$" . __PACKAGE__ . "::default_warning_bits }";
+
+sub compile {
+ my $self = shift;
+ my ($next, $line, %args) = @_;
+
+ return eval_closure(
+ source => "sub { $PREFIX; $line }",
+ terse_error => 1,
+ %args,
+ );
+}
+
+sub execute {
+ my $self = shift;
+ my ($next, $code) = @_;
+
+ return $code->();
+}
+
+sub print_error {
+ my $self = shift;
+ my ($next, $error) = @_;
+
+ print $error
+ if defined $error;
+}
+
+sub print_result {
+ my $self = shift;
+ my ($next, @result) = @_;
+
+ print @result, "\n"
+ if @result;
+}
+
+1;