| 1 | 4 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | package Ekorn::Pipeline::CSV; | ||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use experimental qw(builtin try); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use builtin qw(blessed); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Encode qw(); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use PerlIO::encoding qw(); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Ref::Util qw( | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | is_plain_arrayref is_plain_coderef is_plain_hashref | ||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | is_plain_ref); | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Scalar::Util qw(openhandle); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Ekorn::Pipeline::Errors qw(croakf); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use Ekorn::Pipeline::Factories qw(gather sink_one source stage); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use namespace::clean -except => 'import'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | our @EXPORT_OK = qw(from_csv to_csv); | |||||||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # Croak at the line that called Ekorn::Pipeline. | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 4 | our @CARP_NOT = @Ekorn::Pipeline::Errors::INTERNAL; | |||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # Text::CSV's code for the end of its input, which isn't an error. | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 4 | my $EOF = 2012; | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # What a file's encoding does with text it can't decode or encode: | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # croak, rather than warn and carry on. | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 4 | my $STRICT = Encode::FB_CROAK() | Encode::STOP_AT_PARTIAL(); | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # Each way of munging a header's names that's named, as Text::CSV's | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # munge_column_names has them. | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | my %MUNGE = ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 5 | ▶ | lc => sub ($name) { lc $name }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 4 | ▶ | uc => sub ($name) { uc $name }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 6 | ▶ | db => sub ($name) { lc($name =~ s( \W+ )(_)xgr =~ s( \A _+ )()xr) }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 4 | none => undef, | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # A source of the rows read from the file or filehandle. Given neither, | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # a stage parsing each value, a text of any number of rows, into its | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # rows. | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 85 | ▶ | sub from_csv (@options) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_csv: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 85 | my %option = _options( | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | 'from_csv', [qw(file fh csv header munge encoding)], @options, | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 65 | ▶ | return _reader(\%option) if _has_input(\%option); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 14 | my($csv, $header, $munge) = ($option{new}->(), @option{qw(header munge)}); | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | 15 | ▶ | return stage { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 15 | my($next) = _table( | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | $csv, Ekorn::Pipeline::CSV::Text->new($_), 'a value', | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | $header, $munge, | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 15 | my @rows; | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 15 | ▶ | while (my $row = $next->()) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 16 | push @rows, $row; | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 14 | return @rows; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | 14 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # A sink writing each row to the file or filehandle, giving how many. | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # Given neither, a stage passing on each row as its line, without its | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # end of line, after the header's, if it has one. | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 54 | ▶ | sub to_csv (@options) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_csv: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 54 | my %option = _options('to_csv', [qw(file fh csv header encoding)], @options); | |||||||||||||||||||||||||||||||||||||||||||||
| 69 | 36 | ▶ | return _writer(\%option) if _has_input(\%option); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 16 | my($csv, $header) = ($option{new}->(), $option{header}); | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | 16 | ▶ | unless ($header) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 7 | my($line) = _formatter($csv, $header, 'chars'); | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | 7 | ▶ | return stage { $line->($_) }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 9 | ▶ | return gather { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 9 | my($line, $head) = _formatter($csv, $header, 'chars'); # this run's names | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 9 | my $first = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # For each row, without a signature: the header's line first. | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 8 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 8 | ▶ | return $line->($_[0]) unless $first; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 6 | $first = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 6 | return ($head->(), $line->($_[0])); | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 8 | ▶ | sub () { return $first ? $head->() : () }, # no rows: the header | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 9 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | 9 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # Return the options given, checked, with "new", a sub returning the | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # parser, and "encoding", a file's, as _encoding gives it. Load | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # Text::CSV first. | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 139 | ▶ | sub _options ($function, $known, @given) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_options: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 139 | _load($function); | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 135 | ▶ | croakf 'Expected options as name => value pairs' if @given % 2; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 133 | my %option = @given; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | 133 | my %allowed = map { $_ => 1 } @$known; | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | 133 | my @unknown = sort grep { !$allowed{$_} } keys %option; | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | 133 | ▶ | croakf 'Expected the options %s, not %s', | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | join(', ', @$known[0 .. $#$known - 1]) . " and $known->[-1]", join ', ', | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | @unknown | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | if @unknown; | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | croakf 'Expected file to be a path' | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 131 | ▶ | if exists $option{file} && !defined $option{file}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $option{"file"} and not defined $option{"file"}
MC/DC100%2/2
exists $option{"file"} and not defined $option{"file"}
exists $option{"file"}
not defined $option{"file"}
Truth table100%3/3
A and not(B)
A= exists $option{"file"}B= not defined $option{"file"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | croakf 'Expected a file or a filehandle, not both' | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 129 | ▶ | if defined $option{file} && exists $option{fh}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $option{"file"} and exists $option{"fh"}
MC/DC100%2/2
defined $option{"file"} and exists $option{"fh"}
defined $option{"file"}
exists $option{"fh"}
Truth table100%3/3
A and B
A= defined $option{"file"}B= exists $option{"fh"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | croakf 'Expected an open filehandle' | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 127 | ▶ | if exists $option{fh} && !defined openhandle($option{fh}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $option{"fh"} and not defined &openhandle($option{"fh"})
MC/DC100%2/2
exists $option{"fh"} and not defined &openhandle($option{"fh"})
exists $option{"fh"}
not defined &openhandle($option{"fh"})
Truth table100%3/3
A and not(B)
A= exists $option{"fh"}B= not defined &openhandle($option{"fh"})
| |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 125 | $option{encoding} = _encoding(\%option); | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | 115 | _check_header(\%option, $function); | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | 109 | ▶ | $option{munge} = _munger(\%option) if exists $option{munge}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 105 | ▶ | my $raw = defined $option{encoding} && !ref $option{encoding}; # the only one not an object | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
defined $option{'encoding'} && !ref($option{'encoding'})
MC/DC100%2/2
defined $option{'encoding'} && !ref($option{'encoding'})
defined $option{'encoding'}
!ref($option{'encoding'})
Truth table100%3/3
A && not(B)
A= defined $option{'encoding'}B= !ref($option{'encoding'})
| |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 105 | $option{new} = _parser($option{csv}, $raw); | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | 101 | return %option; | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | # Load Text::CSV, croaking for the function given if it can't be. | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 139 | ▶ | sub _load ($function) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 139 | ▶ | return if eval { require Text::CSV; 1 }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 4 | my $error = $@; | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | 4 | ▶ | return croakf q(Can't find Text::CSV, which %s needs; install it from CPAN), | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | $function | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | if $error =~ m( \A Can't\ locate\ Text/CSV\.pm\ in\ \@INC )x; | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | return croakf q(Can't load Text::CSV, which %s needs: %s), $function, | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | $error =~ s( \s* \n .* )()xsr; | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | # Return a file's encoding: 'raw', for bytes, or Encode's object for the | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | # one named, UTF-8 if none is. Only a file is opened, so only a file | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | # takes one. | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 125 | ▶ | sub _encoding ($option) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encoding: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 125 | ▶ | return unless defined $option->{file} || exists $option->{encoding}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $option->{"file"} or exists $option->{"encoding"}
MC/DC100%2/2
defined $option->{"file"} or exists $option->{"encoding"}
defined $option->{"file"}
exists $option->{"encoding"}
Truth table100%3/3
A or B
A= defined $option->{"file"}B= exists $option->{"encoding"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 77 | ▶ | my $encoding = exists $option->{encoding} ? $option->{encoding} : 'UTF-8'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 77 | ▶ | croakf 'Expected encoding only with a file' unless defined $option->{file}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 73 | ▶ | return 'raw' if defined $encoding && $encoding eq 'raw'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $encoding and $encoding eq "raw"
MC/DC100%2/2
defined $encoding and $encoding eq "raw"
defined $encoding
$encoding eq "raw"
Truth table100%3/3
A and B
A= defined $encodingB= $encoding eq "raw"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 70 | ▶ | my $found = !ref $encoding && Encode::find_encoding($encoding // q()); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%5/5
!ref($encoding) && &Encode::find_encoding(scalar($encoding // ''))
$encoding // ""
MC/DC100%2/2
!ref($encoding) && &Encode::find_encoding(scalar($encoding // ''))
!ref($encoding)
&Encode::find_encoding(scalar($encoding // ''))
MC/DC100%1/1
$encoding // ""
$encoding
Truth table100%3/3
not(A) && B
A= !ref($encoding)B= &Encode::find_encoding(scalar($encoding // ''))
Truth table100%2/2
A
A= $encoding
| |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 70 | ▶ | croakf 'Expected an encoding Perl knows, not %s', $encoding // 'undef' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$encoding // "undef"
MC/DC100%1/1
$encoding // "undef"
$encoding
Truth table100%2/2
A
A= $encoding
| |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | unless $found; | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 64 | return $found; | |||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | # Croak unless the header is one the function takes: true, false, or | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | # 'skip' to read; true, false, or an array of names to write. | ||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 115 | ▶ | sub _check_header ($option, $function) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_header: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 115 | my $header = $option->{header}; | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | 115 | ▶ | return if !defined $header || (!ref $header && $header =~ m( \A [01]? \z )x); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $header or not ref $header and $header =~ / \A [01]? \z /ux
not ref $header and $header =~ / \A [01]? \z /ux
MC/DC100%3/3
not defined $header or not ref $header and $header =~ / \A [01]? \z /ux
not defined $header
not ref $header
$header =~ / \A [01]? \z /ux
Truth table100%4/4
not(A) or not(B) and C
A= not defined $headerB= not ref $headerC= $header =~ / \A [01]? \z /ux
| |||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 21 | ▶ | if ($function eq 'from_csv') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 8 | ▶ | croakf q(Expected header to be true, false or 'skip') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $header or $header ne "skip"
MC/DC100%2/2
ref $header or $header ne "skip"
ref $header
$header ne "skip"
Truth table100%3/3
A or B
A= ref $headerB= $header ne "skip"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | if ref $header || $header ne 'skip'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 6 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | croakf q(Expected header to be true, false or an array reference of names) | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 13 | ▶ | if !is_plain_arrayref($header) || grep { !defined || ref } @$header; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not Ref::Util::XS::is_plain_arrayref($header) or grep {not defined $_ or ref $_;} @$header
!defined($_) || ref $_
MC/DC100%2/2
not Ref::Util::XS::is_plain_arrayref($header) or grep {not defined $_ or ref $_;} @$header
not Ref::Util::XS::is_plain_arrayref($header)
grep {not defined $_ or ref $_;} @$header
MC/DC100%2/2
!defined($_) || ref $_
!defined($_)
ref $_
Truth table100%3/3
not(A) or B
A= not Ref::Util::XS::is_plain_arrayref($header)B= grep {not defined $_ or ref $_;} @$header
Truth table100%3/3
not(A) || B
A= !defined($_)B= ref $_
| |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 9 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | # Return a sub munging a header's name as munge says, or nothing, for a | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # munge of none. | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 13 | ▶ | sub _munger ($option) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_munger: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 13 | my $munge = $option->{munge}; | |||||||||||||||||||||||||||||||||||||||||||||
| 166 | 13 | ▶ | croakf 'Expected munge only with a header' unless $option->{header}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 12 | ▶ | if (is_plain_coderef($munge)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 1 | ▶ | return sub ($name) { local $_ = $name; $munge->($name) }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 11 | ▶ | if (is_plain_hashref($munge)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 1 | ▶ | return sub ($name) { exists $munge->{$name} ? $munge->{$name} : $name }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 173 | croakf | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 'Expected munge to be lc, uc, db, none, a hash reference or a code reference' | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 10 | ▶ | if ref $munge || !exists $MUNGE{$munge // q()}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
ref $munge or not exists $MUNGE{$munge // ""}
$munge // ""
MC/DC100%2/2
ref $munge or not exists $MUNGE{$munge // ""}
ref $munge
not exists $MUNGE{$munge // ""}
MC/DC100%1/1
$munge // ""
$munge
Truth table100%3/3
A or not(B)
A= ref $mungeB= not exists $MUNGE{$munge // ""}
Truth table100%2/2
A
A= $munge
| |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 7 | return $MUNGE{$munge}; | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | # Return a sub returning the parser: one made from csv's options, over | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | # the defaults, or csv's object, as it is. Reading a file raw, the | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | # parser leaves bytes that are UTF-8 as bytes, rather than decode them. | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 105 | ▶ | sub _parser ($csv, $raw) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parser: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 105 | ▶ | if (blessed($csv)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 25 | ▶ | return sub () { $csv }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 80 | ▶ | croakf 'Expected csv to be a hash reference of options, or a parser object' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $csv and not Ref::Util::XS::is_plain_hashref($csv)
MC/DC100%2/2
defined $csv and not Ref::Util::XS::is_plain_hashref($csv)
defined $csv
not Ref::Util::XS::is_plain_hashref($csv)
Truth table100%3/3
A and not(B)
A= defined $csvB= not Ref::Util::XS::is_plain_hashref($csv)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | if defined $csv && !is_plain_hashref($csv); | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | my %settings = ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | binary => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 191 | auto_diag => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | ($raw ? (decode_utf8 => 0) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 78 | ▶ | %{ $csv // {} }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$csv // {}
MC/DC100%1/1
$csv // {}
$csv
Truth table100%2/2
A
A= $csv
| |||||||||||||||||||||||||||||||||||||||||||||||
| 194 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 78 | ▶ | croakf 'Expected csv options the parser knows: %s', | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 196 | scalar Text::CSV->error_diag | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | unless Text::CSV->new({%settings}); | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 76 | ▶ | return sub () { Text::CSV->new({%settings}) }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | # Return true if the options name a file or a filehandle. | ||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 101 | ▶ | sub _has_input ($option) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_has_input: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 101 | ▶ | return defined $option->{file} || !!$option->{fh}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
defined $option->{'file'} || !!$option->{'fh'}
MC/DC100%2/2
defined $option->{'file'} || !!$option->{'fh'}
defined $option->{'file'}
!!$option->{'fh'}
Truth table100%3/3
A || B
A= defined $option->{'file'}B= !!$option->{'fh'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||
| 208 | # Where Perl says an error was raised: its file and line, then the last | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | # filehandle read and how far, if one has been. | ||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 4 | my $PLACE = qr( ,? \s+ at \s+ \S+ \s+ line \s+ \d+ )x; | |||||||||||||||||||||||||||||||||||||||||||||
| 211 | 4 | my $READ = qr( , \s+ < \S* > \s+ (?: line | chunk ) \s+ \d+ )x; | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # Return an error's message without the place it was raised, as a | ||||||||||||||||||||||||||||||||||||||||||||||
| 214 | # message of ours says where. | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 2 | ▶ | sub _unplaced ($error) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unplaced: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 2 | return "$error" =~ s( $PLACE $READ? \.? \s* \z )()xr; | |||||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||
| 220 | # Return a sub giving the next row read from $io, by $from, or nothing | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | # at its end; and the context, if a header was read, as the header | ||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # says: rows as arrays, or as hashes keyed by the header's names, | ||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # munged. | ||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 67 | ▶ | sub _table ($csv, $io, $from, $header, $munge) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_table: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 67 | my $next = _rows($csv, $io, $from); | |||||||||||||||||||||||||||||||||||||||||||||
| 226 | 67 | ▶ | return ($next) unless $header; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 43 | ▶ | my @names = @{ $next->() // [] }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
&$next() // []
MC/DC100%1/1
&$next() // []
&$next()
Truth table100%2/2
A
A= &$next()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 43 | ▶ | @names = map { $munge->($_) } @names if $munge; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 43 | my $context = {columns => \@names}; | |||||||||||||||||||||||||||||||||||||||||||||
| 230 | 43 | ▶ | return ($next, $context) if $header eq 'skip'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 37 | my %seen; | |||||||||||||||||||||||||||||||||||||||||||||
| 232 | 37 | for my $name (@names) { | |||||||||||||||||||||||||||||||||||||||||||||
| 233 | croakf q(Expected a header naming each column once, not "%s" twice), $name | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 76 | ▶ | if $seen{$name}++; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 236 | # Without a signature, as it's called for each row. | ||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 87 | ▶ | my $hashes = sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 87 | ▶ | my $row = $next->() or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 53 | my %row; | |||||||||||||||||||||||||||||||||||||||||||||
| 240 | 53 | @row{@names} = @$row; | |||||||||||||||||||||||||||||||||||||||||||||
| 241 | 53 | return \%row; | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | 35 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 243 | 35 | return ($hashes, $context); | |||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | # Return a sub giving the next row read from $io, by $from, or nothing at | ||||||||||||||||||||||||||||||||||||||||||||||
| 248 | # its end, croaking at a row that won't parse. A byte order mark that | ||||||||||||||||||||||||||||||||||||||||||||||
| 249 | # starts the first row is dropped. | ||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 67 | ▶ | sub _rows ($csv, $io, $from) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_rows: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 67 | my $rows_read = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 252 | # Without a signature, as it's called for each row. | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 211 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 211 | my $row; | |||||||||||||||||||||||||||||||||||||||||||||
| 255 | 211 | try { | |||||||||||||||||||||||||||||||||||||||||||||
| 256 | 211 | $row = $csv->getline($io); | |||||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 258 | catch ($e) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 1 | croakf q(Can't read %s: %s), $from, _unplaced($e); | |||||||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 210 | ▶ | unless ($row) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 65 | my($code, $why, undef, undef, $field) = $csv->error_diag; | |||||||||||||||||||||||||||||||||||||||||||||
| 263 | 65 | ▶ | return if $code == $EOF; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 4 | croakf q(Can't parse CSV from %s, record %d, field %d: %s), $from, | |||||||||||||||||||||||||||||||||||||||||||||
| 265 | $rows_read + 1, $field, $why; | ||||||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 145 | ▶ | $row->[0] =~ s( \A \x{FEFF} )()x unless $rows_read++; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 145 | return $row; | |||||||||||||||||||||||||||||||||||||||||||||
| 269 | 67 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||
| 273 | # Return a source of the rows in the file or filehandle. | ||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 51 | ▶ | sub _reader ($option) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reader: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 275 | my($path, $given, $new, $encoding, $header, $munge) | ||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 51 | = @{ $option }{qw(file fh new encoding header munge)}; | |||||||||||||||||||||||||||||||||||||||||||||
| 277 | 51 | ▶ | my $from = $path // 'the filehandle'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$path // "the filehandle"
MC/DC100%1/1
$path // "the filehandle"
$path
Truth table100%2/2
A
A= $path
| |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 51 | ▶ | my $layer = ref $encoding ? sprintf(':encoding(%s)', $encoding->name) : ':raw'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 53 | ▶ | return source { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 53 | my $fh = $given; | |||||||||||||||||||||||||||||||||||||||||||||
| 281 | 53 | ▶ | unless ($fh) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 282 | # Open for the run, and closed by its end. Text that won't | ||||||||||||||||||||||||||||||||||||||||||||||
| 283 | # decode croaks as it's read. | ||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 51 | local $PerlIO::encoding::fallback = $STRICT; ## no critic (Variables::ProhibitPackageVars) | |||||||||||||||||||||||||||||||||||||||||||||
| 285 | 51 | ▶ | open $fh, "<$layer", $path ## no critic (InputOutput::RequireBriefOpen) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 286 | or croakf q(Can't read %s: %s), $path, $!; | ||||||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 52 | my($next, $context) = _table($new->(), $fh, $from, $header, $munge); | |||||||||||||||||||||||||||||||||||||||||||||
| 289 | return { | ||||||||||||||||||||||||||||||||||||||||||||||
| 290 | step => $next, | ||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 48 | ▶ | end => $path ? sub ($how) { close $fh } : undef, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 50 | ▶ | context => $context, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 293 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 51 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||
| 298 | # Return a sub giving a row's line, without its end of line, and, if | ||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # there's a header, a sub giving its line: the names given, or the run's | ||||||||||||||||||||||||||||||||||||||||||||||
| 300 | # context's columns, which also order a hash's fields. A line is in the | ||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # form asked for: characters ('chars'), UTF-8 ('utf8'), or the bytes | ||||||||||||||||||||||||||||||||||||||||||||||
| 302 | # its fields hold ('raw'), each of which must be a byte. | ||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 35 | ▶ | sub _formatter ($csv, $header, $form) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_formatter: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 35 | ▶ | my $names = is_plain_arrayref($header) ? $header : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 35 | my $cut = length $csv->eol; # combine ends a line with eol | |||||||||||||||||||||||||||||||||||||||||||||
| 306 | 57 | ▶ | my $text = sub ($row) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 307 | # The parser is given only bytes: Text::CSV_XS's combine gives | ||||||||||||||||||||||||||||||||||||||||||||||
| 308 | # bytes, characters or a mixture as its fields' flags vary, and | ||||||||||||||||||||||||||||||||||||||||||||||
| 309 | # a line of bytes is the same from both backends. | ||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 57 | my @fields = @$row; | |||||||||||||||||||||||||||||||||||||||||||||
| 311 | 57 | for my $at (0 .. $#fields) { | |||||||||||||||||||||||||||||||||||||||||||||
| 312 | 106 | ▶ | next unless defined $fields[$at]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 102 | ▶ | croakf q(Can't write a row as CSV: field %d is a reference), $at + 1 | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 314 | if is_plain_ref($fields[$at]); | ||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 101 | ▶ | if ($form eq 'raw') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 2 | ▶ | croakf | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 317 | q(Can't write a row as CSV: field %d holds a wide character, and the file is written raw), | ||||||||||||||||||||||||||||||||||||||||||||||
| 318 | $at + 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 319 | unless utf8::downgrade($fields[$at], 1); | ||||||||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 321 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 99 | utf8::encode($fields[$at]); | |||||||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 55 | ▶ | croakf q(Can't write a row as CSV: %s), scalar $csv->error_diag | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 326 | unless $csv->combine(@fields); | ||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 54 | my $line = $csv->string; | |||||||||||||||||||||||||||||||||||||||||||||
| 328 | 54 | ▶ | substr $line, -$cut, $cut, q() if $cut; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 54 | ▶ | utf8::decode($line) if $form eq 'chars'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 54 | return $line; | |||||||||||||||||||||||||||||||||||||||||||||
| 331 | 35 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 332 | 45 | ▶ | my $line = sub ($row) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 45 | ▶ | return $text->($row) if is_plain_arrayref($row); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 19 | ▶ | croakf 'Expected a row as an array or a hash reference' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 335 | unless is_plain_hashref($row); | ||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 18 | ▶ | croakf 'Expected a header naming the columns, to write a hash' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 337 | unless $header; | ||||||||||||||||||||||||||||||||||||||||||||||
| 338 | # The names are known: the header's line comes first. | ||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 17 | return $text->([@{ $row }{@$names}]); | |||||||||||||||||||||||||||||||||||||||||||||
| 340 | 35 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 341 | 35 | ▶ | return ($line) unless $header; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 342 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 343 | $line, | ||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 16 | ▶ | sub () { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 16 | ▶ | $names = _columns() unless $names; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 14 | return $text->($names); | |||||||||||||||||||||||||||||||||||||||||||||
| 349 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 16 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||
| 354 | # Return the run's context's columns, for a header, croaking if it has | ||||||||||||||||||||||||||||||||||||||||||||||
| 355 | # none. | ||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 7 | ▶ | sub _columns () { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_columns: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 7 | my $context = Ekorn::Pipeline::run_context(); | |||||||||||||||||||||||||||||||||||||||||||||
| 358 | 7 | ▶ | my $columns = is_plain_hashref($context) ? $context->{columns} : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 7 | ▶ | croakf q(Expected the run's context to name its columns, for to_csv's header) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 360 | unless is_plain_arrayref($columns); | ||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 5 | return $columns; | |||||||||||||||||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||
| 365 | # Return the form a writer's lines are made in, for _formatter, and a | ||||||||||||||||||||||||||||||||||||||||||||||
| 366 | # sub encoding each, if it needs one: a filehandle takes characters, for | ||||||||||||||||||||||||||||||||||||||||||||||
| 367 | # its own layers; a file, its encoding's bytes, a character the encoding | ||||||||||||||||||||||||||||||||||||||||||||||
| 368 | # can't hold croaking at its row. | ||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 20 | ▶ | sub _encoder ($encoding, $path) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encoder: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 20 | ▶ | return ('chars') unless defined $encoding; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 18 | ▶ | return ('raw') unless ref $encoding; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 16 | ▶ | return ('utf8') if $encoding->name =~ m( \A utf-?8 )xi; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 2 | ▶ | my $encode = sub ($text) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 2 | my $bytes; | |||||||||||||||||||||||||||||||||||||||||||||
| 375 | 2 | try { | |||||||||||||||||||||||||||||||||||||||||||||
| 376 | 2 | $bytes = $encoding->encode($text, $STRICT); | |||||||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 378 | catch ($e) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 1 | croakf q(Can't write %s: %s), $path, _unplaced($e); | |||||||||||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 1 | return $bytes; | |||||||||||||||||||||||||||||||||||||||||||||
| 382 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 383 | 2 | return ('chars', $encode); | |||||||||||||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||||
| 387 | # Return a sink writing each row to the file or filehandle, giving how | ||||||||||||||||||||||||||||||||||||||||||||||
| 388 | # many: after the header's line, if it has one, written even if no row | ||||||||||||||||||||||||||||||||||||||||||||||
| 389 | # comes. | ||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 20 | ▶ | sub _writer ($option) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_writer: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 391 | my($path, $given, $new, $encoding, $header) | ||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 20 | = @{ $option }{qw(file fh new encoding header)}; | |||||||||||||||||||||||||||||||||||||||||||||
| 393 | 20 | ▶ | my $to = $path // 'the filehandle'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$path // "the filehandle"
MC/DC100%1/1
$path // "the filehandle"
$path
Truth table100%2/2
A
A= $path
| |||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 20 | my($form, $encode) = _encoder($encoding, $path); | |||||||||||||||||||||||||||||||||||||||||||||
| 395 | 20 | ▶ | return sink_one { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 20 | my $fh = $given; | |||||||||||||||||||||||||||||||||||||||||||||
| 397 | 20 | ▶ | unless ($fh) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 398 | # Open for the run, and closed by its end. Each line is | ||||||||||||||||||||||||||||||||||||||||||||||
| 399 | # encoded as it's made. | ||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 18 | ▶ | open $fh, '>:raw', $path ## no critic (InputOutput::RequireBriefOpen) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 401 | or croakf q(Can't write %s: %s), $path, $!; | ||||||||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 19 | my $csv = $new->(); | |||||||||||||||||||||||||||||||||||||||||||||
| 404 | 19 | my($line, $head) = _formatter($csv, $header, $form); | |||||||||||||||||||||||||||||||||||||||||||||
| 405 | 19 | ▶ | my $eol = length $csv->eol ? $csv->eol : "\n"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 19 | my $n = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 407 | 35 | ▶ | my $write = sub ($text) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 35 | $text .= $eol; | |||||||||||||||||||||||||||||||||||||||||||||
| 409 | 35 | ▶ | print { $fh } $encode ? $encode->($text) : $text | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 410 | or croakf q(Can't write %s: %s), $to, $!; | ||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 19 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 412 | return { | ||||||||||||||||||||||||||||||||||||||||||||||
| 413 | # Without a signature, as it's called for each row. | ||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 29 | ▶ | step => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 29 | ▶ | $write->($head->()) if $head && !$n; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$head and not $n
MC/DC100%2/2
$head and not $n
$head
not $n
Truth table100%3/3
A and not(B)
A= $headB= not $n
| |||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 29 | $write->($line->($_[0])); | |||||||||||||||||||||||||||||||||||||||||||||
| 417 | 26 | $n++; | |||||||||||||||||||||||||||||||||||||||||||||
| 418 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 16 | ▶ | flush => sub () { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 16 | ▶ | $write->($head->()) if $head && !$n; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$head and not $n
MC/DC100%2/2
$head and not $n
$head
not $n
Truth table100%3/3
A and not(B)
A= $headB= not $n
| |||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 16 | return $n; | |||||||||||||||||||||||||||||||||||||||||||||
| 422 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 423 | end => $path | ||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 17 | ▶ | ? sub ($how) { close $fh or croakf q(Can't write %s: %s), $path, $! } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 425 | : undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 19 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 20 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||
| 430 | # The rows of a text, a line at a time, for Text::CSV's getline, which | ||||||||||||||||||||||||||||||||||||||||||||||
| 431 | # reads from any object that has one. A filehandle opened on the text | ||||||||||||||||||||||||||||||||||||||||||||||
| 432 | # would need a check on an open that can't fail, which no test reaches. | ||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 4 | package Ekorn::Pipeline::CSV::Text { ## no critic (Modules::ProhibitMultiplePackages) | |||||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 15 | ▶ | sub new ($class, $text) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 15 | return bless [split m( (?<= \n ) )x, $text], $class; | |||||||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 46 | ▶ | sub getline ($self) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
getline: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 46 | return shift @$self; | |||||||||||||||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||||
| 448 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||