From 517b376231b126f823bb017a2619cdac83270714 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Mon, 20 Jul 2020 13:37:41 -0700 Subject: [PATCH] Plang can now be used with persistent state via `plangrepl` --- Plugins/Plang.pm | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Plugins/Plang.pm b/Plugins/Plang.pm index 23ee8cde..f8569090 100644 --- a/Plugins/Plang.pm +++ b/Plugins/Plang.pm @@ -58,6 +58,9 @@ sub initialize { # register the `plang` command $self->{pbot}->{commands}->register(sub { $self->cmd_plang(@_) }, "plang", 0); + + # register the `plangrepl` command (does not reset environment) + $self->{pbot}->{commands}->register(sub { $self->cmd_plangrepl(@_) }, "plangrepl", 0); } sub unload { @@ -81,6 +84,22 @@ sub cmd_plang { return length $self->{output} ? $self->{output} : "No output."; } +sub cmd_plangrepl { + my ($self, $context) = @_; + + my $usage = "Usage: plangrepl ; see https://github.com/pragma-/Plang"; + return $usage if not length $context->{arguments}; + + $self->{output} = ""; # collect output of the embedded Plang program + my $result = $self->{plang}->interpret_string($context->{arguments}, repl => 1); + + # check to see if we need to append final result to output + $self->{output} .= $self->{plang}->{interpreter}->output_value($result, repl => 1) if defined $result->[1]; + + # return the output + return length $self->{output} ? $self->{output} : "No output."; +} + # overridden `print` built-in sub print_override {