Eyapp/Base.pm

filter: Statementstmt 29.7% 53 / 178 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 53.1% 17 / 32 Podpod 0.0% 0 / 16 totaltotal 21.6% 70 / 324 CC 50 SCAR 37.0
CC
50
Cov
20%
pop_method
87.6
push_method
73.5
delete_method
71.9
CRAP 40.6
« Eyapp/Driver.pm ↑ Summary Eyapp/Grammar.pm »
1 package Parse::Eyapp::Base;
2 61 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 61 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 61 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 61 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 61 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 61 our @EXPORT_OK = qw(
9 compute_lines
10 empty_method
11 slurp_file
12 valid_keys
13 invalid_keys
14 write_file
15 numbered
16 insert_function
17 insert_method
18 delete_method
19 push_method
20 push_empty_method
21 pop_method
22 firstval
23 lastval
24 part
25 );
26 61 our %EXPORT_TAGS = ( 'all' => [ @EXPORT_OK ] );
27  
28 }
29 61 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 61 our $FILENAME=__FILE__;
32  
33 25 ▶sub firstval(&@) {
Subroutine firstval: called CC=1 SCAR=0.0
Pod undocumented
34 25 my $handler = shift;
35
36 25 return (grep { $handler->($_) } @_)[0]
37 }
38  
39 0 ▶sub lastval(&@) {
Subroutine lastval: not called CC=1 SCAR=6.9
Pod undocumented
40 0 my $handler = shift;
41
42 0 return (grep { $handler->($_) } @_)[-1]
43 }
44  
45 # Receives a handler $h and a list @_
46 # Elements of @_ with the same value of $h go to the same sublist
47 # Returns a list of lists
48 0 ▶sub part(&@) {
Subroutine part: not called CC=2 SCAR=17.9
Pod undocumented
49 0 my $h = shift;
50  
51 0 my @p;
52 0 push @{$p[$h->($_)]}, $_ for (@_);
53 0 return @p;
54 }
55  
56 ####################################################################
57 # Usage : $input = slurp_file($filename, 'trg');
58 # Purpose : opens "$filename.trg" and sets the scalar
59 # Parameters : file name and extension (not icluding the dot)
60 # Comments : Is this O.S dependent?
61  
62 0 ▶sub slurp_file {
Subroutine slurp_file: not called CC=7 SCAR=40.3
Pod undocumented
63 0 my ($filename, $ext) = @_;
64  
65 0 ▶ croak "Error in slurp_file opening file. Provide a filename!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename and length $filename > 0 0 0 0
Condition0%0/3
defined $filename and length $filename > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $filename and length $filename > 0
defined $filename length $filename > 0
Truth table0%0/3
A and B
A= defined $filenameB= length $filename > 0
ABresult
0X0
100
111
66 unless defined($filename) and length($filename) > 0;
67 0 ▶ $ext = "" unless defined($ext);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ext 0 0 0
68 0 ▶ $filename .= ".$ext" unless (-r $filename) or ($filename =~ m{[.]$ext$});
Branch: 1 branch
BranchTrueFalseTotal
unless -r $filename or $filename =~ /[.]$ext$/ 0 0 0
Condition0%0/3
-r $filename or $filename =~ /[.]$ext$/
l!l&&r!l&&!r
000
MC/DC0%0/2
-r $filename or $filename =~ /[.]$ext$/
-r $filename $filename =~ /[.]$ext$/
Truth table0%0/3
A or B
A= -r $filenameB= $filename =~ /[.]$ext$/
ABresult
1X1
011
000
69 0 local $/ = undef;
70 0 ▶ open my $FILE, $filename or croak "Can't open file $filename";
Branch: 1 branch
BranchTrueFalseTotal
open my $FILE, $filename or croak("Can't open file $filename") 0 0 0
71 0 my $input = <$FILE>;
72 0 close($FILE);
73 0 return $input;
74 }
75  
76 36 ▶sub valid_keys {
Subroutine valid_keys: called CC=1 SCAR=0.0
Pod undocumented
77 36 my %valid_args = @_;
78  
79 36 my @valid_args = keys(%valid_args);
80 36 local $" = ", ";
81 36 return "@valid_args"
82 }
83  
84 31 ▶sub invalid_keys {
Subroutine invalid_keys: called CC=1 SCAR=0.0
Pod undocumented
85 31 my $valid_args = shift;
86 31 my $args = shift;
87  
88 31 ▶ return (first { !exists($valid_args->{$_}) } keys(%$args));
Subroutine __ANON__: called CC=1 SCAR=0.0
89 }
90  
91 0 ▶sub write_file {
Subroutine write_file: not called CC=4 SCAR=30.0
Pod undocumented
92 0 my ($outputfile, $text) = @_;
93 0 ▶ defined($outputfile) or croak "Error at write_file. Undefined file name";
Branch: 1 branch
BranchTrueFalseTotal
defined $outputfile or croak("Error at write_file. Undefined file name") 0 0 0
94  
95 0 my $OUTPUTFILE;
96
97 0 ▶ open($OUTPUTFILE, "> $outputfile") or croak "Can't open file $OUTPUTFILE.";
Branch: 1 branch
BranchTrueFalseTotal
open $OUTPUTFILE, "> $outputfile" or croak("Can't open file $OUTPUTFILE.") 0 0 0
98 0 print $OUTPUTFILE ($$text);
99 0 ▶ close($OUTPUTFILE) or croak "Can't close file $OUTPUTFILE.";
Branch: 1 branch
BranchTrueFalseTotal
close $OUTPUTFILE or croak("Can't close file $OUTPUTFILE.") 0 0 0
100 }
101  
102 # Sort of backpatching for line number directives:
103 # Substitutes $pattern by #line $number $filename in string $textr
104 0 ▶sub compute_lines {
Subroutine compute_lines: not called CC=2 SCAR=17.9
Pod undocumented
105 0 my ($textr, $filename, $pattern) = @_;
106
107 0 local $_ = 1;
108 0 $$textr =~ s{\n$pattern\n|(\n)}
109 {
110 0 $_++;
111 0 ▶ if (defined($1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) { } 0 0 0
112 0 "\n";
113 }
114 else {
115 0 my $directive = "\n#line $_ $filename\n";
116 0 $_++;
117 0 $directive;
118 }
119 }eg;
120 }
121  
122 0 ▶sub numbered {
Subroutine numbered: not called CC=2 SCAR=17.9
Pod undocumented
123 0 my ($output, $c) = (shift(), 1);
124 0 my $cr = $output =~ tr/\n//;
125 0 ▶ $cr = 1 if $cr <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cr <= 0 0 0 0
126 0 my $digits = 1+int(log($cr)/log(10));
127 0 $output =~ s/^/sprintf("%${digits}d ",$c++)/emg;
128 0 $output;
129 }
130  
131 0 ▶sub insert_function {
Subroutine insert_function: not called CC=5 SCAR=25.7
Pod undocumented
132 61 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
133 61 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
134  
135 0 my $code = pop;
136 0 ▶ croak "Error in insert_function: last arg must be a CODE ref\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
137 unless ref($code) eq 'CODE';
138  
139 0 for (@_) {
140 0 ▶ croak "Error in insert_function: Illegal function name <$_>\n" unless /^[\w:]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^[\w:]+$/ 0 0 0
141 0 ▶ my $fullname = /^\w+$/? scalar(caller).'::'.$_ : $_;
Branch: 1 branch
BranchTrueFalseTotal
/^\w+$/ ? : 0 0 0
142 0 *{$fullname} = $code;
143 }
144 }
145  
146 0 ▶sub insert_method {
Subroutine insert_method: not called CC=9 SCAR=40.2
Pod undocumented
147  
148 0 my $code = pop;
149  
150 0 ▶ unless (ref($code)) { # not a ref: string or undef
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $code) 0 0 0
151 # Call is: insert_method('Tutu', 'titi')
152 0 ▶ if (defined($code) && $code =~/^\w+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code and $code =~ /^\w+$/) 0 0 0
Condition0%0/3
defined $code and $code =~ /^\w+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $code and $code =~ /^\w+$/
defined $code $code =~ /^\w+$/
Truth table0%0/3
A and B
A= defined $codeB= $code =~ /^\w+$/
ABresult
0X0
100
111
153 0 delete_method(@_, $code);
154 0 return;
155 }
156 # Call is: insert_method('Tutu', 'titi', undef)
157 0 goto &delete_method;
158 }
159 0 ▶ croak "Error in insert_method: expected a CODE ref found $code\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
160 unless ref($code) eq 'CODE';
161  
162 0 my $name = pop;
163 0 ▶ croak "Error in insert_method: Illegal method name <$_>\n" unless $name =~/^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^\w+$/ 0 0 0
164  
165 0 my @classes = @_;
166 0 ▶ @classes = scalar(caller) unless @classes;
Branch: 1 branch
BranchTrueFalseTotal
unless @classes 0 0 0
167 0 for (@classes) {
168 0 ▶ croak "Error in insert_method: Illegal class name <$_>\n" unless /^[\w:]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^[\w:]+$/ 0 0 0
169 61 ▶ no warnings 'redefine';;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 61 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
171 0 *{$_."::".$name} = $code;
172 }
173 }
174  
175 0 ▶sub delete_method {
Subroutine delete_method: not called CC=9 SCAR=42.7
Pod undocumented
176 0 my $name = pop;
177 0 ▶ $name = '' unless defined($name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
178 0 ▶ croak "Error in delete_method: Illegal method name <$name>\n" unless $name =~/^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^\w+$/ 0 0 0
179 0 my @classes = @_;
180  
181 0 ▶ @classes = scalar(caller) unless @classes;
Branch: 1 branch
BranchTrueFalseTotal
unless @classes 0 0 0
182 61 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
183 0 for (@classes) {
184 0 ▶ croak "Error in delete_method: Illegal class name <$_>\n" unless /^[\w:]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^[\w:]+$/ 0 0 0
185 0 ▶ unless ($_->can($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_->can($name)) 0 0 0
186 0 print STDERR "Warning in delete_method: No sub <$name> to delete in package <$_>\n";
187 0 next;
188 }
189 0 my $fullname = $_."::".$name;
190  
191 # Temporarily save the other entries
192 0 my @refs = map { *{$fullname}{$_} } qw{HASH SCALAR ARRAY GLOB};
193  
194 # Delete typeglob
195 0 *{$fullname} = do { local *{$fullname} };
196  
197 # Restore HASH SCALAR ARRAY GLOB entries
198 0 for (@refs) {
199 0 ▶ next unless defined($_);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
200 0 *{$fullname} = $_;
201 }
202 }
203 }
204  
205 0 ▶sub empty_method {
Subroutine empty_method: not called CC=1 SCAR=6.9
Pod undocumented
206 0 ▶ insert_method(@_, sub {});
Subroutine __ANON__: not called CC=1 SCAR=6.9
207 }
208  
209 0 ▶sub push_empty_method {
Subroutine push_empty_method: not called CC=1 SCAR=6.9
Pod undocumented
210 0 ▶ push_method(@_, sub {});
Subroutine __ANON__: not called CC=1 SCAR=6.9
211 }
212  
213 {
214 61 my %methods;
215  
216 0 ▶ sub push_method {
Subroutine push_method: not called CC=9 SCAR=43.0
Pod undocumented
217 0 my $handler;
218 0 ▶ if (ref($_[-1]) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[-1] eq 'CODE') { } 0 0 0
219 0 $handler = pop;
220 }
221 else {
222 0 $handler = undef;
223 }
224  
225 0 my $name = pop;
226 0 ▶ $name = '' unless defined($name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
227 0 ▶ croak "Error in push_method: Illegal method name <$name>\n" unless $name =~/^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^\w+$/ 0 0 0
228 0 my @classes = @_;
229  
230 0 my @returnmethods;
231  
232 0 ▶ @classes = scalar(caller) unless @classes;
Branch: 1 branch
BranchTrueFalseTotal
unless @classes 0 0 0
233 0 for (@classes) {
234 0 ▶ croak "Error in push_method: Illegal class name <$_>\n" unless /^[\w:]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^[\w:]+$/ 0 0 0
235 0 my $fullname = $_."::".$name;
236 0 ▶ if ($_->can($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->can($name)) { } 0 0 0
237 61 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
238 0 my $coderef = \&{$fullname};
239 0 push @returnmethods, $coderef;
240 0 push @{$methods{$fullname}}, $coderef;
241 }
242 else {
243 0 push @returnmethods, undef;
244 0 push @{$methods{$fullname}}, undef;
245 }
246 }
247 0 insert_method(@classes, $name, $handler);
248
249 0 ▶ return wantarray? @returnmethods : $returnmethods[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
250 }
251  
252 0 ▶ sub pop_method {
Subroutine pop_method: not called CC=10 SCAR=44.7
Pod undocumented
253 0 my $name = pop;
254 0 ▶ $name = '' unless defined($name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
255 0 ▶ croak "Error in push_method: Illegal method name <$name>\n" unless $name =~/^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^\w+$/ 0 0 0
256 0 my @classes = @_;
257  
258 0 my @returnmethods;
259  
260 0 ▶ @classes = scalar(caller) unless @classes;
Branch: 1 branch
BranchTrueFalseTotal
unless @classes 0 0 0
261 0 for (@classes) {
262 0 my $fullname = $_."::".$name;
263 61 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
264 0 ▶ push @returnmethods, $_->can($name)? \&{$fullname} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$_->can($name) ? : 0 0 0
265 0 ▶ if (defined($methods{$fullname})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $methods{$fullname} and UNIVERSAL::isa($methods{$fullname}, "ARRAY") and @{$methods{$fullname};}) 0 0 0
Condition0%0/6
defined $methods{$fullname} and UNIVERSAL::isa($methods{$fullname}, "ARRAY") and @{$methods{$fullname};}
!ll&&!rl&&r
000
defined $methods{$fullname} and UNIVERSAL::isa($methods{$fullname}, "ARRAY")
!ll&&!rl&&r
000
MC/DC0%0/3
defined $methods{$fullname} and UNIVERSAL::isa($methods{$fullname}, "ARRAY") and @{$methods{$fullname};}
defined $methods{$fullname} UNIVERSAL::isa($methods{$fullname}, "ARRAY") @{$methods{$fullname};}
Truth table0%0/4
A and B and C
A= defined $methods{$fullname}B= UNIVERSAL::isa($methods{$fullname}, "ARRAY")C= @{$methods{$fullname};}
ABCresult
0XX0
10X0
1100
1111
266 && UNIVERSAL::isa($methods{$fullname}, 'ARRAY')
267 0 && @{$methods{$fullname}}) {
268 0 my $handler = pop @{$methods{$fullname}};
269 0 insert_method($_, $name, $handler);
270 }
271 }
272 0 ▶ return wantarray? @returnmethods : $returnmethods[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
273 }
274  
275 } # Closure for %methods
276  
277 61 1;
278  
« Eyapp/Driver.pm ↑ Summary Eyapp/Grammar.pm »