| 1 | package File::Process; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use File::Process::Utils qw(:booleans :chars); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use IO::Scalar; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Scalar::Util qw( openhandle ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use parent qw( Exporter ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 5 | our $VERSION = '0.10'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 5 | our %DEFAULT_PROCESSORS = ( | |||||||||||||||||||||||||||
| 18 | pre => \&_pre, | ||||||||||||||||||||||||||||
| 19 | next_line => \&_next_line, | ||||||||||||||||||||||||||||
| 20 | filter => \&_filter, | ||||||||||||||||||||||||||||
| 21 | process => \&_process, | ||||||||||||||||||||||||||||
| 22 | post => \&_post, | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 5 | ▶ | use parent qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 5 | our @EXPORT = qw( process_file ); ## no critic (ProhibitAutomaticExportation) | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 5 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 30 | post | ||||||||||||||||||||||||||||
| 31 | pre | ||||||||||||||||||||||||||||
| 32 | process | ||||||||||||||||||||||||||||
| 33 | process_csv | ||||||||||||||||||||||||||||
| 34 | filter | ||||||||||||||||||||||||||||
| 35 | next_line | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 5 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||
| 39 | 'booleans' => [qw($TRUE $FALSE $SUCCESS $FAILURE)], | ||||||||||||||||||||||||||||
| 40 | 'all' => \@EXPORT_OK, | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 5 | ▶ | caller or __PACKAGE__->main(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | ######################################################################## | ||||||||||||||||||||||||||||
| 46 | sub process_csv { | ||||||||||||||||||||||||||||
| 47 | ######################################################################## | ||||||||||||||||||||||||||||
| 48 | 9 | ▶ | require File::Process::Utils; | ||||||||||||||||||||||||||
|
Subroutine
process_csv: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 9 | return File::Process::Utils::process_csv(@_); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | ######################################################################## | ||||||||||||||||||||||||||||
| 54 | sub process_file { | ||||||||||||||||||||||||||||
| 55 | ######################################################################## | ||||||||||||||||||||||||||||
| 56 | 18 | ▶ | my ( $file, %args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
process_file: called CC=12 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 18 | my $chomp = $args{'chomp'}; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 18 | ▶ | $args{'file'} = $file || $EMPTY; | ||||||||||||||||||||||||||
|
Condition33%1/3
$file || $File::Process::EMPTY
MC/DC0%0/2
$file || $File::Process::EMPTY
$file
$File::Process::EMPTY
Truth table33%1/3
A || B
A= $fileB= $File::Process::EMPTY
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | my %processors | ||||||||||||||||||||||||||||
| 63 | 18 | = map { ( $_, $args{$_} ) } qw( pre filter next_line process post ); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 18 | foreach (qw( pre filter next_line process post)) { | |||||||||||||||||||||||||||
| 66 | 90 | ▶ | if ( !$processors{$_} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 68 | $processors{$_} = $DEFAULT_PROCESSORS{$_}; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 18 | $args{'default_processors'} = \%DEFAULT_PROCESSORS; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 18 | my ( $fh, $all_lines ) = $processors{'pre'}->( $file, \%args ); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 18 | ▶ | if ( !$fh || !ref $all_lines ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$fh and ref $all_lines
MC/DC0%0/2
$fh and ref $all_lines
$fh
ref $all_lines
Truth table33%1/3
A and B
A= $fhB= ref $all_lines
| |||||||||||||||||||||||||||||
| 76 | 0 | croak | |||||||||||||||||||||||||||
| 77 | "invalid pre processor return values: wanted file handle, array ref\n"; | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 18 | LINE: while (1) { | |||||||||||||||||||||||||||
| 81 | 144 | my $current_line = $processors{'next_line'}->( $fh, $all_lines, \%args ); | |||||||||||||||||||||||||||
| 82 | 144 | ▶ | last LINE if !defined $current_line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 126 | $args{'raw_count'}++; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 126 | foreach my $p ( @processors{qw( filter process )} ) { | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | $current_line | ||||||||||||||||||||||||||||
| 89 | 250 | = eval { return $p->( $fh, $all_lines, \%args, $current_line ); }; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 250 | ▶ | last LINE if $EVAL_ERROR; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 250 | ▶ | next LINE if !defined $current_line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 124 | ▶ | if ( $args{merge_lines} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 4 | $all_lines->print($current_line); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | else { | ||||||||||||||||||||||||||||
| 100 | 120 | push @{$all_lines}, $current_line; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 18 | ▶ | if ($EVAL_ERROR) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | croak "$EVAL_ERROR"; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 18 | return $processors{'post'}->( $fh, $all_lines, \%args ); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | ######################################################################## | ||||||||||||||||||||||||||||
| 112 | sub _pre { | ||||||||||||||||||||||||||||
| 113 | ######################################################################## | ||||||||||||||||||||||||||||
| 114 | 18 | ▶ | my ( $file, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_pre: called CC=4 SCAR=14.7
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 18 | my $fh; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 18 | ▶ | if ( openhandle $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 18 | $fh = $file; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 18 | $args->{file} = ref $fh; # GLOB | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | else { | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | open $fh, '<', $file ## no critic (RequireBriefOpen) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | or croak 'could not open ' . $file . $NL; | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | $args->{'file'} = $file; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 18 | $args->{'raw_count'} = 0; | |||||||||||||||||||||||||||
| 131 | 18 | $args->{'skipped'} = 0; | |||||||||||||||||||||||||||
| 132 | 18 | $args->{'start_time'} = time; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 18 | ▶ | my $lines = $args->{merge_lines} ? IO::Scalar->new : []; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 18 | return ( $fh, $lines ); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | ######################################################################## | ||||||||||||||||||||||||||||
| 140 | sub _next_line { | ||||||||||||||||||||||||||||
| 141 | ######################################################################## | ||||||||||||||||||||||||||||
| 142 | 52 | ▶ | my ( $fh, $all_lines, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_next_line: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 52 | my $current_line; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 52 | ▶ | if ( openhandle $fh ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 52 | ▶ | if ( !eof $fh ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 44 | ▶ | defined( $current_line = readline $fh ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | or croak "readline failed: $OS_ERROR\n"; | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 52 | return $current_line; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | ######################################################################## | ||||||||||||||||||||||||||||
| 157 | sub _filter { | ||||||||||||||||||||||||||||
| 158 | ######################################################################## | ||||||||||||||||||||||||||||
| 159 | 126 | ▶ | my ( $fh, $all_lines, $args, $current_line ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_filter: called CC=16 SCAR=27.9
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 126 | ▶ | if ( $args->{'chomp'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 126 | chomp $current_line; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 126 | ▶ | if ( $args->{'trim'} && $args->{'trim'} =~ /(front|both)/xsm ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args->{"trim"} and $args->{"trim"} =~ /(front|both)/msx
MC/DC100%2/2
$args->{"trim"} and $args->{"trim"} =~ /(front|both)/msx
$args->{"trim"}
$args->{"trim"} =~ /(front|both)/msx
Truth table100%3/3
A and B
A= $args->{"trim"}B= $args->{"trim"} =~ /(front|both)/msx
| |||||||||||||||||||||||||||||
| 166 | 12 | $current_line =~ s/^\s+//xsm; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 126 | ▶ | if ( $args->{'trim'} && $args->{'trim'} =~ /(both|back)/xsm ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args->{"trim"} and $args->{"trim"} =~ /(both|back)/msx
MC/DC100%2/2
$args->{"trim"} and $args->{"trim"} =~ /(both|back)/msx
$args->{"trim"}
$args->{"trim"} =~ /(both|back)/msx
Truth table100%3/3
A and B
A= $args->{"trim"}B= $args->{"trim"} =~ /(both|back)/msx
| |||||||||||||||||||||||||||||
| 170 | 12 | $current_line =~ s/\s+$//xsm; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # skip? | ||||||||||||||||||||||||||||
| 174 | 126 | my $skip = $FALSE; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 126 | ▶ | if ( $args->{'skip_blank_lines'} || $args->{'skip_comments'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args->{"skip_blank_lines"} or $args->{"skip_comments"}
MC/DC100%2/2
$args->{"skip_blank_lines"} or $args->{"skip_comments"}
$args->{"skip_blank_lines"}
$args->{"skip_comments"}
Truth table100%3/3
A or B
A= $args->{"skip_blank_lines"}B= $args->{"skip_comments"}
| |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 12 | ▶ | if ( $args->{'skip_blank_lines'} && "$current_line" eq $EMPTY ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args->{"skip_blank_lines"} and "$current_line" eq $File::Process::EMPTY
MC/DC100%2/2
$args->{"skip_blank_lines"} and "$current_line" eq $File::Process::EMPTY
$args->{"skip_blank_lines"}
"$current_line" eq $File::Process::EMPTY
Truth table100%3/3
A and B
A= $args->{"skip_blank_lines"}B= "$current_line" eq $File::Process::EMPTY
| |||||||||||||||||||||||||||||
| 179 | 1 | $skip = $TRUE; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # if we're not chomping, then consider new line a blank line? | ||||||||||||||||||||||||||||
| 183 | 12 | ▶ | if ( !$args->{chomp} && "$current_line" eq $NL ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $args->{"chomp"} and "$current_line" eq $File::Process::NL
MC/DC0%0/2
not $args->{"chomp"} and "$current_line" eq $File::Process::NL
not $args->{"chomp"}
"$current_line" eq $File::Process::NL
Truth table33%1/3
not(A) and B
A= not $args->{"chomp"}B= "$current_line" eq $File::Process::NL
| |||||||||||||||||||||||||||||
| 184 | 0 | $skip = $TRUE; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 12 | ▶ | if ( $args->{'skip_comments'} && $current_line =~ /^\#/xsm ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args->{"skip_comments"} and $current_line =~ /^\#/msx
MC/DC100%2/2
$args->{"skip_comments"} and $current_line =~ /^\#/msx
$args->{"skip_comments"}
$current_line =~ /^\#/msx
Truth table100%3/3
A and B
A= $args->{"skip_comments"}B= $current_line =~ /^\#/msx
| |||||||||||||||||||||||||||||
| 188 | 1 | $skip = $TRUE; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 126 | ▶ | $args->{skipped} = $args->{skipped} + $skip ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 126 | ▶ | return $skip ? undef : $current_line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | ######################################################################## | ||||||||||||||||||||||||||||
| 198 | sub _process { | ||||||||||||||||||||||||||||
| 199 | ######################################################################## | ||||||||||||||||||||||||||||
| 200 | 115 | ▶ | my ( $fh, $all_lines, $args, $current_line ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_process: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 115 | return $current_line; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | ######################################################################## | ||||||||||||||||||||||||||||
| 206 | sub _post { | ||||||||||||||||||||||||||||
| 207 | ######################################################################## | ||||||||||||||||||||||||||||
| 208 | 17 | ▶ | my ( $fh, $all_lines, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_post: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 17 | $args->{end_time} = time; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 17 | my $retval; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 17 | ▶ | if ( $args->{merge_lines} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | 1 | $retval = ${ $all_lines->sref }; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | else { | ||||||||||||||||||||||||||||
| 218 | 16 | $retval = $all_lines; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 17 | ▶ | if ( !$args->{'keep_open'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | close $fh | ||||||||||||||||||||||||||||
| 223 | 2 | ▶ | or croak 'could not close' . $args->{file} . $NL; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 17 | return ( $retval, %{$args} ); | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | ######################################################################## | ||||||||||||||||||||||||||||
| 230 | sub post { ## no critic [Subroutines::RequireArgUnpacking] | ||||||||||||||||||||||||||||
| 231 | ######################################################################## | ||||||||||||||||||||||||||||
| 232 | 0 | ▶ | return $_[2]->{default_processors}->{post}->(@_); | ||||||||||||||||||||||||||
|
Subroutine
post: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | ######################################################################## | ||||||||||||||||||||||||||||
| 236 | sub filter { ## no critic [Subroutines::RequireArgUnpacking] | ||||||||||||||||||||||||||||
| 237 | ######################################################################## | ||||||||||||||||||||||||||||
| 238 | 0 | ▶ | return $_[2]->{default_processors}->{filter}->(@_); | ||||||||||||||||||||||||||
|
Subroutine
filter: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | ######################################################################## | ||||||||||||||||||||||||||||
| 242 | sub pre { ## no critic [Subroutines::RequireArgUnpacking] | ||||||||||||||||||||||||||||
| 243 | ######################################################################## | ||||||||||||||||||||||||||||
| 244 | 10 | ▶ | return $_[1]->{default_processors}->{pre}->(@_); | ||||||||||||||||||||||||||
|
Subroutine
pre: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | ######################################################################## | ||||||||||||||||||||||||||||
| 248 | sub process { ## no critic [Subroutines::RequireArgUnpacking] | ||||||||||||||||||||||||||||
| 249 | ######################################################################## | ||||||||||||||||||||||||||||
| 250 | 0 | ▶ | return $_[2]->{default_processors}->{process}->(@_); | ||||||||||||||||||||||||||
|
Subroutine
process: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | ######################################################################## | ||||||||||||||||||||||||||||
| 254 | sub next_line { ## no critic [Subroutines::RequireArgUnpacking] | ||||||||||||||||||||||||||||
| 255 | ######################################################################## | ||||||||||||||||||||||||||||
| 256 | 0 | ▶ | return $_[2]->{default_processors}->{next_line}->(@_); | ||||||||||||||||||||||||||
|
Subroutine
next_line: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | ######################################################################## | ||||||||||||||||||||||||||||
| 260 | sub main { | ||||||||||||||||||||||||||||
| 261 | ######################################################################## | ||||||||||||||||||||||||||||
| 262 | 0 | ▶ | require IO::Scalar; | ||||||||||||||||||||||||||
|
Subroutine
main: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 263 | 0 | require Data::Dumper; | |||||||||||||||||||||||||||
| 264 | 0 | require JSON::PP; | |||||||||||||||||||||||||||
| 265 | 0 | require Text::CSV_XS; | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 0 | JSON::PP->import('decode_json'); | |||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 0 | Data::Dumper->import('Dumper'); | |||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | # +------------------+ | ||||||||||||||||||||||||||||
| 272 | # | READ A TEXT FILE | | ||||||||||||||||||||||||||||
| 273 | # +------------------+ | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 0 | my $buffer = <<'END_OF_TEXT'; | |||||||||||||||||||||||||||
| 276 | line 1 | ||||||||||||||||||||||||||||
| 277 | line 2 | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | line 4 | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | line 5 | ||||||||||||||||||||||||||||
| 282 | END_OF_TEXT | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 0 | my $fh = IO::Scalar->new( \$buffer ); | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 0 | print Dumper( | |||||||||||||||||||||||||||
| 287 | process_file( | ||||||||||||||||||||||||||||
| 288 | $fh, | ||||||||||||||||||||||||||||
| 289 | skip_blank_lines => $TRUE, | ||||||||||||||||||||||||||||
| 290 | chomp => $TRUE, | ||||||||||||||||||||||||||||
| 291 | trim => 'both' | ||||||||||||||||||||||||||||
| 292 | ) | ||||||||||||||||||||||||||||
| 293 | ); | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 0 | $fh = IO::Scalar->new( \$buffer ); | |||||||||||||||||||||||||||
| 296 | print Dumper( | ||||||||||||||||||||||||||||
| 297 | process_file( | ||||||||||||||||||||||||||||
| 298 | $fh, | ||||||||||||||||||||||||||||
| 299 | post => sub { | ||||||||||||||||||||||||||||
| 300 | 0 | ▶ | my @retval = post(@_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 301 | 0 | $retval[0] = join $EMPTY, @{ $_[1] }; | |||||||||||||||||||||||||||
| 302 | 0 | return @retval; | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | ) | ||||||||||||||||||||||||||||
| 305 | 0 | ); | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | # +------------------+ | ||||||||||||||||||||||||||||
| 308 | # | READ A JSON FILE | | ||||||||||||||||||||||||||||
| 309 | # +------------------+ | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 0 | my $json_text = <<'END_OF_TEXT'; | |||||||||||||||||||||||||||
| 312 | { | ||||||||||||||||||||||||||||
| 313 | "foo" : "bar", | ||||||||||||||||||||||||||||
| 314 | "baz" : "buz" | ||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | END_OF_TEXT | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 0 | $fh = IO::Scalar->new( \$json_text ); | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | print Dumper( | ||||||||||||||||||||||||||||
| 322 | process_file( | ||||||||||||||||||||||||||||
| 323 | $fh, | ||||||||||||||||||||||||||||
| 324 | chomp => 1, | ||||||||||||||||||||||||||||
| 325 | post => sub { | ||||||||||||||||||||||||||||
| 326 | 0 | ▶ | post(@_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 327 | 0 | return decode_json( join $EMPTY, @{ $_[1] } ); | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | ) | ||||||||||||||||||||||||||||
| 330 | 0 | ); | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 0 | $fh = IO::Scalar->new( \$json_text ); | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 0 | print Dumper( | |||||||||||||||||||||||||||
| 335 | decode_json( process_file( $fh, merge_lines => 1, chomp => 1 ) ) ); | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | # +-----------------+ | ||||||||||||||||||||||||||||
| 338 | # | READ A CSV FILE | | ||||||||||||||||||||||||||||
| 339 | # +-----------------+ | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 0 | my $csv_text = <<'END_OF_TEXT'; | |||||||||||||||||||||||||||
| 342 | "id","first_name","last_name" | ||||||||||||||||||||||||||||
| 343 | 0,"Rob","Lauer" | ||||||||||||||||||||||||||||
| 344 | END_OF_TEXT | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 0 | $fh = IO::Scalar->new( \$csv_text ); | |||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 0 | my $csv = Text::CSV_XS->new; | |||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | my ($csv_lines) = process_file( | ||||||||||||||||||||||||||||
| 351 | $fh, | ||||||||||||||||||||||||||||
| 352 | csv => $csv, | ||||||||||||||||||||||||||||
| 353 | chomp => 1, | ||||||||||||||||||||||||||||
| 354 | has_headers => 1, | ||||||||||||||||||||||||||||
| 355 | pre => sub { | ||||||||||||||||||||||||||||
| 356 | 0 | ▶ | my ( $csv_fh, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 0 | ▶ | if ( $args->{'has_headers'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 359 | 0 | my @column_names = $args->{csv}->getline($csv_fh); | |||||||||||||||||||||||||||
| 360 | 0 | $args->{csv}->column_names(@column_names); | |||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 0 | return ( pre( $fh, $args ) ); | |||||||||||||||||||||||||||
| 364 | }, | ||||||||||||||||||||||||||||
| 365 | next_line => sub { | ||||||||||||||||||||||||||||
| 366 | 0 | ▶ | my ( $csv_fh, $all_lines, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 367 | 0 | my $ref = $args->{csv}->getline_hr($csv_fh); | |||||||||||||||||||||||||||
| 368 | 0 | return $ref; | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | 0 | ); | |||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 0 | print Dumper($csv_lines); | |||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | 0 | exit 0; | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 5 | 1; | |||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | __END__ | ||||||||||||||||||||||||||||