File Coverage

blib/lib/Devel/REPL/Plugin/Turtles.pm
Criterion Covered Total %
statement 15 24 62.5
branch 0 6 0.0
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 20 37 54.0


line stmt bran cond sub pod time code
1 2     2   20761 use strict;
  2         5  
  2         67  
2 2     2   11 use warnings;
  2         5  
  2         109  
3             # ABSTRACT: Generic command creation using a read hook
4              
5             our $VERSION = '1.003029';
6              
7             use Devel::REPL::Plugin;
8 2     2   11 use Scalar::Util qw(reftype);
  2         2  
  2         19  
9 2     2   8485 use namespace::autoclean;
  2         5  
  2         135  
10 2     2   13  
  2         4  
  2         18  
11             has default_command_prefix => (
12             isa => "RegexpRef",
13             is => "rw",
14             default => sub { qr/\#/ },
15             );
16              
17             has turtles_matchers => (
18             traits => ['Array'],
19             isa => "ArrayRef[RegexpRef|CodeRef]",
20             is => "rw",
21             lazy => 1,
22             default => sub { my $prefix = shift->default_command_prefix; [qr/^ $prefix (\w+) \s* (.*) /x] },
23             handles => {
24             add_turtles_matcher => 'unshift',
25             },
26             );
27              
28             around 'formatted_eval' => sub {
29             my $next = shift;
30             my ($self, $line, @args) = @_;
31              
32             if ( my ( $command, @rest ) = $self->match_turtles($line) ) {
33             my $method = "command_$command";
34             my $expr_method = "expr_$method";
35              
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);
39             }
40             $self->$expr_code($next, @rest);
41             } elsif ( my $cmd_code = $self->can($method) ) {
42             return $self->$cmd_code($next, @rest);
43             } else {
44             unless ( $line =~ /^\s*#/ ) { # special case for comments
45             return $self->format($self->error_return("REPL Error", "Command '$command' does not exist"));
46             }
47             }
48             } else {
49             return $self->$next($line, @args);
50             }
51             };
52              
53             my ( $self, $line ) = @_;
54              
55 0     0 0   foreach my $thingy ( @{ $self->turtles_matchers } ) {
56             if ( reftype $thingy eq 'CODE' ) {
57 0           if ( my @res = $self->$thingy($line) ) {
  0            
58 0 0         return @res;
59 0 0         }
60 0           } else {
61             if ( my @res = ( $line =~ $thingy ) ) {
62             return @res;
63 0 0         }
64 0           }
65             }
66              
67             return;
68             }
69 0            
70             1;
71              
72              
73             =pod
74              
75             =encoding UTF-8
76              
77             =head1 NAME
78              
79             Devel::REPL::Plugin::Turtles - Generic command creation using a read hook
80              
81             =head1 VERSION
82              
83             version 1.003029
84              
85             =head1 DESCRIPTION
86              
87             By default, this plugin allows calling commands using a read hook
88             to detect a default_command_prefix followed by the command name,
89             say MYCMD as an example. The actual routine to call for the
90             command is constructed by looking for subs named 'command_MYCMD'
91             or 'expr_MYCMD' and executing them.
92              
93             =head2 NOTE
94              
95             The C<default_command_prefix> is C<qr/\#/> so care must be taken
96             if other uses for that character are needed (e.g., '#' for the
97             shell escape character in the PDL shell.
98              
99             =head1 SUPPORT
100              
101             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Devel-REPL>
102             (or L<bug-Devel-REPL@rt.cpan.org|mailto:bug-Devel-REPL@rt.cpan.org>).
103              
104             There is also an irc channel available for users of this distribution, at
105             L<C<#devel> on C<irc.perl.org>|irc://irc.perl.org/#devel-repl>.
106              
107             =head1 AUTHOR
108              
109             Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>)
110              
111             =head1 COPYRIGHT AND LICENCE
112              
113             This software is copyright (c) 2007 by Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>).
114              
115             This is free software; you can redistribute it and/or modify it under
116             the same terms as the Perl 5 programming language system itself.
117              
118             =cut