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.192450'; # TRIAL |
10
|
79
|
|
|
79
|
|
23356868
|
use v5.10; |
|
79
|
|
|
|
|
784
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# ABSTRACT: Line-by-line file access loops |
13
|
|
|
|
|
|
|
|
14
|
79
|
|
|
79
|
|
529
|
use strict; |
|
79
|
|
|
|
|
158
|
|
|
79
|
|
|
|
|
1956
|
|
15
|
79
|
|
|
79
|
|
330
|
use warnings; |
|
79
|
|
|
|
|
154
|
|
|
79
|
|
|
|
|
1911
|
|
16
|
79
|
|
|
79
|
|
330
|
use autodie; |
|
79
|
|
|
|
|
146
|
|
|
79
|
|
|
|
|
603
|
|
17
|
|
|
|
|
|
|
|
18
|
79
|
|
|
79
|
|
413457
|
use Carp; |
|
79
|
|
|
|
|
160
|
|
|
79
|
|
|
|
|
5325
|
|
19
|
79
|
|
|
79
|
|
553
|
use Fcntl; |
|
79
|
|
|
|
|
97
|
|
|
79
|
|
|
|
|
17182
|
|
20
|
79
|
|
|
79
|
|
42355
|
use File::ByLine::Object; |
|
79
|
|
|
|
|
222
|
|
|
79
|
|
|
|
|
3019
|
|
21
|
79
|
|
|
79
|
|
553
|
use Scalar::Util qw(reftype); |
|
79
|
|
|
|
|
170
|
|
|
79
|
|
|
|
|
94955
|
|
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
|
14957
|
my ( $code, $file ) = @_; |
44
|
|
|
|
|
|
|
|
45
|
3
|
|
|
|
|
15
|
return $OBJ->do( $code, $file ); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub forlines ($&) { |
50
|
2
|
|
|
2
|
1
|
7021
|
my ( $file, $code ) = @_; |
51
|
|
|
|
|
|
|
|
52
|
2
|
|
|
|
|
10
|
return $OBJ->do( $code, $file ); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub parallel_dolines (&$$) { |
57
|
323
|
|
|
323
|
1
|
1531303
|
my ( $code, $file, $procs ) = @_; |
58
|
|
|
|
|
|
|
|
59
|
323
|
100
|
|
|
|
1299
|
if ( !defined($procs) ) { |
60
|
61
|
|
|
|
|
8906
|
croak("Must include number of child processes"); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
262
|
50
|
|
|
|
1048
|
if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } |
|
0
|
|
|
|
|
0
|
|
64
|
|
|
|
|
|
|
|
65
|
262
|
|
|
|
|
3251
|
my $byline = File::ByLine::Object->new(); |
66
|
262
|
|
|
|
|
1694
|
$byline->processes($procs); |
67
|
|
|
|
|
|
|
|
68
|
140
|
|
|
|
|
682
|
return $byline->do( $code, $file ); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub parallel_forlines ($$&) { |
73
|
184
|
|
|
184
|
1
|
556879
|
my ( $file, $procs, $code ) = @_; |
74
|
|
|
|
|
|
|
|
75
|
184
|
100
|
|
|
|
981
|
if ( !defined($procs) ) { |
76
|
61
|
|
|
|
|
6466
|
croak("Must include number of child processes"); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
123
|
50
|
|
|
|
1532
|
if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } |
|
0
|
|
|
|
|
0
|
|
80
|
|
|
|
|
|
|
|
81
|
123
|
|
|
|
|
4254
|
my $byline = File::ByLine::Object->new(); |
82
|
123
|
|
|
|
|
1727
|
$byline->processes($procs); |
83
|
|
|
|
|
|
|
|
84
|
123
|
|
|
|
|
1112
|
return $byline->do( $code, $file ); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub greplines (&$) { |
89
|
1
|
|
|
1
|
1
|
3350
|
my ( $code, $file ) = @_; |
90
|
|
|
|
|
|
|
|
91
|
1
|
|
|
|
|
8
|
return $OBJ->grep( $code, $file ); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub parallel_greplines (&$$) { |
96
|
130
|
|
|
130
|
1
|
202718
|
my ( $code, $file, $procs ) = @_; |
97
|
|
|
|
|
|
|
|
98
|
130
|
100
|
|
|
|
469
|
if ( !defined($procs) ) { |
99
|
61
|
|
|
|
|
9821
|
croak("Must include number of child processes"); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
69
|
50
|
|
|
|
466
|
if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } |
|
0
|
|
|
|
|
0
|
|
103
|
|
|
|
|
|
|
|
104
|
69
|
|
|
|
|
1370
|
my $byline = File::ByLine::Object->new(); |
105
|
69
|
|
|
|
|
642
|
$byline->processes($procs); |
106
|
|
|
|
|
|
|
|
107
|
69
|
|
|
|
|
276
|
return $byline->grep( $code, $file ); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub maplines (&$) { |
112
|
2
|
|
|
2
|
1
|
6901
|
my ( $code, $file ) = @_; |
113
|
|
|
|
|
|
|
|
114
|
2
|
|
|
|
|
11
|
return $OBJ->map( $code, $file ); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub parallel_maplines (&$$) { |
119
|
170
|
|
|
170
|
1
|
471134
|
my ( $code, $file, $procs ) = @_; |
120
|
|
|
|
|
|
|
|
121
|
170
|
100
|
|
|
|
671
|
if ( !defined($procs) ) { |
122
|
61
|
|
|
|
|
7442
|
croak("Must include number of child processes"); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
109
|
50
|
|
|
|
820
|
if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } |
|
0
|
|
|
|
|
0
|
|
126
|
|
|
|
|
|
|
|
127
|
109
|
|
|
|
|
2468
|
my $byline = File::ByLine::Object->new(); |
128
|
109
|
|
|
|
|
1255
|
$byline->processes($procs); |
129
|
|
|
|
|
|
|
|
130
|
109
|
|
|
|
|
610
|
return $byline->map( $code, $file ); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub readlines ($) { |
135
|
14
|
|
|
14
|
1
|
5093
|
my ($file) = @_; |
136
|
|
|
|
|
|
|
|
137
|
14
|
|
|
|
|
175
|
return $OBJ->lines($file); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub writefile ($@) { |
142
|
3
|
|
|
3
|
1
|
18228
|
my ( $file, @lines ) = @_; |
143
|
3
|
50
|
|
|
|
14
|
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
|
|
|
|
9
|
if (@lines) { $lines[-1] =~ s/\n$//s; } |
|
3
|
|
|
|
|
10
|
|
147
|
|
|
|
|
|
|
|
148
|
3
|
|
|
|
|
17
|
open my $fh, '>', $file; |
149
|
3
|
|
|
|
|
3164
|
foreach my $line (@lines) { |
150
|
7
|
|
|
|
|
57
|
say $fh $line; |
151
|
|
|
|
|
|
|
} |
152
|
3
|
|
|
|
|
15
|
close $fh; |
153
|
|
|
|
|
|
|
|
154
|
3
|
|
|
|
|
1165
|
return; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub appendfile ($@) { |
159
|
4
|
|
|
4
|
1
|
22482
|
my ( $file, @lines ) = @_; |
160
|
4
|
50
|
|
|
|
16
|
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
|
|
|
|
15
|
if (@lines) { $lines[-1] =~ s/\n$//s; } |
|
4
|
|
|
|
|
15
|
|
164
|
|
|
|
|
|
|
|
165
|
4
|
|
|
|
|
22
|
open my $fh, '>>', $file; |
166
|
4
|
|
|
|
|
3555
|
foreach my $line (@lines) { |
167
|
10
|
|
|
|
|
100
|
say $fh $line; |
168
|
|
|
|
|
|
|
} |
169
|
4
|
|
|
|
|
19
|
close $fh; |
170
|
|
|
|
|
|
|
|
171
|
4
|
|
|
|
|
1357
|
return; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# |
175
|
|
|
|
|
|
|
# Object Oriented Interface |
176
|
|
|
|
|
|
|
# |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub new { |
179
|
172
|
|
|
172
|
1
|
1146773
|
shift; # Remove the first parameter because we want to specify the class. |
180
|
|
|
|
|
|
|
|
181
|
172
|
|
|
|
|
3490
|
return File::ByLine::Object->new(@_); |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
1; |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
__END__ |