line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::MultilineGrep; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
24455
|
use 5.006; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
65
|
|
4
|
1
|
|
|
1
|
|
14
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
49
|
|
5
|
1
|
|
|
1
|
|
7
|
use warnings FATAL => 'all'; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
48
|
|
6
|
1
|
|
|
1
|
|
978
|
use English; |
|
1
|
|
|
|
|
4876
|
|
|
1
|
|
|
|
|
5
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Exporter; |
9
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
our @EXPORT = qw(file_multiline_grep); |
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
652
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
804
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my %group_lines = () ; # reset %group_lines |
16
|
|
|
|
|
|
|
my $line; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
#-------------------------------------------------# |
19
|
|
|
|
|
|
|
sub file_multiline_grep |
20
|
|
|
|
|
|
|
{ |
21
|
0
|
|
|
0
|
1
|
|
my ($begin_pattern, $finish_pattern, $middle_pattern, $numeration, $file, $separator) = @_ ; |
22
|
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
|
my $one_separator_flag = 0; |
24
|
0
|
0
|
0
|
|
|
|
if ($begin_pattern and ! $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
|
0
|
|
|
|
|
if (defined $file) { open ($FILE, "<$file") || die "Error: Can't open file $file: $!"; } |
|
0
|
0
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
else { $FILE = *STDIN; } |
36
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
|
MAIN_LOOP: while (1) |
38
|
|
|
|
|
|
|
{ |
39
|
0
|
0
|
|
|
|
|
if ($next_line) |
40
|
|
|
|
|
|
|
{ |
41
|
0
|
|
|
|
|
|
$line = $next_line ; |
42
|
0
|
|
|
|
|
|
$next_line = ''; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
else |
45
|
|
|
|
|
|
|
{ |
46
|
0
|
|
|
|
|
|
$line = <$FILE> ; |
47
|
0
|
0
|
|
|
|
|
unless ($line) |
48
|
|
|
|
|
|
|
{ # found EOF |
49
|
0
|
0
|
0
|
|
|
|
$one_separator_flag && $match_flag && &_PrintMatchingGroup($numeration, $match_flag, $separator) ; |
50
|
0
|
|
|
|
|
|
exit 0; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
0
|
0
|
|
|
|
|
if ($inside_flag ) { $group_lines{$.} = $line } |
|
0
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
else |
56
|
|
|
|
|
|
|
{ |
57
|
0
|
0
|
|
|
|
|
($line !~ /$begin_pattern/) && next MAIN_LOOP; |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
$inside_flag = 1 ; |
60
|
0
|
|
|
|
|
|
$group_lines{$.} = $line ; |
61
|
0
|
|
|
|
|
|
$line = $' ; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# $inside_flag = 1 |
65
|
0
|
0
|
0
|
|
|
|
if ( (! $match_flag) && ($line =~ /$middle_pattern/) ) |
66
|
|
|
|
|
|
|
{ |
67
|
0
|
|
|
|
|
|
$match_flag = $. |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
|
if ( $line =~ /$finish_pattern/ ) |
71
|
|
|
|
|
|
|
{ |
72
|
0
|
|
|
|
|
|
$next_line = $' ; |
73
|
0
|
0
|
|
|
|
|
$match_flag && &_PrintMatchingGroup($numeration, $match_flag, $separator) ; |
74
|
0
|
|
|
|
|
|
$match_flag = 0 ; |
75
|
0
|
|
|
|
|
|
%group_lines = () ; # reset %group_lines |
76
|
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
|
unless ($one_separator_flag) { $inside_flag = 0 } |
|
0
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} # end of "MAIN_LOOP: while(1)" |
80
|
|
|
|
|
|
|
} # end of "sub file_multiline_grep" |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
#-------------------------------------------------# |
83
|
|
|
|
|
|
|
sub _PrintMatchingGroup |
84
|
|
|
|
|
|
|
{ |
85
|
0
|
|
|
0
|
|
|
my ($numeration, $match_line_nr, $separator) = @_ ; |
86
|
0
|
|
|
|
|
|
for my $line_nr (sort {$a <=> $b} keys %group_lines) |
|
0
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
{ |
88
|
0
|
0
|
|
|
|
|
if ($numeration) |
89
|
|
|
|
|
|
|
{ |
90
|
0
|
0
|
|
|
|
|
if ($line_nr == $match_line_nr) { printf "%3d= ", $line_nr; } |
|
0
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
else { printf "%3d: ", $line_nr; } |
92
|
|
|
|
|
|
|
} |
93
|
0
|
|
|
|
|
|
print $group_lines{ $line_nr }; |
94
|
|
|
|
|
|
|
} |
95
|
0
|
0
|
|
|
|
|
defined $separator && print "$separator\n"; |
96
|
|
|
|
|
|
|
} # end of "sub PrintMatchingGroup" |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
#-------------------------------------------------# |
99
|
|
|
|
|
|
|
1; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
__END__ |