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