3 package Devel::REPL::Plugin::Turtles;
5 our $VERSION = '1.003027';
7 use Devel::REPL::Plugin;
8 use Scalar::Util qw(reftype);
9 use namespace::autoclean;
11 has default_command_prefix => (
14 default => sub { qr/\#/ },
17 has turtles_matchers => (
19 isa => "ArrayRef[RegexpRef|CodeRef]",
22 default => sub { my $prefix = shift->default_command_prefix; [qr/^ $prefix (\w+) \s* (.*) /x] },
24 add_turtles_matcher => 'unshift',
28 around 'formatted_eval' => sub {
30 my ($self, $line, @args) = @_;
32 if ( my ( $command, @rest ) = $self->match_turtles($line) ) {
33 my $method = "command_$command";
34 my $expr_method = "expr_$method";
36 if ( my $expr_code = $self->can($expr_method) ) {
37 if ( my $read_more = $self->can("continue_reading_if_necessary") ) {
38 push @rest, $self->$read_more(pop @rest);
40 $self->$expr_code($next, @rest);
41 } elsif ( my $cmd_code = $self->can($method) ) {
42 return $self->$cmd_code($next, @rest);
44 unless ( $line =~ /^\s*#/ ) { # special case for comments
45 return $self->format($self->error_return("REPL Error", "Command '$command' does not exist"));
49 return $self->$next($line, @args);
54 my ( $self, $line ) = @_;
56 foreach my $thingy ( @{ $self->turtles_matchers } ) {
57 if ( reftype $thingy eq 'CODE' ) {
58 if ( my @res = $self->$thingy($line) ) {
62 if ( my @res = ( $line =~ $thingy ) ) {
77 Devel::REPL::Plugin::Turtles - Generic command creation using a read hook
81 By default, this plugin allows calling commands using a read hook
82 to detect a default_command_prefix followed by the command name,
83 say MYCMD as an example. The actual routine to call for the
84 command is constructed by looking for subs named 'command_MYCMD'
85 or 'expr_MYCMD' and executing them.
89 The C<default_command_prefix> is C<qr/\#/> so care must be taken
90 if other uses for that character are needed (e.g., '#' for the
91 shell escape character in the PDL shell.