Reply.pm

filter: Statementstmt 14.6% 21 / 143 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 22.2% 6 / 27 Podpod 100% 3 / 3 totaltotal 14.2% 30 / 210 CC 24 SCAR 26.2
CC
24
Cov
12%
_load_config
42.0
new
30.0
step
20.0
CRAP 13.8
« Reply/Plugin/DataDump.pm ↑ Summary Reply/Plugin/Editor.pm »
1 package Reply;
2 2 our $AUTHORITY = 'cpan:DOY';
3 2 $Reply::VERSION = '0.42';
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: read, eval, print, loop, yay!
7  
8 2 ▶use Module::Runtime qw(compose_module_name require_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Reply::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15  
16 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
17 0 my $class = shift;
18 0 my %opts = @_;
19  
20 0 my $self = bless {}, $class;
21  
22 0 $self->{plugins} = [];
23 0 $self->{_default_plugin} = $self->_instantiate_plugin('Defaults');
24  
25 0 ▶ if (defined $opts{config}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"config"}) 0 0 0
26 0 ▶ if (!ref($opts{config})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $opts{"config"}) 0 0 0
27 0 $opts{config} = Reply::Config->new(file => $opts{config});
28 }
29 0 $self->_load_config($opts{config});
30 }
31  
32 0 ▶ $self->_load_plugin($_) for @{ $opts{plugins} || [] };
Condition0%0/2
$opts{'plugins'} || []
l!l
00
MC/DC0%0/1
$opts{'plugins'} || []
$opts{'plugins'}
Truth table0%0/2
A
A= $opts{'plugins'}
Aresult
11
00
33  
34 0 return $self;
35 }
36  
37  
38 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
39 0 my $self = shift;
40  
41 0 while (1) {
42 0 my $continue = $self->step;
43 0 ▶ last unless $continue;
Branch: 1 branch
BranchTrueFalseTotal
unless $continue 0 0 0
44 }
45 0 print "\n";
46 }
47  
48  
49 0 ▶sub step {
Subroutine step: not called CC=4 SCAR=30.0
Pod documented
50 0 my $self = shift;
51 0 my ($line, $verbose) = @_;
52  
53 0 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) { } 0 0 0
54 0 ▶ print $self->_wrapped_plugin('prompt'), $line, "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
55 if $verbose;
56 }
57 else {
58 0 $line = $self->_read;
59 }
60  
61 0 ▶ return unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
62  
63 0 $line = $self->_preprocess_line($line);
64  
65 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 my @result = $self->_eval($line);
67 0 $self->_print_result(@result);
68 }
69 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 0 $self->_print_error($_);
71 0 };
72  
73 0 my ($continue) = $self->_loop;
74 0 return $continue;
75 }
76  
77 0 ▶sub _load_config {
Subroutine _load_config: not called CC=6 SCAR=37.4
78 0 my $self = shift;
79 0 my ($config) = @_;
80  
81 0 my $data = $config->data;
82  
83 0 my $root_config;
84 0 for my $section (@$data) {
85 0 my ($name, $data) = @$section;
86 0 ▶ if ($name eq '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq '_') { } 0 0 0
87 0 $root_config = $data;
88 }
89 else {
90 0 $self->_load_plugin($name => $data);
91 }
92 }
93  
94 0 for my $line (sort grep { /^script_line/ } keys %$root_config) {
95 0 $self->step($root_config->{$line});
96 }
97  
98 0 ▶ if (defined(my $file = $root_config->{script_file})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = $root_config->{"script_file"})) 0 0 0
99 0 my $contents = do {
100 0 ▶ open my $fh, '<', $file or die "Couldn't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Couldn't open ${file}: $!" 0 0 0
101 0 local $/ = undef;
102 <$fh>
103 0 };
104 0 $self->step($contents);
105 }
106 }
107  
108 0 ▶sub _load_plugin {
Subroutine _load_plugin: not called CC=1 SCAR=6.9
109 0 my $self = shift;
110 0 my ($plugin, $opts) = @_;
111  
112 0 $plugin = $self->_instantiate_plugin($plugin, $opts);
113  
114 0 push @{ $self->{plugins} }, $plugin;
115 }
116  
117 0 ▶sub _instantiate_plugin {
Subroutine _instantiate_plugin: not called CC=3 SCAR=24.8
118 0 my $self = shift;
119 0 my ($plugin, $opts) = @_;
120  
121 0 ▶ if (!blessed($plugin)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($plugin)) 0 0 0
122 0 $plugin = compose_module_name("Reply::Plugin", $plugin);
123 0 require_module($plugin);
124 0 ▶ die "$plugin is not a valid plugin"
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin->isa("Reply::Plugin") 0 0 0
125 unless $plugin->isa("Reply::Plugin");
126  
127 0 my $weakself = $self;
128 0 weaken($weakself);
129  
130 $plugin = $plugin->new(
131 %$opts,
132 0 ▶ publisher => sub { $weakself->_publish(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 );
134 }
135  
136 0 return $plugin;
137 }
138  
139 0 ▶sub _plugins {
Subroutine _plugins: not called CC=1 SCAR=6.9
140 0 my $self = shift;
141  
142 return (
143 0 @{ $self->{plugins} },
144 $self->{_default_plugin},
145 0 );
146 }
147  
148 0 ▶sub _read {
Subroutine _read: not called CC=1 SCAR=6.9
149 0 my $self = shift;
150  
151 0 my $prompt = $self->_wrapped_plugin('prompt');
152 0 return $self->_wrapped_plugin('read_line', $prompt);
153 }
154  
155 0 ▶sub _preprocess_line {
Subroutine _preprocess_line: not called CC=2 SCAR=17.9
156 0 my $self = shift;
157 0 my ($line) = @_;
158  
159 0 ▶ if ($line =~ s/^#(\w+)(?:\s+|$)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^#(\w+)(?:\s+|$)//) 0 0 0
160 0 ($line) = $self->_chained_plugin("command_\L$1", $line);
161 }
162  
163 0 return "\n#line 1 \"reply input\"\n$line";
164 }
165  
166 0 ▶sub _eval {
Subroutine _eval: not called CC=2 SCAR=17.9
167 0 my $self = shift;
168 0 my ($line) = @_;
169  
170 0 ▶ ($line) = $self->_chained_plugin('mangle_line', $line)
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 0 0 0
171 if defined $line;
172  
173 0 my ($code) = $self->_wrapped_plugin('compile', $line);
174 0 return $self->_wrapped_plugin('execute', $code);
175 }
176  
177 0 ▶sub _print_error {
Subroutine _print_error: not called CC=1 SCAR=6.9
178 0 my $self = shift;
179 0 my ($error) = @_;
180  
181 0 ($error) = $self->_chained_plugin('mangle_error', $error);
182 0 $self->_wrapped_plugin('print_error', $error);
183 }
184  
185 0 ▶sub _print_result {
Subroutine _print_result: not called CC=1 SCAR=6.9
186 0 my $self = shift;
187 0 my (@result) = @_;
188  
189 0 @result = $self->_chained_plugin('mangle_result', @result);
190 0 $self->_wrapped_plugin('print_result', @result);
191 }
192  
193 0 ▶sub _loop {
Subroutine _loop: not called CC=1 SCAR=6.9
194 0 my $self = shift;
195  
196 0 $self->_chained_plugin('loop', 1);
197 }
198  
199 0 ▶sub _publish {
Subroutine _publish: not called CC=1 SCAR=6.9
200 0 my $self = shift;
201  
202 0 $self->_concatenate_plugin(@_);
203 }
204  
205 0 ▶sub _wrapped_plugin {
Subroutine _wrapped_plugin: not called CC=3 SCAR=24.8
206 0 my $self = shift;
207 0 ▶ my @plugins = ref($_[0]) ? @{ shift() } : $self->_plugins;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
208 0 my ($method, @args) = @_;
209  
210 0 @plugins = grep { $_->can($method) } @plugins;
211  
212 0 ▶ return @args unless @plugins;
Branch: 1 branch
BranchTrueFalseTotal
unless @plugins 0 0 0
213  
214 0 my $plugin = shift @plugins;
215 0 ▶ my $next = sub { $self->_wrapped_plugin(\@plugins, $method, @_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
216  
217 0 return $plugin->$method($next, @args);
218 }
219  
220 0 ▶sub _chained_plugin {
Subroutine _chained_plugin: not called CC=3 SCAR=24.8
221 0 my $self = shift;
222 0 ▶ my @plugins = ref($_[0]) ? @{ shift() } : $self->_plugins;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
223 0 my ($method, @args) = @_;
224  
225 0 @plugins = grep { $_->can($method) } @plugins;
226  
227 0 for my $plugin (@plugins) {
228 0 @args = $plugin->$method(@args);
229 }
230  
231 0 return @args;
232 }
233  
234 0 ▶sub _concatenate_plugin {
Subroutine _concatenate_plugin: not called CC=3 SCAR=24.8
235 0 my $self = shift;
236 0 ▶ my @plugins = ref($_[0]) ? @{ shift() } : $self->_plugins;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
237 0 my ($method, @args) = @_;
238  
239 0 @plugins = grep { $_->can($method) } @plugins;
240  
241 0 my @results;
242  
243 0 for my $plugin (@plugins) {
244 0 push @results, $plugin->$method(@args);
245 }
246  
247 0 return @results;
248 }
249  
250  
251 2 1;
252  
253 __END__
« Reply/Plugin/DataDump.pm ↑ Summary Reply/Plugin/Editor.pm »