blib/lib/File/MultilineGrep.pm

filter: Statementstmt 32.8% 21 / 64 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 71.4% 5 / 7 Podpod 100% 1 / 1 totaltotal 23.0% 27 / 117 CC 20 SCAR 51.6
CC
20
Cov
20%
file_multiline_grep
272.0
_PrintMatchingGroup
30.0
BEGIN
1.0
CRAP 173.3
↑ Summary
1 package File::MultilineGrep;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 require Exporter;
9 2 our @ISA = qw(Exporter);
10 2 our @EXPORT = qw(file_multiline_grep);
11  
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 our $VERSION = '0.01';
14  
15 2 my %group_lines = () ; # reset %group_lines
16 2 my $line;
17  
18 #-------------------------------------------------#
19 sub file_multiline_grep
20 0 ▶{
Subroutine file_multiline_grep: not called CC=16 SCAR=56.1
Pod documented
21 0 my ($begin_pattern, $finish_pattern, $middle_pattern, $numeration, $file, $separator) = @_ ;
22  
23 0 my $one_separator_flag = 0;
24 0 ▶ if ($begin_pattern and ! $finish_pattern)
Branch: 1 branch
BranchTrueFalseTotal
if ($begin_pattern and not $finish_pattern) 0 0 0
Condition0%0/3
$begin_pattern and not $finish_pattern
!ll&&!rl&&r
000
MC/DC0%0/2
$begin_pattern and not $finish_pattern
$begin_pattern not $finish_pattern
Truth table0%0/3
A and not(B)
A= $begin_patternB= not $finish_pattern
ABresult
0X0
100
111
25 {
26 0 $finish_pattern = $begin_pattern;
27 0 $one_separator_flag = 1;
28 }
29  
30 0 my $inside_flag = 0;
31 0 my $match_flag = 0;
32 0 my $next_line;
33 my $FILE;
34 0 ▶ if (defined $file) { open ($FILE, "<$file") || die "Error: Can't open file $file: $!"; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $file) { } 0 0 0
open $FILE, "<$file" or die "Error: Can't open file ${file}: $!" 0 0 0
35 0 else { $FILE = *STDIN; }
36  
37 0 MAIN_LOOP: while (1)
38 {
39 0 ▶ if ($next_line)
Branch: 1 branch
BranchTrueFalseTotal
if ($next_line) { } 0 0 0
40 {
41 0 $line = $next_line ;
42 0 $next_line = '';
43 }
44 else
45 {
46 0 $line = <$FILE> ;
47 0 ▶ unless ($line)
Branch: 1 branch
BranchTrueFalseTotal
unless ($line) 0 0 0
48 { # found EOF
49 0 ▶ $one_separator_flag && $match_flag && &_PrintMatchingGroup($numeration, $match_flag, $separator) ;
Branch: 1 branch
BranchTrueFalseTotal
$one_separator_flag and $match_flag and &_PrintMatchingGroup($numeration, $match_flag, $separator) 0 0 0
Condition0%0/3
$one_separator_flag and $match_flag
!ll&&!rl&&r
000
MC/DC0%0/2
$one_separator_flag and $match_flag
$one_separator_flag $match_flag
Truth table0%0/3
A and B
A= $one_separator_flagB= $match_flag
ABresult
0X0
100
111
50 0 exit 0;
51 }
52 }
53  
54 0 ▶ if ($inside_flag ) { $group_lines{$.} = $line }
Branch: 1 branch
BranchTrueFalseTotal
if ($inside_flag) { } 0 0 0
55 else
56 {
57 0 ▶ ($line !~ /$begin_pattern/) && next MAIN_LOOP;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /$begin_pattern/ or next MAIN_LOOP 0 0 0
58  
59 0 $inside_flag = 1 ;
60 0 $group_lines{$.} = $line ;
61 0 $line = $' ;
62 }
63  
64 # $inside_flag = 1
65 0 ▶ if ( (! $match_flag) && ($line =~ /$middle_pattern/) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $match_flag and $line =~ /$middle_pattern/) 0 0 0
Condition0%0/3
not $match_flag and $line =~ /$middle_pattern/
!ll&&!rl&&r
000
MC/DC0%0/2
not $match_flag and $line =~ /$middle_pattern/
not $match_flag $line =~ /$middle_pattern/
Truth table0%0/3
not(A) and B
A= not $match_flagB= $line =~ /$middle_pattern/
ABresult
0X0
100
111
66 {
67 0 $match_flag = $.
68 }
69  
70 0 ▶ if ( $line =~ /$finish_pattern/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$finish_pattern/) 0 0 0
71 {
72 0 $next_line = $' ;
73 0 ▶ $match_flag && &_PrintMatchingGroup($numeration, $match_flag, $separator) ;
Branch: 1 branch
BranchTrueFalseTotal
$match_flag and &_PrintMatchingGroup($numeration, $match_flag, $separator) 0 0 0
74 0 $match_flag = 0 ;
75 0 %group_lines = () ; # reset %group_lines
76  
77 0 ▶ unless ($one_separator_flag) { $inside_flag = 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($one_separator_flag) 0 0 0
78 }
79 } # end of "MAIN_LOOP: while(1)"
80 } # end of "sub file_multiline_grep"
81  
82 #-------------------------------------------------#
83 sub _PrintMatchingGroup
84 0 ▶{
Subroutine _PrintMatchingGroup: not called CC=5 SCAR=34.0
85 0 my ($numeration, $match_line_nr, $separator) = @_ ;
86 0 for my $line_nr (sort {$a <=> $b} keys %group_lines)
87 {
88 0 ▶ if ($numeration)
Branch: 1 branch
BranchTrueFalseTotal
if ($numeration) 0 0 0
89 {
90 0 ▶ if ($line_nr == $match_line_nr) { printf "%3d= ", $line_nr; }
Branch: 1 branch
BranchTrueFalseTotal
if ($line_nr == $match_line_nr) { } 0 0 0
91 0 else { printf "%3d: ", $line_nr; }
92 }
93 0 print $group_lines{ $line_nr };
94 }
95 0 ▶ defined $separator && print "$separator\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $separator and print "$separator\n" 0 0 0
96 } # end of "sub PrintMatchingGroup"
97  
98 #-------------------------------------------------#
99 2 1;
100  
101 __END__
↑ Summary