blib/lib/App/RecordStream/InputStream.pm

filter: Statementstmt 90.9% 90 / 99 Branchbran 68.4% 26 / 38 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 18 / 18 Podpod 33.3% 4 / 12 totaltotal 78.3% 141 / 180 CC 26 SCAR 15.0
CC
26
Cov
82%
_init
8.4
get_filename
6.9
call_next_record
4.6
CRAP 4.5
« blib/lib/App/RecordStream/Operation/help.pm ↑ Summary blib/lib/App/RecordStream/Operation/fromcsv.pm »
1 package App::RecordStream::InputStream;
2  
3 =head1 NAME
4  
5 App::RecordStream::InputStream
6  
7 =head1 AUTHOR
8  
9 Benjamin Bernard <perlhacker@benjaminbernard.com>
10 Keith Amling <keith.amling@gmail.com>
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<App::RecordStream::Record> 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 48 our $VERSION = "4.0.25";
76  
77 48 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 48 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 48 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 48 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 48 ▶use App::RecordStream::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 48 require App::RecordStream::Operation;
85  
86 48 my $json = JSON->new;
87  
88 48 my $ONE_OF = [qw(FH STRING FILE)];
89  
90 48 my $ARGUMENTS = {
91 FH => 0,
92 STRING => 0,
93 FILE => 0,
94 NEXT => 0,
95 };
96  
97 1 ▶sub new_magic {
Subroutine new_magic: called CC=3 SCAR=12.0
Pod documented
98 1 my $class = shift;
99 1 ▶ my $files = shift || \@ARGV;
Condition50%1/2
shift() || \@ARGV
l!l
01
MC/DC0%0/1
shift() || \@ARGV
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
100  
101 1 ▶ if ( scalar @$files > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$files > 0) 1 0 1
102 1 return $class->new_from_files($files);
103 }
104  
105 0 return $class->new(FH => \*STDIN);
106 }
107  
108 2 ▶sub new_from_files {
Subroutine new_from_files: called CC=4 SCAR=14.8
Pod documented
109 2 my $class = shift;
110 2 my $files = shift;
111  
112 2 my $last_stream;
113  
114 2 foreach my $file ( reverse @$files ) {
115 4 ▶ unless ( -e $file && -r $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file and -r $file) 0 4 4
Condition33%1/3
-e $file and -r $file
!ll&&!rl&&r
004
MC/DC0%0/2
-e $file and -r $file
-e $file -r $file
Truth table33%1/3
A and B
A= -e $fileB= -r $file
ABresult
0X0
100
111
116 0 die "File does not exist or is not readable: $file\n";
117 }
118  
119 4 my $new_stream = $class->new(FILE => $file, NEXT => $last_stream);
120 4 $last_stream = $new_stream;
121 }
122  
123 2 return $last_stream;
124 }
125  
126 255 ▶sub new {
Subroutine new: called CC=4 SCAR=14.6
Pod documented
127 255 my $class = shift;
128 255 my %args = @_;
129  
130 255 my $this = {};
131  
132 255 foreach my $key (keys %$ARGUMENTS) {
133 1020 my $value = $args{$key};
134 1020 $this->{$key} = $value;
135  
136 1020 ▶ if ( $ARGUMENTS->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGUMENTS->{$key}) 0 1020 1020
137 0 ▶ die "Did not supply required argument: $key" unless ( $value );
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
138 }
139 }
140  
141 255 bless $this, $class;
142  
143 255 $this->_init();
144 255 return $this;
145 }
146  
147 255 ▶sub _init {
Subroutine _init: called CC=8 SCAR=21.3
148 255 my $this = shift;
149  
150 255 my $found = {};
151  
152 255 foreach my $arg (@$ONE_OF) {
153 765 ▶ if ( $this->{$arg} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{$arg}) 255 510 765
154 255 $found->{$arg} = $this->{$arg};
155 }
156 }
157  
158 255 ▶ if ( scalar keys %$found > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$found > 1) 0 255 255
159 0 die "Must specify only one of " . join(' ', keys %$found);
160 }
161  
162 255 ▶ unless ( scalar keys %$found == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %$found == 1) 0 255 255
163 0 die "Must specify one of " . join(' ', @$ONE_OF);
164 }
165  
166 255 ▶ if ( $this->get_string() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->get_string) 238 17 255
167 238 $this->{'FH'} = IO::String->new($this->get_string());
168 }
169  
170 255 my $file = $this->get_file();
171 255 ▶ if ( $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 10 245 255
172 10 ▶ open(my $fh, '<', $file) or die "Cannot open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Cannot open ${file}: $!" 10 0 10
173 10 $this->{'FH'} = $fh;
174 }
175 }
176  
177 377 ▶sub get_file {
Subroutine get_file: called CC=1 SCAR=0.0
Pod undocumented
178 377 my $this = shift;
179 377 return $this->{'FILE'};
180 }
181  
182 611 ▶sub get_string {
Subroutine get_string: called CC=1 SCAR=0.0
Pod undocumented
183 611 my $this = shift;
184 611 return $this->{'STRING'};
185 }
186  
187 # Performance! :(
188 1498 ▶sub get_fh {
Subroutine get_fh: called CC=1 SCAR=0.0
Pod undocumented
189 1498 return $_[0]->{'FH'};
190 }
191  
192 1502 ▶sub get_record {
Subroutine get_record: called CC=3 SCAR=11.0
Pod documented
193 1502 my $this = shift;
194  
195 1502 ▶ if ( $this->is_done() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->is_done) 4 1498 1502
196 4 return $this->call_next_record();
197 }
198  
199 1498 my $fh = $this->get_fh();
200  
201 1498 my $line = <$fh>;
202  
203 1498 ▶ if ( ! $line ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line) 245 1253 1498
204 245 close $fh;
205 245 $this->set_done();
206  
207 # This is ugly, reaching into the other class
208 245 App::RecordStream::Operation::set_current_filename($this->get_filename());
209  
210 245 return $this->call_next_record();
211 }
212  
213 # Direct bless done in the name of performance
214 1253 my $record = $json->decode($line);
215 1253 bless $record, 'App::RecordStream::Record';
216  
217 1253 return $record;
218 }
219  
220 249 ▶sub call_next_record {
Subroutine call_next_record: called CC=4 SCAR=15.2
Pod undocumented
221 249 my $this = shift;
222  
223 249 my $next = $this->get_next();
224  
225 249 ▶ unless ( $next ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($next) 243 6 249
226 243 return undef;
227 }
228  
229 # Prevent a deep recursion with many passed files
230 6 ▶ if ( $next && $next->is_done() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next and $next->is_done) 0 6 6
Condition33%1/3
$next and $next->is_done
!ll&&!rl&&r
060
MC/DC0%0/2
$next and $next->is_done
$next $next->is_done
Truth table33%1/3
A and B
A= $nextB= $next->is_done
ABresult
0X0
100
111
231 0 $next = $next->get_next();
232 0 $this->{'NEXT'} = $next;
233 }
234  
235 6 return $next->get_record();
236 }
237  
238 365 ▶sub get_filename {
Subroutine get_filename: called CC=6 SCAR=19.3
Pod undocumented
239 365 my $this = shift;
240  
241 365 ▶ if ( ! $this->is_done() ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $this->is_done) { } 120 245 365
elsif ($this->get_next) { } 2 243 245
242 120 ▶ return $this->get_file() if ( $this->get_file() );
Branch: 1 branch
BranchTrueFalseTotal
if $this->get_file 2 118 120
243 118 ▶ return 'STRING_INPUT' if ( $this->get_string() );
Branch: 1 branch
BranchTrueFalseTotal
if $this->get_string 118 0 118
244 0 ▶ return 'STREAM_INPUT' if ( $this->get_fh() );
Branch: 1 branch
BranchTrueFalseTotal
if $this->get_fh 0 0 0
245 0 return 'UNKNOWN';
246 }
247 elsif ( $this->get_next() ) {
248 2 return $this->get_next()->get_filename();
249 }
250  
251 }
252  
253 496 ▶sub get_next {
Subroutine get_next: called CC=1 SCAR=0.0
Pod undocumented
254 496 my $this = shift;
255 496 return $this->{'NEXT'};
256 }
257  
258 1874 ▶sub is_done {
Subroutine is_done: called CC=1 SCAR=0.0
Pod undocumented
259 1874 return $_[0]->{'DONE'};
260 }
261  
262 245 ▶sub set_done {
Subroutine set_done: called CC=1 SCAR=0.0
Pod undocumented
263 245 my $this = shift;
264 245 $this->{'DONE'} = 1;
265 }
266  
267 48 1;
« blib/lib/App/RecordStream/Operation/help.pm ↑ Summary blib/lib/App/RecordStream/Operation/fromcsv.pm »