| 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
| |||||||||||||||||||||||||||||||
| 24 | 2 | _init_supported_xs_args(); | |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | my %xs_args = ( 'eol' => "\n", map {exists $SUPPORTED_XS_ARGS->{$_} ? ($_ => $args->{$_}):()} keys %$args); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 26 | 2 | my $csv = Text::CSV_XS->new(\%xs_args); | |||||||||||||||||||||||||||||
| 27 | 2 | my $mode; | |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | if(@_ > 1){ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | croak 'usage: $fh->open(FILENAME [ ,< > >> ][,CSVOPT])' if $_[2] =~ /^\d+$/; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 30 | 0 | $mode = IO::Handle::_open_mode_string($_[1]); | |||||||||||||||||||||||||||||
| 31 | }else{ | ||||||||||||||||||||||||||||||
| 32 | 2 | $mode = $_[0]; | |||||||||||||||||||||||||||||
| 33 | 2 | ▶ | $mode =~ s/^(\+?<|>>?)(.*)$/$1/ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 34 | or croak 'usage: $fh->open(FILENAME [,< > >> ][,CSVOPT])'; | ||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||
| 36 | 2 | my ($fh, $firstline); | |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | if($mode =~ /^<$/){ | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | $fh = $self->SUPER::open( @_ ) or return; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | unless($args->{noheader}){ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | unless( $firstline = $self->getline ){ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
Condition50%2/4
$args->{'col'} || $csv->parse($firstline) && ($args->{'col'} = [$csv->fields])
$csv->parse($firstline) and $args->{"col"} = [$csv->fields]
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)
| |||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | unless(${*$self}{io_csvheaderfile_cols} = $args->{col}){ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 59 | $csv->print($self, $args->{col}) | ||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | unless $mode =~ /^>>$/ or $args->{noheader}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$mode =~ /^>>$/ or $args->{"noheader"}
MC/DC0%0/2
$mode =~ /^>>$/ or $args->{"noheader"}
$mode =~ /^>>$/
$args->{"noheader"}
Truth table33%1/3
A or B
A= $mode =~ /^>>$/B= $args->{"noheader"}
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 72 | 3 | my @result = (); | |||||||||||||||||||||||||||||
| 73 | 3 | ▶ | if( ${*$self}{io_csvheaderfile_csv}->parse($line) ){ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | for(my $i = 0; $i < $avail_cols; $i++){ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 78 | 9 | push @result, $colnames->[$i] => $cols[$i]; | |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||
| 81 | 3 | ▶ | wantarray? @result : { @result } | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 91 | 1 | my %map = (); | |||||||||||||||||||||||||||||
| 92 | 1 | ▶ | for(my $i = 0; $i < @$colnames; $i++){ | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 93 | 3 | ▶ | $map{$colnames->[$i]} = [] unless exists $map{$colnames->[$i]}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 94 | 3 | push @{$map{$colnames->[$i]}}, $i; | |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | while ( my ($key, $value) = splice(@_, 0, 2) ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 97 | 4 | ▶ | my $idx = $map{$key} or next; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 98 | 4 | $columns[$idx->[0]] = $value; | |||||||||||||||||||||||||||||
| 99 | 4 | ▶ | shift @$idx if @$idx > 1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 116 | allow_loose_quotes escape_char allow_loose_escapes binary types always_quote | ||||||||||||||||||||||||||||||
| 117 | keep_meta_info)}; | ||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||
| 120 | 1 | 1; | |||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||||