CCSV.pm

filter: Statementstmt 54.6% 35 / 64 Branchbran 18.7% 6 / 32 Conditioncond 28.2% 11 / 39 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 63.6% 7 / 11 Podpod 100% 3 / 3 totaltotal 36.9% 62 / 168 CC 34 SCAR 51.8
CC
34
Cov
39%
_init
273.4
_confme
30.0
cprint
6.0
CRAP 178.1
↑ Summary CCSV/TieCSV.pm »
1 package App::CCSV;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use App::CCSV::TieCSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Config::General;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 require Exporter;
9 1 our @ISA = qw(Exporter);
10  
11 1 our $csv;
12 1 our $csvout;
13  
14 1 our @EXPORT = qw(cprint csay parse $csv);
15  
16 1 our $VERSION = 0.02;
17  
18 sub import
19 1 ▶{
Subroutine import: called CC=1 SCAR=0.0
20 1 my $name = shift;
21 1 _init(@_);
22 1 __PACKAGE__->export_to_level(1,1,qw(csay cprint parse $csv));
23 1 tie *ARGV, 'TieCSV',$csv;
24 }
25  
26 sub _init
27 1 ▶{
Subroutine _init: called CC=28 SCAR=56.1
28 1 ▶ my $out = length($_[-1] || '') > 1 && $#_ > 0 ? pop : ''; # last argument length > 1 means csvout config section if more than 1 argument
Branch: 1 branch
BranchTrueFalseTotal
length($_[-1] || '') > 1 && $#_ > 0 ? : 0 1 1
Condition40%2/5
length($_[-1] || "") > 1 and $#_ > 0
!ll&&!rl&&r
100
$_[-1] || ''
l!l
01
MC/DC0%0/2
length($_[-1] || "") > 1 and $#_ > 0
$_[-1] $#_ > 0
Truth table33%1/3
A and B
A= $_[-1]B= $#_ > 0
ABresult
0X0
100
111
29 1 my ($quote,$sep,$escape,$eol) = @_;
30 #print join "|", ($quote, $sep, $escape, $eol,"\n");
31 1 ▶ my $cfgfile = $ENV{CCSVCONF} || $ENV{HOME} || '~';
Condition40%2/5
$ENV{'CCSVCONF'} || $ENV{'HOME'} || '~'
l!l
10
$ENV{"CCSVCONF"} or $ENV{"HOME"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$ENV{'CCSVCONF'} || $ENV{'HOME'} || '~'
$ENV{"CCSVCONF"} $ENV{"HOME"}
Truth table33%1/3
A or B
A= $ENV{"CCSVCONF"}B= $ENV{"HOME"}
ABresult
1X1
011
000
32 1 ▶ $cfgfile = $cfgfile . '/.CCSVConf' unless $ENV{CCSVCONF};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"CCSVCONF"} 1 0 1
33 1 ▶ if (length($quote || '') <= 1) # length > 1 specifies config section
Branch: 1 branch
BranchTrueFalseTotal
if (length($quote || '') <= 1) { } 1 0 1
Condition50%1/2
$quote || ''
l!l
01
MC/DC0%0/1
$quote || ''
$quote
Truth table50%1/2
A
A= $quote
Aresult
11
00
34 {
35 1 ▶ $csv = Text::CSV_XS->new( {
Condition50%4/8
$quote || '"'
l!l
01
$sep || ','
l!l
01
$escape || '"'
l!l
01
$eol || ''
l!l
01
MC/DC0%0/1
$quote || '"'
$quote
MC/DC0%0/1
$sep || ','
$sep
MC/DC0%0/1
$escape || '"'
$escape
MC/DC0%0/1
$eol || ''
$eol
Truth table50%1/2
A
A= $quote
Aresult
11
00
Truth table50%1/2
A
A= $sep
Aresult
11
00
Truth table50%1/2
A
A= $escape
Aresult
11
00
Truth table50%1/2
A
A= $eol
Aresult
11
00
36 quote_char => $quote || '"',
37 sep_char => $sep || ',',
38 escape_char => $escape || '"',
39 eol => $eol || '',
40 binary => 1,
41 } );
42 } else
43 {
44 0 $csv = Text::CSV_XS->new(); # config later from cfgfile
45 }
46 1 ▶ if (-r $cfgfile)
Branch: 1 branch
BranchTrueFalseTotal
if (-r $cfgfile) { } 0 1 1
47 {
48 0 my $conf = new Config::General($cfgfile);
49 0 my %config = $conf->getall;
50 # CSV:
51 0 ▶ if (!@_ || ($#_ == 0 and $quote eq '')) # no override(s) from command line
Branch: 2 branches
BranchTrueFalseTotal
if (not @_ or $#_ == 0 and $quote eq '') { } 0 0 0
elsif (length $quote > 1) { } 0 0 0
Condition0%0/6
not @_ or $#_ == 0 and $quote eq ""
l!l&&r!l&&!r
000
$#_ == 0 and $quote eq ""
!ll&&!rl&&r
000
MC/DC0%0/3
not @_ or $#_ == 0 and $quote eq ""
not @_ $#_ == 0 $quote eq ""
Truth table0%0/4
not(A) or B and C
A= not @_B= $#_ == 0C= $quote eq ""
ABCresult
1XX1
0111
00X0
0100
52 {
53 0 ▶ _confme($csv,$config{CCSV}) if $config{CCSV};
Branch: 1 branch
BranchTrueFalseTotal
if $config{"CCSV"} 0 0 0
54 } elsif (length($quote) > 1) # config section
55 {
56 0 ▶ die "no config section with name <$quote> could be found in $cfgfile\n" unless $config{CCSV}->{names}->{$quote};
Branch: 1 branch
BranchTrueFalseTotal
unless $config{"CCSV"}{"names"}{$quote} 0 0 0
57 0 _confme($csv,$config{CCSV}->{names}->{$quote});
58 }
59  
60 0 ▶ _confme($csv,$config{CCSV}->{overrideall}) if $config{CCSV}->{overrideall};
Branch: 1 branch
BranchTrueFalseTotal
if $config{"CCSV"}{"overrideall"} 0 0 0
61 # different CSVOUT?
62 0 ▶ if ($out) # get from config section
Branch: 2 branches
BranchTrueFalseTotal
if ($out) { } 0 0 0
elsif ($config{'CCSV'}{'out'}) { } 0 0 0
63 {
64 0 ▶ die "no config section with name <$out> could be found in $cfgfile\n" unless $config{CCSV}->{names}->{$out};
Branch: 1 branch
BranchTrueFalseTotal
unless $config{"CCSV"}{"names"}{$out} 0 0 0
65 0 $csvout = Text::CSV_XS->new();
66 0 _confme($csvout,$config{CCSV}->{names}->{$out});
67 } elsif ($config{CCSV}->{out})
68 {
69 0 $csvout = Text::CSV_XS->new();
70 0 _confme($csvout,$config{CCSV}->{out});
71 }
72 } else # die if config file is needed but not present:
73 {
74 1 ▶ die "1st param char length > 1 means input config section, but no readable config file could be found at $cfgfile \n" if (length($quote || '') > 1);
Branch: 1 branch
BranchTrueFalseTotal
if length($quote || "") > 1 0 1 1
Condition50%1/2
$quote || ''
l!l
01
MC/DC0%0/1
$quote || ''
$quote
Truth table50%1/2
A
A= $quote
Aresult
11
00
75 1 ▶ die "last param length > 1 means output config section, but no readable config file could be found at $cfgfile\n" if (length($out || '') > 1);
Branch: 1 branch
BranchTrueFalseTotal
if length($out || "") > 1 0 1 1
Condition50%1/2
$out || ''
l!l
01
MC/DC0%0/1
$out || ''
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
76 }
77 }
78  
79 sub _confme
80 0 ▶{
Subroutine _confme: not called CC=5 SCAR=34.0
81 # ($csv,$conf);
82 0 my $conf = $_[1];
83 0 for my $key (keys %{$conf})
84 {
85 0 ▶ next if ($key eq 'overrideall' or $key eq 'out' or $key eq 'names');
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "overrideall" or $key eq "out" or $key eq "names" 0 0 0
Condition0%0/6
$key eq "overrideall" or $key eq "out" or $key eq "names"
l!l&&r!l&&!r
000
$key eq "overrideall" or $key eq "out"
l!l&&r!l&&!r
000
MC/DC0%0/3
$key eq "overrideall" or $key eq "out" or $key eq "names"
$key eq "overrideall" $key eq "out" $key eq "names"
Truth table0%0/4
A or B or C
A= $key eq "overrideall"B= $key eq "out"C= $key eq "names"
ABCresult
1XX1
01X1
0011
0000
86 0 $_[0]->$key($conf->{$key});
87 }
88 }
89  
90 sub cprint
91 0 ▶{
Subroutine cprint: not called CC=2 SCAR=17.9
Pod documented
92 0 ▶ if ($csvout)
Branch: 1 branch
BranchTrueFalseTotal
if ($App::CCSV::csvout) { } 0 0 0
93 {
94 0 $csvout->combine(@_);
95 0 print $csvout->string();
96  
97 } else
98 {
99 0 $csv->combine(@_);
100 0 print $csv->string();
101 }
102 }
103  
104 sub csay
105 0 ▶{
Subroutine csay: not called CC=1 SCAR=6.9
Pod documented
106 0 cprint(@_);
107 0 print $/;
108 }
109  
110 sub parse
111 0 ▶{
Subroutine parse: not called CC=2 SCAR=17.9
Pod documented
112 0 my $line = shift;
113 0 ▶ $csv->parse($line || $_);
Condition0%0/3
$line || $_
l!l&&r!l&&!r
000
MC/DC0%0/2
$line || $_
$line $_
Truth table0%0/3
A || B
A= $lineB= $_
ABresult
1X1
011
000
114 0 return $csv->fields();
115 }
116  
117 1 1;
118  
119  
120 __END__
↑ Summary CCSV/TieCSV.pm »