blib/lib/IO/CSVHeaderFile.pm

filter: Statementstmt 86.8% 86 / 99 Branchbran 67.1% 43 / 64 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 77.5% 145 / 187 CC 36 SCAR 29.9
CC
36
Cov
78%
open
38.7
csv_print
12.1
csv_read
6.1
CRAP 19.9
↑ Summary
1 package IO::CSVHeaderFile;
2 # $Id: CSVHeaderFile.pm,v 1.2 2007/07/06 08:44:46 vasek Exp $
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw(@ISA @EXPORT $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 @ISA = qw(IO::File Exporter);
12  
13 1 @EXPORT = qw(
14
15 );
16  
17 1 $VERSION = '0.04';
18  
19 1 my $SUPPORTED_XS_ARGS;
20  
21 2 ▶sub open {
Subroutine open: called CC=18 SCAR=36.6
Pod documented
22 2 my $self = shift;
23 2 ▶ my $args = {}; $args = pop @_ if ref($_[$#_]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[$#_] eq "HASH" 1 1 2
24 2 _init_supported_xs_args();
25 2 ▶ my %xs_args = ( 'eol' => "\n", map {exists $SUPPORTED_XS_ARGS->{$_} ? ($_ => $args->{$_}):()} keys %$args);
Branch: 1 branch
BranchTrueFalseTotal
exists $SUPPORTED_XS_ARGS->{$_} ? : 0 1 1
26 2 my $csv = Text::CSV_XS->new(\%xs_args);
27 2 my $mode;
28 2 ▶ if(@_ > 1){
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 2 2
29 0 ▶ croak 'usage: $fh->open(FILENAME [ ,< > >> ][,CSVOPT])' if $_[2] =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] =~ /^\d+$/ 0 0 0
30 0 $mode = IO::Handle::_open_mode_string($_[1]);
31 }else{
32 2 $mode = $_[0];
33 2 ▶ $mode =~ s/^(\+?<|>>?)(.*)$/$1/
Branch: 1 branch
BranchTrueFalseTotal
$mode =~ s/^(\+?<|>>?)(.*)$/$1/ or croak("usage: \$fh->open(FILENAME [,< > >> ][,CSVOPT])") 2 0 2
34 or croak 'usage: $fh->open(FILENAME [,< > >> ][,CSVOPT])';
35 }
36 2 my ($fh, $firstline);
37 2 ▶ if($mode =~ /^<$/){
Branch: 2 branches
BranchTrueFalseTotal
if ($mode =~ /^<$/) { } 1 1 2
elsif ($mode =~ /^>>?$/) { } 1 0 1
38 1 ▶ $fh = $self->SUPER::open( @_ ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$fh = $self->SUPER::open(@_) or return 1 0 1
39 1 ▶ unless($args->{noheader}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"noheader"}) 1 0 1
40 1 ▶ unless( $firstline = $self->getline ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($firstline = $self->getline) 0 1 1
41 0 $self->close;
42 0 return;
43 }
44 $csv->parse($firstline) and $args->{col} = [ $csv->fields ]
45 1 ▶ unless $args->{col};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"col"} 1 0 1
Condition50%2/4
$args->{'col'} || $csv->parse($firstline) && ($args->{'col'} = [$csv->fields])
l!l
01
$csv->parse($firstline) and $args->{"col"} = [$csv->fields]
!ll
01
MC/DC0%0/2
$args->{'col'} || $csv->parse($firstline) && ($args->{'col'} = [$csv->fields])
$args->{'col'} $csv->parse($firstline)
Truth table33%1/3
A || B
A= $args->{'col'}B= $csv->parse($firstline)
ABresult
1X1
011
000
46 }
47 1 ▶ unless(${*$self}{io_csvheaderfile_cols} = $args->{col}){
Branch: 1 branch
BranchTrueFalseTotal
unless (do { *$self }->{"io_csvheaderfile_cols"} = $args->{"col"}) 0 1 1
48 0 $self->close;
49 0 croak "IO::CSVHeaderFile: Can't find the column names in '$_[0]'";
50 0 return;
51 }
52 }elsif( $mode =~ /^>>?$/){
53 1 ▶ unless(${*$self}{io_csvheaderfile_cols} = $args->{col}){
Branch: 1 branch
BranchTrueFalseTotal
unless (do { *$self }->{"io_csvheaderfile_cols"} = $args->{"col"}) 0 1 1
54 0 $self->close;
55 0 croak "IO::CSVHeaderFile: Can't find the column names in '$_[0]'";
56 0 return;
57 }
58 1 ▶ $fh = $self->SUPER::open( @_ ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$fh = $self->SUPER::open(@_) or return 1 0 1
59 $csv->print($self, $args->{col})
60 1 ▶ unless $mode =~ /^>>$/ or $args->{noheader};
Branch: 1 branch
BranchTrueFalseTotal
unless $mode =~ /^>>$/ or $args->{"noheader"} 1 0 1
Condition33%1/3
$mode =~ /^>>$/ or $args->{"noheader"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$mode =~ /^>>$/ or $args->{"noheader"}
$mode =~ /^>>$/ $args->{"noheader"}
Truth table33%1/3
A or B
A= $mode =~ /^>>$/B= $args->{"noheader"}
ABresult
1X1
011
000
61 }else{
62 0 croak "IO::CSVHeaderFile: Invalid mode '$mode'";
63 0 return;
64 }
65 2 ${*$self}{io_csvheaderfile_csv} = $csv;
66 2 $fh
67 }
68  
69 3 ▶sub csv_read{
Subroutine csv_read: called CC=6 SCAR=18.0
Pod documented
70 3 my $self = shift;
71 3 ▶ my $line = $self->getline() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $line = $self->getline or return 3 0 3
72 3 my @result = ();
73 3 ▶ if( ${*$self}{io_csvheaderfile_csv}->parse($line) ){
Branch: 1 branch
BranchTrueFalseTotal
if (do { *$self }->{"io_csvheaderfile_csv"}->parse($line)) 3 0 3
74 3 my @cols = ${*$self}{io_csvheaderfile_csv}->fields;
75 3 my $colnames = ${*$self}{io_csvheaderfile_cols};
76 3 ▶ my $avail_cols = (@cols > @$colnames)? @$colnames : @cols;
Branch: 1 branch
BranchTrueFalseTotal
@cols > @$colnames ? : 0 3 3
77 3 ▶ for(my $i = 0; $i < $avail_cols; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $avail_cols) 9 3 12
78 9 push @result, $colnames->[$i] => $cols[$i];
79 }
80 }
81 3 ▶ wantarray? @result : { @result }
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 2 3
82 }
83  
84 3 ▶sub csv_print{
Subroutine csv_print: called CC=12 SCAR=24.9
Pod documented
85 3 my $self = shift;
86 3 ▶ return undef unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 3 3
87 3 my $rec = $_[0];
88 3 my @columns = ();
89 3 my $colnames = ${*$self}{io_csvheaderfile_cols};
90 3 ▶ unless( ref $rec ){
Branch: 3 branches
BranchTrueFalseTotal
if (ref $rec) { } 2 1 3
if (do { ref $rec eq 'HASH' }) { } 1 1 2
elsif (ref $rec eq 'ARRAY') { } 1 0 1
91 1 my %map = ();
92 1 ▶ for(my $i = 0; $i < @$colnames; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$colnames) 3 1 4
93 3 ▶ $map{$colnames->[$i]} = [] unless exists $map{$colnames->[$i]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $map{$colnames->[$i]} 2 1 3
94 3 push @{$map{$colnames->[$i]}}, $i;
95 }
96 1 ▶ while ( my ($key, $value) = splice(@_, 0, 2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 4 1 5
97 4 ▶ my $idx = $map{$key} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $idx = $map{$key} or next 4 0 4
98 4 $columns[$idx->[0]] = $value;
99 4 ▶ shift @$idx if @$idx > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$idx > 1 1 3 4
100 }
101 }elsif( ref ($rec) eq 'HASH' ){
102 1 push( @columns, $rec->{$_}) foreach (@$colnames);
103 }elsif( ref ($rec) eq 'ARRAY' ){
104 1 ▶ for( my $i = 0; $i < @$colnames; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$colnames) 3 1 4
105 3 push @columns, $rec->[$i];
106 }
107 }
108 3 ${*$self}{io_csvheaderfile_csv}->print($self,\@columns);
109 }
110  
111 2 ▶sub _init_supported_xs_args {
Subroutine _init_supported_xs_args: called CC=3 SCAR=11.4
112 2 ▶ return if defined $SUPPORTED_XS_ARGS;
Branch: 1 branch
BranchTrueFalseTotal
if defined $SUPPORTED_XS_ARGS 1 1 2
113 1 my $tmpcsvxs = Text::CSV_XS->new();
114 $SUPPORTED_XS_ARGS = UNIVERSAL::isa($tmpcsvxs, "HASH")?
115 0 ▶ {%$tmpcsvxs}: {map {$_ => undef} qw(eol sep_char allow_whitespace quote_char
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($tmpcsvxs, 'HASH') ? : 1 0 1
116 allow_loose_quotes escape_char allow_loose_escapes binary types always_quote
117 keep_meta_info)};
118 }
119  
120 1 1;
121 __END__
↑ Summary