line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Copyright (C) 2018 Joelle Maslak |
5
|
|
|
|
|
|
|
# All Rights Reserved - See License |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
package File::ByLine; |
9
|
|
|
|
|
|
|
$File::ByLine::VERSION = '1.192590'; |
10
|
79
|
|
|
79
|
|
18820521
|
use v5.10; |
|
79
|
|
|
|
|
766
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# ABSTRACT: Line-by-line file access loops |
13
|
|
|
|
|
|
|
|
14
|
79
|
|
|
79
|
|
1369
|
use strict; |
|
79
|
|
|
|
|
152
|
|
|
79
|
|
|
|
|
1519
|
|
15
|
79
|
|
|
79
|
|
374
|
use warnings; |
|
79
|
|
|
|
|
145
|
|
|
79
|
|
|
|
|
1678
|
|
16
|
79
|
|
|
79
|
|
789
|
use autodie; |
|
79
|
|
|
|
|
11794
|
|
|
79
|
|
|
|
|
453
|
|
17
|
|
|
|
|
|
|
|
18
|
79
|
|
|
79
|
|
342229
|
use Carp; |
|
79
|
|
|
|
|
156
|
|
|
79
|
|
|
|
|
3716
|
|
19
|
79
|
|
|
79
|
|
396
|
use Fcntl; |
|
79
|
|
|
|
|
144
|
|
|
79
|
|
|
|
|
14023
|
|
20
|
79
|
|
|
79
|
|
33448
|
use File::ByLine::Object; |
|
79
|
|
|
|
|
220
|
|
|
79
|
|
|
|
|
2940
|
|
21
|
79
|
|
|
79
|
|
472
|
use Scalar::Util qw(reftype); |
|
79
|
|
|
|
|
215
|
|
|
79
|
|
|
|
|
74317
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Object with default options |
24
|
|
|
|
|
|
|
our $OBJ = File::ByLine::Object->new(); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# |
28
|
|
|
|
|
|
|
# Exports |
29
|
|
|
|
|
|
|
# |
30
|
|
|
|
|
|
|
require Exporter; |
31
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
## no critic (Modules::ProhibitAutomaticExportation) |
34
|
|
|
|
|
|
|
our @EXPORT = |
35
|
|
|
|
|
|
|
qw(dolines forlines greplines maplines parallel_dolines parallel_forlines parallel_greplines parallel_maplines readlines writefile appendfile); |
36
|
|
|
|
|
|
|
## use critic |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
our @EXPORT_OK = |
39
|
|
|
|
|
|
|
qw(dolines forlines greplines maplines parallel_dolines parallel_forlines parallel_greplines parallel_maplines readlines writefile appendfile); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub dolines (&$) { |
43
|
3
|
|
|
3
|
1
|
12036
|
my ( $code, $file ) = @_; |
44
|
|
|
|
|
|
|
|
45
|
3
|
|
|
|
|
18
|
return $OBJ->do( $code, $file ); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub forlines ($&) { |
50
|
2
|
|
|
2
|
1
|
5755
|
my ( $file, $code ) = @_; |
51
|
|
|
|
|
|
|
|
52
|
2
|
|
|
|
|
7
|
return $OBJ->do( $code, $file ); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub parallel_dolines (&$$) { |
57
|
323
|
|
|
323
|
1
|
1218834
|
my ( $code, $file, $procs ) = @_; |
58
|
|
|
|
|
|
|
|
59
|
323
|
100
|
|
|
|
1048
|
if ( !defined($procs) ) { |
60
|
61
|
|
|
|
|
7381
|
croak("Must include number of child processes"); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
262
|
50
|
|
|
|
614
|
if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } |
|
0
|
|
|
|
|
0
|
|
64
|
|
|
|
|
|
|
|
65
|
262
|
|
|
|
|
2442
|
my $byline = File::ByLine::Object->new(); |
66
|
262
|
|
|
|
|
1268
|
$byline->processes($procs); |
67
|
|
|
|
|
|
|
|
68
|
140
|
|
|
|
|
610
|
return $byline->do( $code, $file ); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub parallel_forlines ($$&) { |
73
|
184
|
|
|
184
|
1
|
539176
|
my ( $file, $procs, $code ) = @_; |
74
|
|
|
|
|
|
|
|
75
|
184
|
100
|
|
|
|
1054
|
if ( !defined($procs) ) { |
76
|
61
|
|
|
|
|
5429
|
croak("Must include number of child processes"); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
123
|
50
|
|
|
|
1360
|
if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } |
|
0
|
|
|
|
|
0
|
|
80
|
|
|
|
|
|
|
|
81
|
123
|
|
|
|
|
3686
|
my $byline = File::ByLine::Object->new(); |
82
|
123
|
|
|
|
|
1724
|
$byline->processes($procs); |
83
|
|
|
|
|
|
|
|
84
|
123
|
|
|
|
|
863
|
return $byline->do( $code, $file ); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub greplines (&$) { |
89
|
1
|
|
|
1
|
1
|
2841
|
my ( $code, $file ) = @_; |
90
|
|
|
|
|
|
|
|
91
|
1
|
|
|
|
|
6
|
return $OBJ->grep( $code, $file ); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub parallel_greplines (&$$) { |
96
|
130
|
|
|
130
|
1
|
232849
|
my ( $code, $file, $procs ) = @_; |
97
|
|
|
|
|
|
|
|
98
|
130
|
100
|
|
|
|
555
|
if ( !defined($procs) ) { |
99
|
61
|
|
|
|
|
5307
|
croak("Must include number of child processes"); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
69
|
50
|
|
|
|
598
|
if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } |
|
0
|
|
|
|
|
0
|
|
103
|
|
|
|
|
|
|
|
104
|
69
|
|
|
|
|
1431
|
my $byline = File::ByLine::Object->new(); |
105
|
69
|
|
|
|
|
865
|
$byline->processes($procs); |
106
|
|
|
|
|
|
|
|
107
|
69
|
|
|
|
|
535
|
return $byline->grep( $code, $file ); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub maplines (&$) { |
112
|
2
|
|
|
2
|
1
|
5253
|
my ( $code, $file ) = @_; |
113
|
|
|
|
|
|
|
|
114
|
2
|
|
|
|
|
9
|
return $OBJ->map( $code, $file ); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub parallel_maplines (&$$) { |
119
|
170
|
|
|
170
|
1
|
473308
|
my ( $code, $file, $procs ) = @_; |
120
|
|
|
|
|
|
|
|
121
|
170
|
100
|
|
|
|
710
|
if ( !defined($procs) ) { |
122
|
61
|
|
|
|
|
4636
|
croak("Must include number of child processes"); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
109
|
50
|
|
|
|
863
|
if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } |
|
0
|
|
|
|
|
0
|
|
126
|
|
|
|
|
|
|
|
127
|
109
|
|
|
|
|
2389
|
my $byline = File::ByLine::Object->new(); |
128
|
109
|
|
|
|
|
1286
|
$byline->processes($procs); |
129
|
|
|
|
|
|
|
|
130
|
109
|
|
|
|
|
648
|
return $byline->map( $code, $file ); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub readlines ($) { |
135
|
14
|
|
|
14
|
1
|
4315
|
my ($file) = @_; |
136
|
|
|
|
|
|
|
|
137
|
14
|
|
|
|
|
162
|
return $OBJ->lines($file); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub writefile ($@) { |
142
|
3
|
|
|
3
|
1
|
14506
|
my ( $file, @lines ) = @_; |
143
|
3
|
50
|
|
|
|
9
|
if ( !defined($file) ) { die("Must define the filename"); } |
|
0
|
|
|
|
|
0
|
|
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# Last line should have it's newline removed, if applicable |
146
|
3
|
50
|
|
|
|
10
|
if (@lines) { $lines[-1] =~ s/\n$//s; } |
|
3
|
|
|
|
|
8
|
|
147
|
|
|
|
|
|
|
|
148
|
3
|
|
|
|
|
15
|
open my $fh, '>', $file; |
149
|
3
|
|
|
|
|
2417
|
foreach my $line (@lines) { |
150
|
7
|
|
|
|
|
44
|
say $fh $line; |
151
|
|
|
|
|
|
|
} |
152
|
3
|
|
|
|
|
12
|
close $fh; |
153
|
|
|
|
|
|
|
|
154
|
3
|
|
|
|
|
961
|
return; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub appendfile ($@) { |
159
|
4
|
|
|
4
|
1
|
15442
|
my ( $file, @lines ) = @_; |
160
|
4
|
50
|
|
|
|
11
|
if ( !defined($file) ) { die("Must define the filename"); } |
|
0
|
|
|
|
|
0
|
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# Last line should have it's newline removed, if applicable |
163
|
4
|
50
|
|
|
|
11
|
if (@lines) { $lines[-1] =~ s/\n$//s; } |
|
4
|
|
|
|
|
10
|
|
164
|
|
|
|
|
|
|
|
165
|
4
|
|
|
|
|
14
|
open my $fh, '>>', $file; |
166
|
4
|
|
|
|
|
2468
|
foreach my $line (@lines) { |
167
|
10
|
|
|
|
|
53
|
say $fh $line; |
168
|
|
|
|
|
|
|
} |
169
|
4
|
|
|
|
|
15
|
close $fh; |
170
|
|
|
|
|
|
|
|
171
|
4
|
|
|
|
|
1030
|
return; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# |
175
|
|
|
|
|
|
|
# Object Oriented Interface |
176
|
|
|
|
|
|
|
# |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub new { |
179
|
172
|
|
|
172
|
1
|
1184066
|
shift; # Remove the first parameter because we want to specify the class. |
180
|
|
|
|
|
|
|
|
181
|
172
|
|
|
|
|
3058
|
return File::ByLine::Object->new(@_); |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
1; |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
__END__ |