« blib/lib/App/RecordStream/Operation/toprettyprint.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 | 47 | our $VERSION = "4.0.23"; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 47 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | 47 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 47 | ▶ | use IO::String; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | 47 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 47 | ▶ | use App::RecordStream::Record; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | 47 | require App::RecordStream::Operation; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 47 | my $json = JSON->new; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 47 | my $ONE_OF = [qw(FH STRING FILE)]; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 47 | my $ARGUMENTS = { | |||||||||||||||||||||||||||
| 91 | FH => 0, | ||||||||||||||||||||||||||||
| 92 | STRING => 0, | ||||||||||||||||||||||||||||
| 93 | FILE => 0, | ||||||||||||||||||||||||||||
| 94 | NEXT => 0, | ||||||||||||||||||||||||||||
| 95 | }; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub new_magic { | ||||||||||||||||||||||||||||
| 98 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new_magic: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 1 | ▶ | my $files = shift || \@ARGV; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || \@ARGV
MC/DC0%0/1
shift() || \@ARGV
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | ▶ | if ( scalar @$files > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 1 | return $class->new_from_files($files); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | return $class->new(FH => \*STDIN); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub new_from_files { | ||||||||||||||||||||||||||||
| 109 | 2 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new_from_files: called CC=4 SCAR=14.8
Pod
documented
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
-e $file and -r $file
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
| |||||||||||||||||||||||||||||
| 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 | sub new { | ||||||||||||||||||||||||||||
| 127 | 251 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 251 | my %args = @_; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 251 | my $this = {}; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 251 | foreach my $key (keys %$ARGUMENTS) { | |||||||||||||||||||||||||||
| 133 | 1004 | my $value = $args{$key}; | |||||||||||||||||||||||||||
| 134 | 1004 | $this->{$key} = $value; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1004 | ▶ | if ( $ARGUMENTS->{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | die "Did not supply required argument: $key" unless ( $value ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 251 | bless $this, $class; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 251 | $this->_init(); | |||||||||||||||||||||||||||
| 144 | 251 | return $this; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | sub _init { | ||||||||||||||||||||||||||||
| 148 | 251 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=8 SCAR=21.3
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 251 | my $found = {}; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 251 | foreach my $arg (@$ONE_OF) { | |||||||||||||||||||||||||||
| 153 | 753 | ▶ | if ( $this->{$arg} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 251 | $found->{$arg} = $this->{$arg}; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 251 | ▶ | if ( scalar keys %$found > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | die "Must specify only one of " . join(' ', keys %$found); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 251 | ▶ | unless ( scalar keys %$found == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | die "Must specify one of " . join(' ', @$ONE_OF); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 251 | ▶ | if ( $this->get_string() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 234 | $this->{'FH'} = IO::String->new($this->get_string()); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 251 | my $file = $this->get_file(); | |||||||||||||||||||||||||||
| 171 | 251 | ▶ | if ( $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 10 | ▶ | open(my $fh, '<', $file) or die "Cannot open $file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 10 | $this->{'FH'} = $fh; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | sub get_file { | ||||||||||||||||||||||||||||
| 178 | 371 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 179 | 371 | return $this->{'FILE'}; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | sub get_string { | ||||||||||||||||||||||||||||
| 183 | 601 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_string: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 184 | 601 | return $this->{'STRING'}; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | # Performance! :( | ||||||||||||||||||||||||||||
| 188 | sub get_fh { | ||||||||||||||||||||||||||||
| 189 | 1478 | ▶ | return $_[0]->{'FH'}; | ||||||||||||||||||||||||||
|
Subroutine
get_fh: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | sub get_record { | ||||||||||||||||||||||||||||
| 193 | 1482 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_record: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 1482 | ▶ | if ( $this->is_done() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 4 | return $this->call_next_record(); | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 1478 | my $fh = $this->get_fh(); | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 1478 | my $line = <$fh>; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 1478 | ▶ | if ( ! $line ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 239 | close $fh; | |||||||||||||||||||||||||||
| 205 | 239 | $this->set_done(); | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # This is ugly, reaching into the other class | ||||||||||||||||||||||||||||
| 208 | 239 | App::RecordStream::Operation::set_current_filename($this->get_filename()); | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 239 | return $this->call_next_record(); | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # Direct bless done in the name of performance | ||||||||||||||||||||||||||||
| 214 | 1239 | my $record = $json->decode($line); | |||||||||||||||||||||||||||
| 215 | 1238 | bless $record, 'App::RecordStream::Record'; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 1238 | return $record; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | sub call_next_record { | ||||||||||||||||||||||||||||
| 221 | 243 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
call_next_record: called CC=4 SCAR=15.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 243 | my $next = $this->get_next(); | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 243 | ▶ | unless ( $next ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 237 | return undef; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # Prevent a deep recursion with many passed files | ||||||||||||||||||||||||||||
| 230 | 6 | ▶ | if ( $next && $next->is_done() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$next and $next->is_done
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
| |||||||||||||||||||||||||||||
| 231 | 0 | $next = $next->get_next(); | |||||||||||||||||||||||||||
| 232 | 0 | $this->{'NEXT'} = $next; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 6 | return $next->get_record(); | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | sub get_filename { | ||||||||||||||||||||||||||||
| 239 | 357 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_filename: called CC=6 SCAR=19.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 357 | ▶ | if ( ! $this->is_done() ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 242 | 118 | ▶ | return $this->get_file() if ( $this->get_file() ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | 116 | ▶ | return 'STRING_INPUT' if ( $this->get_string() ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | 0 | ▶ | return 'STREAM_INPUT' if ( $this->get_fh() ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 0 | return 'UNKNOWN'; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | elsif ( $this->get_next() ) { | ||||||||||||||||||||||||||||
| 248 | 2 | return $this->get_next()->get_filename(); | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | sub get_next { | ||||||||||||||||||||||||||||
| 254 | 484 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_next: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 255 | 484 | return $this->{'NEXT'}; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | sub is_done { | ||||||||||||||||||||||||||||
| 259 | 1846 | ▶ | return $_[0]->{'DONE'}; | ||||||||||||||||||||||||||
|
Subroutine
is_done: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | sub set_done { | ||||||||||||||||||||||||||||
| 263 | 239 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_done: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 264 | 239 | $this->{'DONE'} = 1; | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 47 | 1; | |||||||||||||||||||||||||||