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