Catmandu/Exporter/Text.pm

filter: Statementstmt 92.7% 51 / 55 Branchbran 61.1% 11 / 18 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 4 totaltotal 78.2% 79 / 101 CC 14 SCAR 14.7
CC
14
Cov
82%
hash_text
8.1
array_text
6.9
unbackslash
2.0
CRAP 4.4
« Catmandu/Iterable.pm ↑ Summary Catmandu/Fix/Bind/visitor.pm »
1 package Catmandu::Exporter::Text;
2  
3 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.2025';
6  
7 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Catmandu::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 with 'Catmandu::Exporter';
12  
13 1 ▶use vars qw(%Interpolated );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # From String::Escape
16 # Earlier definitions are preferred to later ones, thus we output \n not \x0d
17 _define_backslash_escapes(
18 4 (map {$_ => $_} ('\\', '"', '$', '@')),
19 ('r' => "\r", 'n' => "\n", 't' => "\t"),
20 256 (map {'x' . unpack('H2', chr($_)) => chr($_)} (0 .. 255)),
21 1 (map {sprintf('%03o', $_) => chr($_)} (0 .. 255)),
22 );
23  
24 1 ▶sub _define_backslash_escapes {
Subroutine _define_backslash_escapes: called CC=1 SCAR=0.0
25 1 %Interpolated = @_;
26 }
27  
28 # $original_string = unbackslash( $special_characters_escaped );
29 4 ▶sub unbackslash ($) {
Subroutine unbackslash: called CC=2 SCAR=7.0
Pod undocumented
30 4 ▶ local $_ = (defined $_[0] ? $_[0] : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 4 0 4
31 4 s/ (\A|\G|[^\\]) [\\] ( [0][0-9][0-9] | [x][0-9a-fA-F]{2} | . ) / $1 . ( $Interpolated{lc($2) }) /gsxe;
32 4 return $_;
33 }
34  
35 # End from String::Escape
36  
37 has line_sep => (
38 is => 'ro',
39 1 ▶ default => sub {"\n"},
Subroutine __ANON__: called CC=1 SCAR=0.0
40 2 ▶ coerce => sub {unbackslash($_[0]);}
Subroutine __ANON__: called CC=1 SCAR=0.0
41 1 );
42 has field_sep => (
43 is => 'ro',
44 0 ▶ default => sub {undef},
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 2 ▶ coerce => sub {unbackslash($_[0])}
Subroutine __ANON__: called CC=1 SCAR=0.0
46 1 );
47  
48 6 ▶sub add {
Subroutine add: called CC=2 SCAR=7.0
Pod undocumented
49 6 my ($self, $data) = @_;
50 6 my $text = $self->hash_text('', $data);
51  
52 6 $self->fh->print($text);
53 6 ▶ $self->fh->print($self->line_sep) if defined $self->line_sep;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->line_sep 6 0 6
54 }
55  
56 6 ▶sub hash_text {
Subroutine hash_text: called CC=7 SCAR=21.0
Pod undocumented
57 6 my ($self, $text, $hash) = @_;
58  
59 6 for my $k (sort keys %$hash) {
60 6 ▶ next if ($k =~ /^_.*/);
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^_.*/ 0 6 6
61 6 my $item = $hash->{$k};
62  
63 6 ▶ $text .= $self->field_sep
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->field_sep and length $text 0 6 6
Condition33%1/3
defined $self->field_sep and length $text
!ll&&!rl&&r
060
MC/DC0%0/2
defined $self->field_sep and length $text
defined $self->field_sep length $text
Truth table33%1/3
A and B
A= defined $self->field_sepB= length $text
ABresult
0X0
100
111
64 if defined $self->field_sep && length($text);
65  
66 6 ▶ if (Catmandu::Util::is_array_ref($item)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Catmandu::Util::is_array_ref($item)) { } 2 4 6
elsif (Catmandu::Util::is_hash_ref($item)) { } 0 4 4
67 2 $text .= $self->array_text($text, $item);
68 }
69 elsif (Catmandu::Util::is_hash_ref($item)) {
70 0 $text .= $self->hash_text($text, $item);
71 }
72 else {
73 4 $text .= $item;
74 }
75 }
76  
77 6 return $text;
78 }
79  
80 2 ▶sub array_text {
Subroutine array_text: called CC=6 SCAR=19.3
Pod undocumented
81 2 my ($self, $text, $arr) = @_;
82  
83 2 for my $item (@$arr) {
84 4 ▶ $text .= $self->field_sep
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->field_sep and length $text 2 2 4
Condition66%2/3
defined $self->field_sep and length $text
!ll&&!rl&&r
022
MC/DC50%1/2
defined $self->field_sep and length $text
defined $self->field_sep length $text
Truth table66%2/3
A and B
A= defined $self->field_sepB= length $text
ABresult
0X0
100
111
85 if defined $self->field_sep && length($text);
86  
87 4 ▶ if (Catmandu::Util::is_array_ref($item)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Catmandu::Util::is_array_ref($item)) { } 0 4 4
elsif (Catmandu::Util::is_hash_ref($item)) { } 0 4 4
88 0 $text .= $self->array_text($text, $item);
89 }
90 elsif (Catmandu::Util::is_hash_ref($item)) {
91 0 $text .= $self->hash_text($text, $item);
92 }
93 else {
94 4 $text .= $item;
95 }
96 }
97  
98 2 return $text;
99 }
100  
101 1 1;
102  
103 __END__
« Catmandu/Iterable.pm ↑ Summary Catmandu/Fix/Bind/visitor.pm »