Encoded.pm

filter: Statementstmt 96.7% 118 / 122 Branchbran 76.4% 52 / 68 Conditioncond 63.6% 14 / 22 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 22 / 22 Podpod 92.3% 12 / 13 totaltotal 84.9% 220 / 259 CC 46 SCAR 15.9
CC
46
Cov
87%
new
9.1
BEGIN
7.7
encode
6.5
CRAP 4.9
↑ Summary Encoded/Coder/EncodeGuess.pm »
1 package Text::CSV::Encoded;
2 13 $Text::CSV::Encoded::VERSION = '0.25';
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # VERSION
8  
9  
10 13 ▶BEGIN {
Subroutine BEGIN: called CC=6 SCAR=20.5
11 13 require Text::CSV;
12 13 ▶ if ( Text::CSV->VERSION < 1.06 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Text::CSV"->VERSION < "1.06") 0 13 13
13 0 Carp::croak "Base class Text::CSV version is less than 1.06.";
14 }
15 13 my $backend = Text::CSV->backend;
16 13 my $version = Text::CSV->backend->VERSION;
17 13 ▶ if ( ( $backend =~ /XS/ and $version >= 0.99 ) or ( $backend =~ /PP/ and $version >= 1.30 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend =~ /XS/ and $version >= 0.99 or $backend =~ /PP/ and $version >= 1.3) { } 13 0 13
Condition55%5/9
$backend =~ /XS/ and $version >= "0.99" or $backend =~ /PP/ and $version >= "1.3"
l!l&&r!l&&!r
580
$backend =~ /XS/ and $version >= "0.99"
!ll&&!rl&&r
805
$backend =~ /PP/ and $version >= "1.3"
!ll&&!rl&&r
008
MC/DC0%0/4
$backend =~ /XS/ and $version >= "0.99" or $backend =~ /PP/ and $version >= "1.3"
$backend =~ /XS/ $version >= "0.99" $backend =~ /PP/ $version >= "1.3"
Truth table28%2/7
A and B or C and D
A= $backend =~ /XS/B= $version >= "0.99"C= $backend =~ /PP/D= $version >= "1.3"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
18 13 ▶ eval q/ sub automatic_UTF8 { 1; } /; # parse/getline return strings (UNICODE)
Subroutine automatic_UTF8: called CC=1 SCAR=0.0
Pod undocumented
19 }
20 else {
21 0 eval q/ sub automatic_UTF8 { 0; } /;
22 }
23 }
24  
25 13 ▶use base qw( Text::CSV );
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 13 ▶my $DefaultCoderClass = $] >= 5.008 ? 'Text::CSV::Encoded::Coder::Encode'
Branch: 1 branch
BranchTrueFalseTotal
$] >= 5.008 ? : 13 0 13
29 : 'Text::CSV::Encoded::Coder::Base';
30 13 my @Attrs;
31  
32  
33 13 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
34 13 @Attrs = qw(
35 encoding
36 encoding_in encoding_out
37 encoding_io_in encoding_io_out
38 encoding_to_parse encoding_to_combine
39 );
40 }
41  
42  
43 4 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
44 4 my ( $class, %args ) = @_;
45  
46 4 ▶ return unless %args;
Branch: 1 branch
BranchTrueFalseTotal
unless %args 3 1 4
47  
48 1 ▶ if ( exists $args{ coder_class } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"coder_class"}) 1 0 1
49 1 $DefaultCoderClass = $args{ coder_class };
50 }
51  
52 }
53  
54  
55 13 ▶sub new {
Subroutine new: called CC=9 SCAR=22.1
Pod documented
56 13 my $class = shift;
57 13 ▶ my $opt = shift || {};
Condition100%2/2
shift() || {}
l!l
85
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
58 13 my %opt;
59  
60 13 $opt->{binary} = 1;
61  
62 13 for my $attr ( @Attrs, 'encoding', 'coder_class' ) {
63 117 ▶ $opt{ $attr } = delete $opt->{ $attr } if ( exists $opt->{ $attr } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt->{$attr} 9 108 117
64 }
65  
66 13 ▶ my $self = $class->SUPER::new( $opt ) || return;
Condition100%2/2
$class->SUPER::new($opt) || (return)
l!l
121
MC/DC100%1/1
$class->SUPER::new($opt) || (return)
$class->SUPER::new($opt)
Truth table100%2/2
A
A= $class->SUPER::new($opt)
Aresult
11
00
67  
68 12 ▶ if ( my $coder_class = ( $opt{coder_class} || $DefaultCoderClass ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $coder_class = $opt{'coder_class'} || $DefaultCoderClass) { } 12 0 12
Condition66%2/3
$opt{'coder_class'} || $DefaultCoderClass
l!l&&r!l&&!r
1110
MC/DC0%0/2
$opt{'coder_class'} || $DefaultCoderClass
$opt{'coder_class'} $DefaultCoderClass
Truth table66%2/3
A || B
A= $opt{'coder_class'}B= $DefaultCoderClass
ABresult
1X1
011
000
69 12 $self->coder_class( $coder_class );
70 }
71 else {
72 0 Carp::croak "Coder class is not specified.";
73 }
74  
75 12 for my $attr ( @Attrs, 'encoding' ) {
76 96 ▶ $self->$attr( $opt{ $attr } ) if ( exists $opt{ $attr } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{$attr} 8 88 96
77 }
78  
79 12 $self;
80 }
81  
82  
83 #
84 # Methods
85 #
86  
87 39 ▶sub combine {
Subroutine combine: called CC=4 SCAR=13.9
Pod documented
88 39 my $self = shift;
89 39 my @fields = @_;
90  
91 39 ▶ $self->coder->decode_fields_ref( $self->encoding, \@fields ) if ( $self->encoding );
Branch: 1 branch
BranchTrueFalseTotal
if $self->encoding 1 38 39
92  
93 39 ▶ unless ( $self->encoding_out ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->encoding_out) 12 27 39
94 12 return $self->SUPER::combine( @fields );
95 }
96  
97 27 my $ret = $self->encode( $self->encoding_out, \@fields );
98  
99 27 ▶ $self->{_STRING} = \$ret if ( $ret );
Branch: 1 branch
BranchTrueFalseTotal
if $ret 27 0 27
100  
101 27 return $self->{_STATUS};
102 }
103  
104  
105 42 ▶sub parse {
Subroutine parse: called CC=5 SCAR=16.4
Pod documented
106 42 my $self = shift;
107 42 my $ret;
108  
109 42 ▶ if ( $self->encoding_in ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->encoding_in) { } 30 12 42
110 30 $ret = $self->decode( $self->encoding_in, $_[0] );
111 }
112 else {
113 12 ▶ $ret = [ $self->fields ] if $self->SUPER::parse( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if $self->SUPER::parse(@_) 12 0 12
114 }
115  
116 42 ▶ if ( $ret ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) 42 0 42
117 42 ▶ $self->coder->encode_fields_ref( $self->encoding, $ret ) if ( $self->encoding );
Branch: 1 branch
BranchTrueFalseTotal
if $self->encoding 0 42 42
118 42 $self->{_FIELDS} = $ret;
119 }
120  
121 42 return $self->{_STATUS};
122 }
123  
124  
125 #
126 # IO style
127 #
128  
129 8 ▶sub print { # to CSV
Subroutine print: called CC=2 SCAR=7.0
Pod documented
130 8 my ( $self, $io, $cols ) = @_;
131  
132 8 ▶ $self->coder->decode_fields_ref( $self->encoding, $cols ) if ( $self->encoding );
Branch: 1 branch
BranchTrueFalseTotal
if $self->encoding 0 8 8
133 8 $self->coder->encode_fields_ref( $self->encoding_out, $cols );
134  
135 8 $self->SUPER::print( $io, $cols );
136 }
137  
138  
139 62 ▶sub getline { # from CSV
Subroutine getline: called CC=6 SCAR=17.9
Pod documented
140 62 my ( $self, $io ) = @_;
141 62 my $cols = $self->SUPER::getline( $io );
142  
143 62 ▶ if ( my $binds = $self->{_BOUND_COLUMNS} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $binds = $self->{"_BOUND_COLUMNS"}) 10 52 62
144 10 for my $val ( @$binds ) {
145 20 $$val = $self->coder->decode( $self->encoding_in, $$val );
146 20 ▶ $$val = $self->coder->encode( $self->encoding, $$val ) if ( $self->encoding );
Branch: 1 branch
BranchTrueFalseTotal
if $self->encoding 20 0 20
147 }
148 10 return $cols;
149 }
150  
151 52 ▶ return unless $cols;
Branch: 1 branch
BranchTrueFalseTotal
unless $cols 10 42 52
152  
153 42 $self->coder->decode_fields_ref( $self->encoding_in, $cols );
154 42 ▶ $self->coder->encode_fields_ref( $self->encoding, $cols ) if ( $self->encoding );
Branch: 1 branch
BranchTrueFalseTotal
if $self->encoding 16 26 42
155  
156 42 $cols;
157 }
158  
159  
160 #
161 # decode/encode style
162 #
163  
164 48 ▶sub decode {
Subroutine decode: called CC=6 SCAR=18.0
Pod documented
165 48 my ( $self, $enc, $text ) = @_;
166  
167 48 ▶ if ( @_ == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 9 39 48
168 9 $text = $enc, $enc = '';
169 }
170  
171 48 ▶ $self->coder->upgrade( $text ) unless ( $enc ); # as unicode
Branch: 1 branch
BranchTrueFalseTotal
unless $enc 9 39 48
172  
173 48 ▶ return unless ( defined $text );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 48 48
174 48 ▶ return unless ( $self->SUPER::parse( $text ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SUPER::parse($text) 0 48 48
175  
176 48 ▶ return $enc ? [ map { $self->coder->decode( $enc, $_ ) } $self->fields() ] : [ $self->fields() ];
Branch: 1 branch
BranchTrueFalseTotal
$enc ? : 39 9 48
177 }
178  
179  
180 43 ▶sub encode {
Subroutine encode: called CC=6 SCAR=18.7
Pod documented
181 43 my ( $self, $enc, $array ) = @_;
182  
183 43 ▶ if ( @_ == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 8 35 43
184 8 $array = $enc, $enc = '';
185 }
186  
187 43 ▶ return unless ( defined $array and ref $array eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $array and ref $array eq "ARRAY" 0 43 43
Condition33%1/3
defined $array and ref $array eq "ARRAY"
!ll&&!rl&&r
0043
MC/DC0%0/2
defined $array and ref $array eq "ARRAY"
defined $array ref $array eq "ARRAY"
Truth table33%1/3
A and B
A= defined $arrayB= ref $array eq "ARRAY"
ABresult
0X0
100
111
188 43 ▶ return unless ( $self->SUPER::combine ( @$array ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SUPER::combine(@$array) 0 43 43
189  
190 43 ▶ return $enc ? $self->coder->encode( $enc, $self->string() ) : $self->string();
Branch: 1 branch
BranchTrueFalseTotal
$enc ? : 35 8 43
191 }
192  
193  
194 # Internal
195  
196 12 ▶sub _load_coder_class {
Subroutine _load_coder_class: called CC=2 SCAR=7.1
197 12 my ( $class, $coder_class ) = @_;
198 12 (my $file = "$coder_class.pm") =~ s{::}{/}g;
199  
200 12 eval { require $file };
201  
202 12 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 12 12
203 0 Carp::croak $@;
204 }
205  
206 12 $coder_class;
207 }
208  
209  
210 # Accessors
211  
212 13 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
213 13 for my $method ( qw( encoding encoding_in encoding_out ) ) {
214 39 ▶ eval qq|
Subroutine encoding: called CC=2 SCAR=6.9
Subroutine encoding_in: called CC=2 SCAR=6.9
Subroutine encoding_out: called CC=2 SCAR=6.9
Pod documented
Pod documented
Pod documented
Branch: 3 branches
BranchTrueFalseTotal
if (@_ > 1) { } 10 188 198
if (@_ > 1) { } 51 134 185
if (@_ > 1) { } 41 74 115
215 sub $method {
216 my ( \$self, \$encoding ) = \@_;
217 if ( \@_ > 1 ) {
218 \$self->{ $method } = \$encoding;
219 return \$self;
220 }
221 else {
222 \$self->{ $method };
223 }
224 }
225 |;
226 }
227 }
228  
229  
230 13 *encoding_io_in = *encoding_to_parse = *encoding_in;
231 13 *encoding_io_out = *encoding_to_combine = *encoding_out;
232  
233  
234 207 ▶sub coder {
Subroutine coder: called CC=2 SCAR=7.1
Pod documented
235 207 my $self = shift;
236 207 ▶ $self->{coder} ||= $self->coder_class->new( automatic_UTF8 => $self->automatic_UTF8, @_ );
Condition66%2/3
$self->{"coder"} ||= $self->coder_class->new("automatic_UTF8", $self->automatic_UTF8, @_)
l!l&&r!l&&!r
197100
MC/DC0%0/2
$self->{"coder"} ||= $self->coder_class->new("automatic_UTF8", $self->automatic_UTF8, @_)
$self->{"coder"} $self->coder_class->new("automatic_UTF8", $self->automatic_UTF8, @_)
Truth table66%2/3
A ||= B
A= $self->{"coder"}B= $self->coder_class->new("automatic_UTF8", $self->automatic_UTF8, @_)
ABresult
1X1
011
000
237 }
238  
239  
240 24 ▶sub coder_class {
Subroutine coder_class: called CC=2 SCAR=6.9
Pod documented
241 24 my ( $self, $coder_class ) = @_;
242  
243 24 ▶ return $self->{coder_class} if ( @_ == 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 12 12 24
244  
245 12 $self->_load_coder_class( $coder_class );
246 12 $self->{coder_class} = $coder_class;
247 12 $self;
248 }
249  
250  
251 13 1;
252 __END__
↑ Summary Encoded/Coder/EncodeGuess.pm »