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 SCAR 37.8
CC
43
Cov
93%
encode
14.2
init
8.0
decode
7.0
CRAP 43.7
« Message/Body/Construct.pm ↑ Summary Box/Parser.pm »
1 # Copyrights 2001-2023 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 24 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 $VERSION = '3.013';
12  
13 24 ▶use base 'Mail::Reporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 24 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 24 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 24 ▶use Carp 'cluck';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 use overload
24 417 ▶ '""' => sub {shift->value}
Subroutine __ANON__: called CC=1 SCAR=0.0
25 203 ▶ , cmp => sub { my ($self, $other) = @_;
Subroutine __ANON__: called CC=3 SCAR=15.4
26 203 ▶ UNIVERSAL::isa($other, 'Mail::Message::Field')
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($other, 'Mail::Message::Field') ? : 0 203 203
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 24 ▶ , fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32  
33  
34 sub new($$@)
35 133 ▶{ my ($class, $attr) = (shift, shift);
Subroutine new: called CC=2 SCAR=6.9
Pod documented
36 133 ▶ my $value = @_ % 2 == 1 ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 1 ? : 48 85 133
37 133 $class->SUPER::new(attr => $attr, value => $value, @_);
38 }
39  
40 sub init($$)
41 133 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=8 SCAR=20.8
Pod undocumented
42 133 $self->SUPER::init($args);
43  
44 133 my ($attr, $value, $cont) = @$args{ qw/attr value use_continuations/ };
45  
46 133 ▶ my $name = ($attr =~ m/^(.*?)(?:\*\d+)?\*?\s*\=\s*/ ? $1 : $attr);
Branch: 1 branch
BranchTrueFalseTotal
$attr =~ /^(.*?)(?:\*\d+)?\*?\s*\=\s*/ ? : 77 56 133
47 133 ▶ $self->log(WARNING => "Illegal character in parameter name '$name'.")
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[!#-'*+\-.0-9A-Z^-~]+$/ 0 133 133
48 if $name !~ m/^[!#-'*+\-.0-9A-Z^-~]+$/;
49  
50 133 $self->{MMFF_name} = $name;
51 133 ▶ $self->{MMFF_usecont} = defined $cont ? $cont : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $cont ? : 6 127 133
52 133 ▶ $self->{MMFF_charset} = $args->{charset} if defined $args->{charset};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"charset"} 3 130 133
53 133 ▶ $self->{MMFF_language} = $args->{language} if defined $args->{language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"language"} 1 132 133
54  
55 133 ▶ $self->value(defined $value ? "$value" : ''); # enforce stringification
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 48 85 133
56 133 ▶ $self->addComponent($attr) if $attr ne $name;
Branch: 1 branch
BranchTrueFalseTotal
if $attr ne $name 77 56 133
57  
58 133 $self;
59 }
60  
61 #------------------------------------------
62  
63  
64 79 ▶sub name() { shift->{MMFF_name} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
65  
66  
67 sub value(;$)
68 791 ▶{ my $self = shift;
Subroutine value: called CC=3 SCAR=11.0
Pod documented
69 791 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 141 650 791
70 141 { delete $self->{MMFF_cont};
71 141 return $self->{MMFF_value} = shift;
72 }
73 650 ▶ exists $self->{MMFF_value} ? $self->{MMFF_value} : $self->decode;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'MMFF_value'} ? : 560 90 650
74 }
75  
76  
77 sub addComponent($)
78 91 ▶{ my ($self, $component) = @_;
Subroutine addComponent: called CC=6 SCAR=17.9
Pod documented
79 91 delete $self->{MMFF_value};
80  
81 91 my ($name, $value) = split /\=/, $component, 2;
82 91 ▶ if( substr($name, -1) eq '*' && $value =~ m/^([^']*)\'([^']*)\'/ )
Branch: 1 branch
BranchTrueFalseTotal
if (substr($name, -1) eq "*" and $value =~ /^([^']*)\'([^']*)\'/) 6 85 91
Condition100%3/3
substr($name, -1) eq "*" and $value =~ /^([^']*)\'([^']*)\'/
!ll&&!rl&&r
8146
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 91 ▶ if( $name =~ m/\*([0-9]+)\*?$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\*([0-9]+)\*?$/) { } 8 83 91
88 8 { $self->{MMFF_cont}[$1] = $component }
89 83 else { $self->{MMFF_cont} = [ $component ] }
90  
91 91 $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 66 ▶{ my $self = shift;
Subroutine string: called CC=4 SCAR=14.0
Pod documented
103 66 ▶ my $cont = $self->{MMFF_cont} || $self->encode;
Condition66%2/3
$self->{'MMFF_cont'} || $self->encode
l!l&&r!l&&!r
15510
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 66 ▶ return @$cont if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 57 9 66
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 51 ▶{ my $self = shift;
Subroutine encode: called CC=14 SCAR=26.5
Pod documented
116 51 my $value = $self->{MMFF_value};
117  
118 51 my @lines;
119 51 my ($pre, $encoded);
120  
121 51 ▶ my $charset = $self->{MMFF_charset} || '';
Condition100%2/2
$self->{'MMFF_charset'} || ''
l!l
645
MC/DC100%1/1
$self->{'MMFF_charset'} || ''
$self->{'MMFF_charset'}
Truth table100%2/2
A
A= $self->{'MMFF_charset'}
Aresult
11
00
122 51 ▶ my $lang = $self->{MMFF_language} || '';
Condition100%2/2
$self->{'MMFF_language'} || ''
l!l
150
MC/DC100%1/1
$self->{'MMFF_language'} || ''
$self->{'MMFF_language'}
Truth table100%2/2
A
A= $self->{'MMFF_language'}
Aresult
11
00
123 51 my $name = $self->{MMFF_name};
124 51 my $cont = $self->{MMFF_usecont};
125  
126 51 ▶ if($charset || $lang)
Branch: 2 branches
BranchTrueFalseTotal
if ($charset or $lang) { } 6 45 51
elsif (grep /[^\x20-\x7E]/, $value) { } 0 45 45
Condition66%2/3
$charset or $lang
l!l&&r!l&&!r
6045
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 45 { $pre = "$name*0=";
137 45 $value =~ s/"/\\"/g;
138 45 $encoded = 0;
139 }
140  
141 51 ▶ if($encoded)
Branch: 2 branches
BranchTrueFalseTotal
if ($encoded) { } 6 45 51
elsif ($cont) { } 45 0 45
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 45 while(1)
160 46 { push @lines, $pre.'"'. substr($value, 0, 75-length($pre), '') .'"';
161 46 ▶ last unless length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless length $value 45 1 46
162 1 $pre = $name . '*' . @lines . '=';
163 }
164
165 }
166 else
167 { # Single string only
168 0 push @lines, $pre . $value;
169 }
170  
171 51 ▶ $lines[0] =~ s/\*0// if @lines==1;
Branch: 1 branch
BranchTrueFalseTotal
if @lines == 1 49 2 51
172 51 $self->{MMFF_cont} = \@lines;
173 }
174  
175  
176 sub decode()
177 90 ▶{ my $self = shift;
Subroutine decode: called CC=7 SCAR=19.5
Pod documented
178 90 my $value = '';
179  
180 90 foreach my $cont ( @{$self->{MMFF_cont}} )
181 96 ▶ { unless(defined $cont)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cont) 1 95 96
182 1 { $value .= "[continuation missing]";
183 1 next;
184 }
185  
186 95 (my $name, local $_) = split /\=/, $cont, 2;
187  
188 95 ▶ if(substr($name, -1) eq '*')
Branch: 3 branches
BranchTrueFalseTotal
if (substr($name, -1) eq '*') { } 12 83 95
elsif (s/^\"(.*)\"$/$1/) { } 1 82 83
elsif (s/^\'(.*)\'$/$1/) { } 2 80 82
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 95 $value .= $_;
196 }
197  
198 90 my $charset = $self->{MMFF_charset};
199 90 ▶ $value = Encode::decode($charset, $value, 0) if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 5 85 90
200  
201 90 $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 24 1;
« Message/Body/Construct.pm ↑ Summary Box/Parser.pm »