my $line = $self->$orig(@args);
if (defined $line) {
- while ($self->line_needs_continuation($line)) {
- my $orig_prompt = $self->prompt;
- $self->prompt($self->continuation_prompt);
+ return $self->continue_reading_if_necessary($line, @args);
+ } else {
+ return $line;
+ }
+};
+
+sub continue_reading_if_necessary {
+ my ( $self, $line, @args ) = @_;
+
+ while ($self->line_needs_continuation($line)) {
+ my $orig_prompt = $self->prompt;
+ $self->prompt($self->continuation_prompt);
- $self->line_depth($self->line_depth + 1);
- my $append = $self->read(@args);
- $self->line_depth($self->line_depth - 1);
+ $self->line_depth($self->line_depth + 1);
+ my $append = $self->read(@args);
+ $self->line_depth($self->line_depth - 1);
- $line .= $append if defined($append);
+ $line .= $append if defined($append);
- $self->prompt($orig_prompt);
+ $self->prompt($orig_prompt);
- # ^D means "shut up and eval already"
- return $line if !defined($append);
- }
+ # ^D means "shut up and eval already"
+ return $line if !defined($append);
}
+
return $line;
-};
+}
sub line_needs_continuation
{
use namespace::clean -except => [ 'meta' ];
around 'eval' => sub {
- my $next = shift;
- my ($self, $line) = @_;
- if ($line =~ /^#(.*)/) {
- return $next->($self, ('$_REPL->' . $1 . '; return();'));
+ my $next = shift;
+ my ($self, $line) = @_;
+ if ( my ( $command, $rest ) = ( $line =~ /^#(\w+)\s*(.*)/ ) ) {
+ if ( my $cont = $self->can("continue_reading_if_necessary") ) {
+ $rest = $self->$cont($rest);
}
- else {
- return $next->($self, $line);
+
+ my $method = "command_$command";
+
+ if ( $self->can($method) ) {
+ return $self->$method($rest);
+ } else {
+ return $self->error_return("REPL error", "Command '$command' does not exist");
}
-
+ }
+ else {
+ return $next->($self, $line);
+ }
};
1;