| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package App::RecordStream::InputStream; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
App::RecordStream::InputStream |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 AUTHOR |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Benjamin Bernard |
|
10
|
|
|
|
|
|
|
Keith Amling |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
This module will generate an stream of App::RecordStream::Record objects for given inputs. |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use App::RecordStream::InputStream; |
|
19
|
|
|
|
|
|
|
my $stream = App::RecordStream::InputStream(STRING => $recs_string); |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
while ( my $record = $stream->get_record() ) { |
|
22
|
|
|
|
|
|
|
... do stuff ... |
|
23
|
|
|
|
|
|
|
} |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=over 4 |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=item my $in = App::RecordStream::InputStream->new(OPTIONS); |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
The input stream takes named parameters, it will take one of: FILE, STRING, or FH |
|
32
|
|
|
|
|
|
|
(a file handle). |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
FILE - Name of a file, must be readable |
|
35
|
|
|
|
|
|
|
STRING - String of new line separated records |
|
36
|
|
|
|
|
|
|
FH - File handle to a stream of data |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
Optionally, it wil take a NEXT argument. The NEXT argument should be another |
|
39
|
|
|
|
|
|
|
InputStream object. Once the returned object reaches the end of its string, it |
|
40
|
|
|
|
|
|
|
will get records from the NEXT App::RecordStream::InputStream. In this manner, InputStream |
|
41
|
|
|
|
|
|
|
objects can be chained |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
returns an instance of InputStream |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=item my $in = App::RecordStream::InputStream->new_magic() |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Provides GNU-style input semantics for scripts. If there are arguments left in |
|
48
|
|
|
|
|
|
|
@ARGV, it will assume those are file names and make a set of chained streams |
|
49
|
|
|
|
|
|
|
for those files, returning the first stream. If no files are specified, will |
|
50
|
|
|
|
|
|
|
open an InputStream on STDIN |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=item my $in = App::RecordStream::InpustStream->new_from_files(FILES) |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Takes an array of FILES and constructs a set of chained streams for those |
|
55
|
|
|
|
|
|
|
files. Returns the first stream |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=back |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head1 PUBLIC METHODS |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=over 4 |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item my $record = $this->get_record(); |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Retrieve the next L from the stream. Will return a false value |
|
67
|
|
|
|
|
|
|
if no records are available. If this stream has a NEXT stream specified in the |
|
68
|
|
|
|
|
|
|
constructor, this will continue to return Record objects until all chained |
|
69
|
|
|
|
|
|
|
streams are exhausted |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=back |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=cut |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
our $VERSION = "4.0.25"; |
|
76
|
|
|
|
|
|
|
|
|
77
|
46
|
|
|
46
|
|
56674
|
use strict; |
|
|
46
|
|
|
|
|
75
|
|
|
|
46
|
|
|
|
|
925
|
|
|
78
|
46
|
|
|
46
|
|
166
|
use warnings; |
|
|
46
|
|
|
|
|
80
|
|
|
|
46
|
|
|
|
|
865
|
|
|
79
|
|
|
|
|
|
|
|
|
80
|
46
|
|
|
46
|
|
16075
|
use IO::String; |
|
|
46
|
|
|
|
|
121939
|
|
|
|
46
|
|
|
|
|
1019
|
|
|
81
|
46
|
|
|
46
|
|
14972
|
use JSON::MaybeXS; |
|
|
46
|
|
|
|
|
241205
|
|
|
|
46
|
|
|
|
|
2064
|
|
|
82
|
|
|
|
|
|
|
|
|
83
|
46
|
|
|
46
|
|
14830
|
use App::RecordStream::Record; |
|
|
46
|
|
|
|
|
111
|
|
|
|
46
|
|
|
|
|
31056
|
|
|
84
|
|
|
|
|
|
|
require App::RecordStream::Operation; |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $json = JSON->new; |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
my $ONE_OF = [qw(FH STRING FILE)]; |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
my $ARGUMENTS = { |
|
91
|
|
|
|
|
|
|
FH => 0, |
|
92
|
|
|
|
|
|
|
STRING => 0, |
|
93
|
|
|
|
|
|
|
FILE => 0, |
|
94
|
|
|
|
|
|
|
NEXT => 0, |
|
95
|
|
|
|
|
|
|
}; |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub new_magic { |
|
98
|
1
|
|
|
1
|
1
|
3
|
my $class = shift; |
|
99
|
1
|
|
50
|
|
|
5
|
my $files = shift || \@ARGV; |
|
100
|
|
|
|
|
|
|
|
|
101
|
1
|
50
|
|
|
|
3
|
if ( scalar @$files > 0 ) { |
|
102
|
1
|
|
|
|
|
4
|
return $class->new_from_files($files); |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
0
|
return $class->new(FH => \*STDIN); |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub new_from_files { |
|
109
|
2
|
|
|
2
|
1
|
4
|
my $class = shift; |
|
110
|
2
|
|
|
|
|
3
|
my $files = shift; |
|
111
|
|
|
|
|
|
|
|
|
112
|
2
|
|
|
|
|
2
|
my $last_stream; |
|
113
|
|
|
|
|
|
|
|
|
114
|
2
|
|
|
|
|
4
|
foreach my $file ( reverse @$files ) { |
|
115
|
4
|
50
|
33
|
|
|
95
|
unless ( -e $file && -r $file ) { |
|
116
|
0
|
|
|
|
|
0
|
die "File does not exist or is not readable: $file\n"; |
|
117
|
|
|
|
|
|
|
} |
|
118
|
|
|
|
|
|
|
|
|
119
|
4
|
|
|
|
|
16
|
my $new_stream = $class->new(FILE => $file, NEXT => $last_stream); |
|
120
|
4
|
|
|
|
|
8
|
$last_stream = $new_stream; |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
2
|
|
|
|
|
11
|
return $last_stream; |
|
124
|
|
|
|
|
|
|
} |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub new { |
|
127
|
255
|
|
|
255
|
1
|
586
|
my $class = shift; |
|
128
|
255
|
|
|
|
|
606
|
my %args = @_; |
|
129
|
|
|
|
|
|
|
|
|
130
|
255
|
|
|
|
|
454
|
my $this = {}; |
|
131
|
|
|
|
|
|
|
|
|
132
|
255
|
|
|
|
|
807
|
foreach my $key (keys %$ARGUMENTS) { |
|
133
|
1020
|
|
|
|
|
1250
|
my $value = $args{$key}; |
|
134
|
1020
|
|
|
|
|
1379
|
$this->{$key} = $value; |
|
135
|
|
|
|
|
|
|
|
|
136
|
1020
|
50
|
|
|
|
1689
|
if ( $ARGUMENTS->{$key} ) { |
|
137
|
0
|
0
|
|
|
|
0
|
die "Did not supply required argument: $key" unless ( $value ); |
|
138
|
|
|
|
|
|
|
} |
|
139
|
|
|
|
|
|
|
} |
|
140
|
|
|
|
|
|
|
|
|
141
|
255
|
|
|
|
|
440
|
bless $this, $class; |
|
142
|
|
|
|
|
|
|
|
|
143
|
255
|
|
|
|
|
690
|
$this->_init(); |
|
144
|
255
|
|
|
|
|
967
|
return $this; |
|
145
|
|
|
|
|
|
|
} |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub _init { |
|
148
|
255
|
|
|
255
|
|
335
|
my $this = shift; |
|
149
|
|
|
|
|
|
|
|
|
150
|
255
|
|
|
|
|
353
|
my $found = {}; |
|
151
|
|
|
|
|
|
|
|
|
152
|
255
|
|
|
|
|
488
|
foreach my $arg (@$ONE_OF) { |
|
153
|
765
|
100
|
|
|
|
1424
|
if ( $this->{$arg} ) { |
|
154
|
255
|
|
|
|
|
495
|
$found->{$arg} = $this->{$arg}; |
|
155
|
|
|
|
|
|
|
} |
|
156
|
|
|
|
|
|
|
} |
|
157
|
|
|
|
|
|
|
|
|
158
|
255
|
50
|
|
|
|
647
|
if ( scalar keys %$found > 1 ) { |
|
159
|
0
|
|
|
|
|
0
|
die "Must specify only one of " . join(' ', keys %$found); |
|
160
|
|
|
|
|
|
|
} |
|
161
|
|
|
|
|
|
|
|
|
162
|
255
|
50
|
|
|
|
614
|
unless ( scalar keys %$found == 1 ) { |
|
163
|
0
|
|
|
|
|
0
|
die "Must specify one of " . join(' ', @$ONE_OF); |
|
164
|
|
|
|
|
|
|
} |
|
165
|
|
|
|
|
|
|
|
|
166
|
255
|
100
|
|
|
|
499
|
if ( $this->get_string() ) { |
|
167
|
238
|
|
|
|
|
409
|
$this->{'FH'} = IO::String->new($this->get_string()); |
|
168
|
|
|
|
|
|
|
} |
|
169
|
|
|
|
|
|
|
|
|
170
|
255
|
|
|
|
|
8508
|
my $file = $this->get_file(); |
|
171
|
255
|
100
|
|
|
|
755
|
if ( $file ) { |
|
172
|
10
|
50
|
|
|
|
334
|
open(my $fh, '<', $file) or die "Cannot open $file: $!"; |
|
173
|
10
|
|
|
|
|
47
|
$this->{'FH'} = $fh; |
|
174
|
|
|
|
|
|
|
} |
|
175
|
|
|
|
|
|
|
} |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub get_file { |
|
178
|
377
|
|
|
377
|
0
|
491
|
my $this = shift; |
|
179
|
377
|
|
|
|
|
683
|
return $this->{'FILE'}; |
|
180
|
|
|
|
|
|
|
} |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub get_string { |
|
183
|
611
|
|
|
611
|
0
|
690
|
my $this = shift; |
|
184
|
611
|
|
|
|
|
1904
|
return $this->{'STRING'}; |
|
185
|
|
|
|
|
|
|
} |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
# Performance! :( |
|
188
|
|
|
|
|
|
|
sub get_fh { |
|
189
|
1498
|
|
|
1498
|
0
|
1872
|
return $_[0]->{'FH'}; |
|
190
|
|
|
|
|
|
|
} |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub get_record { |
|
193
|
1502
|
|
|
1502
|
1
|
3010
|
my $this = shift; |
|
194
|
|
|
|
|
|
|
|
|
195
|
1502
|
100
|
|
|
|
1931
|
if ( $this->is_done() ) { |
|
196
|
4
|
|
|
|
|
7
|
return $this->call_next_record(); |
|
197
|
|
|
|
|
|
|
} |
|
198
|
|
|
|
|
|
|
|
|
199
|
1498
|
|
|
|
|
2034
|
my $fh = $this->get_fh(); |
|
200
|
|
|
|
|
|
|
|
|
201
|
1498
|
|
|
|
|
4196
|
my $line = <$fh>; |
|
202
|
|
|
|
|
|
|
|
|
203
|
1498
|
100
|
|
|
|
24009
|
if ( ! $line ) { |
|
204
|
245
|
|
|
|
|
787
|
close $fh; |
|
205
|
245
|
|
|
|
|
2128
|
$this->set_done(); |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
# This is ugly, reaching into the other class |
|
208
|
245
|
|
|
|
|
395
|
App::RecordStream::Operation::set_current_filename($this->get_filename()); |
|
209
|
|
|
|
|
|
|
|
|
210
|
245
|
|
|
|
|
467
|
return $this->call_next_record(); |
|
211
|
|
|
|
|
|
|
} |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
# Direct bless done in the name of performance |
|
214
|
1253
|
|
|
|
|
4964
|
my $record = $json->decode($line); |
|
215
|
1253
|
|
|
|
|
2027
|
bless $record, 'App::RecordStream::Record'; |
|
216
|
|
|
|
|
|
|
|
|
217
|
1253
|
|
|
|
|
2844
|
return $record; |
|
218
|
|
|
|
|
|
|
} |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
sub call_next_record { |
|
221
|
249
|
|
|
249
|
0
|
318
|
my $this = shift; |
|
222
|
|
|
|
|
|
|
|
|
223
|
249
|
|
|
|
|
382
|
my $next = $this->get_next(); |
|
224
|
|
|
|
|
|
|
|
|
225
|
249
|
100
|
|
|
|
445
|
unless ( $next ) { |
|
226
|
243
|
|
|
|
|
674
|
return undef; |
|
227
|
|
|
|
|
|
|
} |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
# Prevent a deep recursion with many passed files |
|
230
|
6
|
50
|
33
|
|
|
13
|
if ( $next && $next->is_done() ) { |
|
231
|
0
|
|
|
|
|
0
|
$next = $next->get_next(); |
|
232
|
0
|
|
|
|
|
0
|
$this->{'NEXT'} = $next; |
|
233
|
|
|
|
|
|
|
} |
|
234
|
|
|
|
|
|
|
|
|
235
|
6
|
|
|
|
|
12
|
return $next->get_record(); |
|
236
|
|
|
|
|
|
|
} |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
sub get_filename { |
|
239
|
365
|
|
|
365
|
0
|
452
|
my $this = shift; |
|
240
|
|
|
|
|
|
|
|
|
241
|
365
|
100
|
|
|
|
627
|
if ( ! $this->is_done() ) { |
|
|
|
100
|
|
|
|
|
|
|
242
|
120
|
100
|
|
|
|
227
|
return $this->get_file() if ( $this->get_file() ); |
|
243
|
118
|
50
|
|
|
|
257
|
return 'STRING_INPUT' if ( $this->get_string() ); |
|
244
|
0
|
0
|
|
|
|
0
|
return 'STREAM_INPUT' if ( $this->get_fh() ); |
|
245
|
0
|
|
|
|
|
0
|
return 'UNKNOWN'; |
|
246
|
|
|
|
|
|
|
} |
|
247
|
|
|
|
|
|
|
elsif ( $this->get_next() ) { |
|
248
|
2
|
|
|
|
|
5
|
return $this->get_next()->get_filename(); |
|
249
|
|
|
|
|
|
|
} |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
} |
|
252
|
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
sub get_next { |
|
254
|
496
|
|
|
496
|
0
|
590
|
my $this = shift; |
|
255
|
496
|
|
|
|
|
1174
|
return $this->{'NEXT'}; |
|
256
|
|
|
|
|
|
|
} |
|
257
|
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
sub is_done { |
|
259
|
1874
|
|
|
1874
|
0
|
3222
|
return $_[0]->{'DONE'}; |
|
260
|
|
|
|
|
|
|
} |
|
261
|
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
sub set_done { |
|
263
|
245
|
|
|
245
|
0
|
308
|
my $this = shift; |
|
264
|
245
|
|
|
|
|
420
|
$this->{'DONE'} = 1; |
|
265
|
|
|
|
|
|
|
} |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
1; |