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.192451'; # TRIAL |
10
|
79
|
|
|
79
|
|
22926884
|
use v5.10; |
|
79
|
|
|
|
|
830
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# ABSTRACT: Line-by-line file access loops |
13
|
|
|
|
|
|
|
|
14
|
79
|
|
|
79
|
|
711
|
use strict; |
|
79
|
|
|
|
|
156
|
|
|
79
|
|
|
|
|
2308
|
|
15
|
79
|
|
|
79
|
|
377
|
use warnings; |
|
79
|
|
|
|
|
156
|
|
|
79
|
|
|
|
|
2248
|
|
16
|
79
|
|
|
79
|
|
1011
|
use autodie; |
|
79
|
|
|
|
|
14979
|
|
|
79
|
|
|
|
|
596
|
|
17
|
|
|
|
|
|
|
|
18
|
79
|
|
|
79
|
|
433759
|
use Carp; |
|
79
|
|
|
|
|
222
|
|
|
79
|
|
|
|
|
4714
|
|
19
|
79
|
|
|
79
|
|
469
|
use Fcntl; |
|
79
|
|
|
|
|
159
|
|
|
79
|
|
|
|
|
16679
|
|
20
|
79
|
|
|
79
|
|
44824
|
use File::ByLine::Object; |
|
79
|
|
|
|
|
222
|
|
|
79
|
|
|
|
|
3242
|
|
21
|
79
|
|
|
79
|
|
595
|
use Scalar::Util qw(reftype); |
|
79
|
|
|
|
|
158
|
|
|
79
|
|
|
|
|
87669
|
|
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
|
14472
|
my ( $code, $file ) = @_; |
44
|
|
|
|
|
|
|
|
45
|
3
|
|
|
|
|
17
|
return $OBJ->do( $code, $file ); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub forlines ($&) { |
50
|
2
|
|
|
2
|
1
|
7003
|
my ( $file, $code ) = @_; |
51
|
|
|
|
|
|
|
|
52
|
2
|
|
|
|
|
61
|
return $OBJ->do( $code, $file ); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub parallel_dolines (&$$) { |
57
|
323
|
|
|
323
|
1
|
1418807
|
my ( $code, $file, $procs ) = @_; |
58
|
|
|
|
|
|
|
|
59
|
323
|
100
|
|
|
|
1350
|
if ( !defined($procs) ) { |
60
|
61
|
|
|
|
|
8601
|
croak("Must include number of child processes"); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
262
|
50
|
|
|
|
987
|
if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } |
|
0
|
|
|
|
|
0
|
|
64
|
|
|
|
|
|
|
|
65
|
262
|
|
|
|
|
3094
|
my $byline = File::ByLine::Object->new(); |
66
|
262
|
|
|
|
|
1416
|
$byline->processes($procs); |
67
|
|
|
|
|
|
|
|
68
|
140
|
|
|
|
|
739
|
return $byline->do( $code, $file ); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub parallel_forlines ($$&) { |
73
|
184
|
|
|
184
|
1
|
523543
|
my ( $file, $procs, $code ) = @_; |
74
|
|
|
|
|
|
|
|
75
|
184
|
100
|
|
|
|
3002
|
if ( !defined($procs) ) { |
76
|
61
|
|
|
|
|
7198
|
croak("Must include number of child processes"); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
123
|
50
|
|
|
|
1430
|
if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } |
|
0
|
|
|
|
|
0
|
|
80
|
|
|
|
|
|
|
|
81
|
123
|
|
|
|
|
3633
|
my $byline = File::ByLine::Object->new(); |
82
|
123
|
|
|
|
|
1127
|
$byline->processes($procs); |
83
|
|
|
|
|
|
|
|
84
|
123
|
|
|
|
|
794
|
return $byline->do( $code, $file ); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub greplines (&$) { |
89
|
1
|
|
|
1
|
1
|
3434
|
my ( $code, $file ) = @_; |
90
|
|
|
|
|
|
|
|
91
|
1
|
|
|
|
|
8
|
return $OBJ->grep( $code, $file ); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub parallel_greplines (&$$) { |
96
|
130
|
|
|
130
|
1
|
209497
|
my ( $code, $file, $procs ) = @_; |
97
|
|
|
|
|
|
|
|
98
|
130
|
100
|
|
|
|
547
|
if ( !defined($procs) ) { |
99
|
61
|
|
|
|
|
5978
|
croak("Must include number of child processes"); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
69
|
50
|
|
|
|
458
|
if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } |
|
0
|
|
|
|
|
0
|
|
103
|
|
|
|
|
|
|
|
104
|
69
|
|
|
|
|
1330
|
my $byline = File::ByLine::Object->new(); |
105
|
69
|
|
|
|
|
569
|
$byline->processes($procs); |
106
|
|
|
|
|
|
|
|
107
|
69
|
|
|
|
|
352
|
return $byline->grep( $code, $file ); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub maplines (&$) { |
112
|
2
|
|
|
2
|
1
|
6561
|
my ( $code, $file ) = @_; |
113
|
|
|
|
|
|
|
|
114
|
2
|
|
|
|
|
12
|
return $OBJ->map( $code, $file ); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub parallel_maplines (&$$) { |
119
|
170
|
|
|
170
|
1
|
476322
|
my ( $code, $file, $procs ) = @_; |
120
|
|
|
|
|
|
|
|
121
|
170
|
100
|
|
|
|
741
|
if ( !defined($procs) ) { |
122
|
61
|
|
|
|
|
5856
|
croak("Must include number of child processes"); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
109
|
50
|
|
|
|
913
|
if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } |
|
0
|
|
|
|
|
0
|
|
126
|
|
|
|
|
|
|
|
127
|
109
|
|
|
|
|
2194
|
my $byline = File::ByLine::Object->new(); |
128
|
109
|
|
|
|
|
1087
|
$byline->processes($procs); |
129
|
|
|
|
|
|
|
|
130
|
109
|
|
|
|
|
805
|
return $byline->map( $code, $file ); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub readlines ($) { |
135
|
14
|
|
|
14
|
1
|
5135
|
my ($file) = @_; |
136
|
|
|
|
|
|
|
|
137
|
14
|
|
|
|
|
168
|
return $OBJ->lines($file); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub writefile ($@) { |
142
|
3
|
|
|
3
|
1
|
19653
|
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
|
|
|
|
8
|
if (@lines) { $lines[-1] =~ s/\n$//s; } |
|
3
|
|
|
|
|
9
|
|
147
|
|
|
|
|
|
|
|
148
|
3
|
|
|
|
|
13
|
open my $fh, '>', $file; |
149
|
3
|
|
|
|
|
2559
|
foreach my $line (@lines) { |
150
|
7
|
|
|
|
|
44
|
say $fh $line; |
151
|
|
|
|
|
|
|
} |
152
|
3
|
|
|
|
|
11
|
close $fh; |
153
|
|
|
|
|
|
|
|
154
|
3
|
|
|
|
|
940
|
return; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub appendfile ($@) { |
159
|
4
|
|
|
4
|
1
|
16105
|
my ( $file, @lines ) = @_; |
160
|
4
|
50
|
|
|
|
14
|
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
|
|
|
|
9
|
if (@lines) { $lines[-1] =~ s/\n$//s; } |
|
4
|
|
|
|
|
11
|
|
164
|
|
|
|
|
|
|
|
165
|
4
|
|
|
|
|
18
|
open my $fh, '>>', $file; |
166
|
4
|
|
|
|
|
2553
|
foreach my $line (@lines) { |
167
|
10
|
|
|
|
|
52
|
say $fh $line; |
168
|
|
|
|
|
|
|
} |
169
|
4
|
|
|
|
|
16
|
close $fh; |
170
|
|
|
|
|
|
|
|
171
|
4
|
|
|
|
|
1031
|
return; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# |
175
|
|
|
|
|
|
|
# Object Oriented Interface |
176
|
|
|
|
|
|
|
# |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub new { |
179
|
172
|
|
|
172
|
1
|
1165401
|
shift; # Remove the first parameter because we want to specify the class. |
180
|
|
|
|
|
|
|
|
181
|
172
|
|
|
|
|
3113
|
return File::ByLine::Object->new(@_); |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
1; |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
__END__ |