| 1 | package Mojo::CSV; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Mojo::Collection qw/c/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Text::CSV; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Scalar::Util qw/reftype/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use MooX::ChainedAttributes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use strictures 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | our $VERSION = '1.001005'; # VERSION | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | has in => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | chained => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 8 | ▶ | coerce => sub { __get_fh( $_[0], '<' ); } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | has out => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | chained => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | ▶ | coerce => sub { __get_fh( $_[0], '>' ); } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | has _obj => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 13 | ▶ | default => sub { Text::CSV->new({ binary => 1 }) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 11 | ▶ | sub __get_fh { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__get_fh: called CC=7 SCAR=19.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 11 | my ( $what, $how ) = @_;; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 11 | my $fh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 11 | ▶ | if ( ref $what eq 'Mojo::Asset::File' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | $fh = $what->handle; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | elsif ( ref $what eq 'Mojo::Asset::Memory' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | ▶ | open $fh, $how, \ $what->slurp or die $!; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | elsif ( ref $what ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | $fh = $what; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | elsif ( defined $what ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 6 | ▶ | open $fh, $how, $what or croak "Failed to open CSV file ($what): $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | croak 'Could not figure out how to access resource'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 11 | return $fh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | ▶ | sub flush { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flush: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | close $self->out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 64 | ▶ | sub row { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
row: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 64 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 64 | ▶ | my $in = $self->in or croak 'You must specify what to read from'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 63 | ▶ | my $row = $self->_obj->getline( $in ) or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 56 | return $row; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 6 | ▶ | sub slurp { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
slurp: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 6 | ▶ | @_ and $self->in( shift ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 6 | my @rows; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 6 | ▶ | while ( my $r = $self->row ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 48 | push @rows, $r; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 6 | return c @rows; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | ▶ | sub slurp_body { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
slurp_body: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | return $self->slurp( @_ )->tail(-1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | ▶ | sub spurt { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spurt: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | my $what = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | ▶ | @_ and $self->out( shift ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 2 | $self->trickle( $_ ) for @$what; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 2 | $self->flush; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 2 | $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 9 | ▶ | sub text { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
text: called CC=9 SCAR=23.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 9 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 9 | ▶ | my $what = shift or return ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 9 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 9 | my $obj = $self->_obj; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 9 | ▶ | if ( reftype $what | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition55%5/9
builtin::reftype($what) and builtin::reftype($what) eq "ARRAY" and builtin::reftype($what->[0]) and builtin::reftype($what->[0]) eq "ARRAY"
builtin::reftype($what) and builtin::reftype($what) eq "ARRAY" and builtin::reftype($what->[0])
builtin::reftype($what) and builtin::reftype($what) eq "ARRAY"
MC/DC25%1/4
builtin::reftype($what) and builtin::reftype($what) eq "ARRAY" and builtin::reftype($what->[0]) and builtin::reftype($what->[0]) eq "ARRAY"
builtin::reftype($what)
builtin::reftype($what) eq "ARRAY"
builtin::reftype($what->[0])
builtin::reftype($what->[0]) eq "ARRAY"
Truth table40%2/5
A and B and C and D
A= builtin::reftype($what)B= builtin::reftype($what) eq "ARRAY"C= builtin::reftype($what->[0])D= builtin::reftype($what->[0]) eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | and reftype $what eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | and reftype $what->[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | and reftype $what->[0] eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | for my $row ( @$what ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 8 | ▶ | $obj->combine( @$row ) or next; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 8 | push @out, $obj->string; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 8 | ▶ | $obj->combine( @$what ) and push @out, $obj->string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 9 | return join "\n", @out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 19 | ▶ | sub trickle { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trickle: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 19 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 19 | ▶ | my $what = shift or return $self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 19 | ▶ | my $out = $self->out or croak 'You must specify where to write'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 18 | my $obj = $self->_obj; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 18 | ▶ | $obj->combine( @$what ) or return $self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 18 | print $out $obj->string . "\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 18 | $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 2 | q| | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | Computers are like air conditioners. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | They work fine until you start opening windows. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||