| 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
Condition0%0/3
$begin_pattern and not $finish_pattern
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 35 | 0 | else { $FILE = *STDIN; } | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | MAIN_LOOP: while (1) | |||||||||||||||||||||||||||
| 38 | { | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | if ($next_line) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 48 | { # found EOF | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $one_separator_flag && $match_flag && &_PrintMatchingGroup($numeration, $match_flag, $separator) ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$one_separator_flag and $match_flag
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
| |||||||||||||||||||||||||||||
| 50 | 0 | exit 0; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ($inside_flag ) { $group_lines{$.} = $line } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | else | ||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | ($line !~ /$begin_pattern/) && next MAIN_LOOP; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
not $match_flag and $line =~ /$middle_pattern/
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/
| |||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||
| 67 | 0 | $match_flag = $. | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | if ( $line =~ /$finish_pattern/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||
| 72 | 0 | $next_line = $' ; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | $match_flag && &_PrintMatchingGroup($numeration, $match_flag, $separator) ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ($line_nr == $match_line_nr) { printf "%3d= ", $line_nr; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 96 | } # end of "sub PrintMatchingGroup" | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | #-------------------------------------------------# | ||||||||||||||||||||||||||||
| 99 | 2 | 1; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | __END__ | ||||||||||||||||||||||||||||