Message/Field/Attribute.pm

filter: Statementstmt 97.6% 122 / 125 Branchbran 89.3% 59 / 66 Conditioncond 73.6% 14 / 19 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 100% 21 / 21 Podpod 90.9% 10 / 11 totaltotal 91.7% 233 / 254 CC 43 SCAR 19.0
CC
43
Cov
93%
encode
14.2
init
8.0
decode
7.0
CRAP 6.7
« Message/Field/Full.pm ↑ Summary Message/Convert/MimeEntity.pm »
1 # Copyrights 2001-2022 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.03.
5 # This code is part of distribution Mail-Message. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package Mail::Message::Field::Attribute;
10 23 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 $VERSION = '3.012';
12  
13 23 ▶use base 'Mail::Reporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 23 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 23 ▶use Carp 'cluck';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 use overload
24 302 ▶ '""' => sub {shift->value}
Subroutine __ANON__: called CC=1 SCAR=0.0
25 176 ▶ , cmp => sub { my ($self, $other) = @_;
Subroutine __ANON__: called CC=3 SCAR=15.4
26 176 ▶ UNIVERSAL::isa($other, 'Mail::Message::Field')
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($other, 'Mail::Message::Field') ? : 0 176 176
Condition0%0/3
lc($_[0])->name cmp lc $_[1]->name or $_[0]->value cmp $_[1]->value
l!l&&r!l&&!r
000
MC/DC0%0/2
lc($_[0])->name cmp lc $_[1]->name or $_[0]->value cmp $_[1]->value
lc($_[0])->name cmp lc $_[1]->name $_[0]->value cmp $_[1]->value
Truth table0%0/3
A or B
A= lc($_[0])->name cmp lc $_[1]->nameB= $_[0]->value cmp $_[1]->value
ABresult
1X1
011
000
27 ? (lc($_[0])->name cmp lc($_[1]->name) || $_[0]->value cmp $_[1]->value)
28 : $_[0]->value cmp $_[1]
29 }
30 23 ▶ , fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32  
33  
34 sub new($$@)
35 125 ▶{ my ($class, $attr) = (shift, shift);
Subroutine new: called CC=2 SCAR=6.9
Pod documented
36 125 ▶ my $value = @_ % 2 == 1 ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 1 ? : 45 80 125
37 125 $class->SUPER::new(attr => $attr, value => $value, @_);
38 }
39  
40 sub init($$)
41 125 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=8 SCAR=20.8
Pod undocumented
42 125 $self->SUPER::init($args);
43  
44 125 my ($attr, $value, $cont) = @$args{ qw/attr value use_continuations/ };
45  
46 125 ▶ my $name = ($attr =~ m/^(.*?)(?:\*\d+)?\*?\s*\=\s*/ ? $1 : $attr);
Branch: 1 branch
BranchTrueFalseTotal
$attr =~ /^(.*?)(?:\*\d+)?\*?\s*\=\s*/ ? : 72 53 125
47 125 ▶ $self->log(WARNING => "Illegal character in parameter name '$name'.")
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[!#-'*+\-.0-9A-Z^-~]+$/ 0 125 125
48 if $name !~ m/^[!#-'*+\-.0-9A-Z^-~]+$/;
49  
50 125 $self->{MMFF_name} = $name;
51 125 ▶ $self->{MMFF_usecont} = defined $cont ? $cont : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $cont ? : 6 119 125
52 125 ▶ $self->{MMFF_charset} = $args->{charset} if defined $args->{charset};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"charset"} 3 122 125
53 125 ▶ $self->{MMFF_language} = $args->{language} if defined $args->{language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"language"} 1 124 125
54  
55 125 ▶ $self->value(defined $value ? "$value" : ''); # enforce stringification
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 45 80 125
56 125 ▶ $self->addComponent($attr) if $attr ne $name;
Branch: 1 branch
BranchTrueFalseTotal
if $attr ne $name 72 53 125
57  
58 125 $self;
59 }
60  
61 #------------------------------------------
62  
63  
64 74 ▶sub name() { shift->{MMFF_name} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
65  
66  
67 sub value(;$)
68 641 ▶{ my $self = shift;
Subroutine value: called CC=3 SCAR=11.0
Pod documented
69 641 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 133 508 641
70 133 { delete $self->{MMFF_cont};
71 133 return $self->{MMFF_value} = shift;
72 }
73 508 ▶ exists $self->{MMFF_value} ? $self->{MMFF_value} : $self->decode;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'MMFF_value'} ? : 423 85 508
74 }
75  
76  
77 sub addComponent($)
78 86 ▶{ my ($self, $component) = @_;
Subroutine addComponent: called CC=6 SCAR=17.9
Pod documented
79 86 delete $self->{MMFF_value};
80  
81 86 my ($name, $value) = split /\=/, $component, 2;
82 86 ▶ if( substr($name, -1) eq '*' && $value =~ m/^([^']*)\'([^']*)\'/ )
Branch: 1 branch
BranchTrueFalseTotal
if (substr($name, -1) eq "*" and $value =~ /^([^']*)\'([^']*)\'/) 6 80 86
Condition100%3/3
substr($name, -1) eq "*" and $value =~ /^([^']*)\'([^']*)\'/
!ll&&!rl&&r
7646
MC/DC100%2/2
substr($name, -1) eq "*" and $value =~ /^([^']*)\'([^']*)\'/
substr($name, -1) eq "*" $value =~ /^([^']*)\'([^']*)\'/
Truth table100%3/3
A and B
A= substr($name, -1) eq "*"B= $value =~ /^([^']*)\'([^']*)\'/
ABresult
0X0
100
111
83 6 ▶ { $self->{MMFF_charset} = length $1 ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $1 ? : 4 2 6
84 6 ▶ $self->{MMFF_language} = length $2 ? $2 : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $2 ? : 2 4 6
85 }
86  
87 86 ▶ if( $name =~ m/\*([0-9]+)\*?$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\*([0-9]+)\*?$/) { } 8 78 86
88 8 { $self->{MMFF_cont}[$1] = $component }
89 78 else { $self->{MMFF_cont} = [ $component ] }
90  
91 86 $component;
92 }
93  
94  
95 7 ▶sub charset() { shift->{MMFF_charset} }
Subroutine charset: called CC=1 SCAR=0.0
Pod documented
96  
97  
98 7 ▶sub language() { shift->{MMFF_language} }
Subroutine language: called CC=1 SCAR=0.0
Pod documented
99  
100  
101 sub string()
102 63 ▶{ my $self = shift;
Subroutine string: called CC=4 SCAR=14.0
Pod documented
103 63 ▶ my $cont = $self->{MMFF_cont} || $self->encode;
Condition66%2/3
$self->{'MMFF_cont'} || $self->encode
l!l&&r!l&&!r
15480
MC/DC0%0/2
$self->{'MMFF_cont'} || $self->encode
$self->{'MMFF_cont'} $self->encode
Truth table66%2/3
A || B
A= $self->{'MMFF_cont'}B= $self->encode
ABresult
1X1
011
000
104 63 ▶ return @$cont if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 54 9 63
105 9 ▶ return [] unless @$cont;
Branch: 1 branch
BranchTrueFalseTotal
unless @$cont 0 9 9
106  
107 9 local $" = "; ";
108 9 "; @$cont";
109 }
110  
111 #------------------------------------------
112  
113  
114 sub encode()
115 48 ▶{ my $self = shift;
Subroutine encode: called CC=14 SCAR=26.5
Pod documented
116 48 my $value = $self->{MMFF_value};
117  
118 48 my @lines;
119 48 my ($pre, $encoded);
120  
121 48 ▶ my $charset = $self->{MMFF_charset} || '';
Condition100%2/2
$self->{'MMFF_charset'} || ''
l!l
642
MC/DC100%1/1
$self->{'MMFF_charset'} || ''
$self->{'MMFF_charset'}
Truth table100%2/2
A
A= $self->{'MMFF_charset'}
Aresult
11
00
122 48 ▶ my $lang = $self->{MMFF_language} || '';
Condition100%2/2
$self->{'MMFF_language'} || ''
l!l
147
MC/DC100%1/1
$self->{'MMFF_language'} || ''
$self->{'MMFF_language'}
Truth table100%2/2
A
A= $self->{'MMFF_language'}
Aresult
11
00
123 48 my $name = $self->{MMFF_name};
124 48 my $cont = $self->{MMFF_usecont};
125  
126 48 ▶ if($charset || $lang)
Branch: 2 branches
BranchTrueFalseTotal
if ($charset or $lang) { } 6 42 48
elsif (grep /[^\x20-\x7E]/, $value) { } 0 42 42
Condition66%2/3
$charset or $lang
l!l&&r!l&&!r
6042
MC/DC50%1/2
$charset or $lang
$charset $lang
Truth table66%2/3
A or B
A= $charsetB= $lang
ABresult
1X1
011
000
127 6 { $pre = "$name*0*=$charset'$lang'";
128 6 $value = Encode::encode($charset, $value, 0);
129 6 $encoded = 1;
130 }
131 elsif(grep m/[^\x20-\x7E]/, $value)
132 0 { $pre = "$name*0*=''";
133 0 $encoded = 1;
134 }
135 else
136 42 { $pre = "$name*0=";
137 42 $value =~ s/"/\\"/g;
138 42 $encoded = 0;
139 }
140  
141 48 ▶ if($encoded)
Branch: 2 branches
BranchTrueFalseTotal
if ($encoded) { } 6 42 48
elsif ($cont) { } 42 0 42
142 { # Use encoding
143 6 my @c = split //, $value;
144 6 ▶ while(@c)
Branch: 1 branch
BranchTrueFalseTotal
if (@c) 169 6 175
145 169 { my $c = shift @c;
146 169 ▶ $c = '%'. sprintf "%02X", ord $c
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /[a-zA-Z0-9]/ 66 103 169
147 unless $c =~ m/[a-zA-Z0-9]/;
148  
149 169 ▶ if($cont && length($pre) + length($c)> 76)
Branch: 1 branch
BranchTrueFalseTotal
if ($cont and length($pre) + length($c) > 76) { } 2 167 169
Condition100%3/3
$cont and length($pre) + length($c) > 76
!ll&&!rl&&r
83842
MC/DC100%2/2
$cont and length($pre) + length($c) > 76
$cont length($pre) + length($c) > 76
Truth table100%3/3
A and B
A= $contB= length($pre) + length($c) > 76
ABresult
0X0
100
111
150 2 { push @lines, $pre;
151 2 $pre = $name . '*' . @lines . '*=' . $c;
152 }
153 167 else { $pre .= $c }
154 }
155 6 push @lines, $pre;
156 }
157 elsif($cont)
158 { # Simple string, but with continuations
159 42 while(1)
160 43 { push @lines, $pre.'"'. substr($value, 0, 75-length($pre), '') .'"';
161 43 ▶ last unless length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless length $value 42 1 43
162 1 $pre = $name . '*' . @lines . '=';
163 }
164
165 }
166 else
167 { # Single string only
168 0 push @lines, $pre . $value;
169 }
170  
171 48 ▶ $lines[0] =~ s/\*0// if @lines==1;
Branch: 1 branch
BranchTrueFalseTotal
if @lines == 1 46 2 48
172 48 $self->{MMFF_cont} = \@lines;
173 }
174  
175  
176 sub decode()
177 85 ▶{ my $self = shift;
Subroutine decode: called CC=7 SCAR=19.5
Pod documented
178 85 my $value = '';
179  
180 85 foreach my $cont ( @{$self->{MMFF_cont}} )
181 91 ▶ { unless(defined $cont)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cont) 1 90 91
182 1 { $value .= "[continuation missing]";
183 1 next;
184 }
185  
186 90 (my $name, local $_) = split /\=/, $cont, 2;
187  
188 90 ▶ if(substr($name, -1) eq '*')
Branch: 3 branches
BranchTrueFalseTotal
if (substr($name, -1) eq '*') { } 12 78 90
elsif (s/^\"(.*)\"$/$1/) { } 1 77 78
elsif (s/^\'(.*)\'$/$1/) { } 2 75 77
189 12 { s/^[^']*\'[^']*\'//;
190 12 s/\%([a-fA-F0-9]{2})/chr hex $1/ge;
191 }
192 1 elsif( s/^\"(.*)\"$/$1/ ) { s/\\\"/"/g }
193 2 elsif( s/^\'(.*)\'$/$1/ ) { s/\\\'/'/g }
194  
195 90 $value .= $_;
196 }
197  
198 85 my $charset = $self->{MMFF_charset};
199 85 ▶ $value = Encode::decode($charset, $value, 0) if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 5 80 85
200  
201 85 $self->{MMFF_value} = $value;
202 }
203  
204 #------------------------------------------
205  
206  
207 sub mergeComponent($)
208 1 ▶{ my ($self, $comp) = @_;
Subroutine mergeComponent: called CC=4 SCAR=14.3
Pod documented
209 my $cont = $self->{MMFF_cont}
210 1 ▶ or croak "ERROR: Too late to merge: value already changed.";
Branch: 1 branch
BranchTrueFalseTotal
my $cont = $self->{"MMFF_cont"} or croak("ERROR: Too late to merge: value already changed.") 0 1 1
211  
212 defined $_ && $self->addComponent($_)
213 1 ▶ foreach @{$comp->{MMFF_cont}};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $self->addComponent($_) 1 0 1
214  
215 1 $self;
216 }
217  
218 23 1;
« Message/Field/Full.pm ↑ Summary Message/Convert/MimeEntity.pm »