Mail/Field.pm

filter: Statementstmt 16.3% 19 / 116 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 21.7% 5 / 23 Podpod 90.0% 9 / 10 totaltotal 15.0% 33 / 219 CC 32 SCAR 30.5
CC
32
Cov
11%
_require_dir
42.0
import
42.0
extract
42.0
CRAP 21.0
« Mail/Mailer/rfc822.pm ↑ Summary Mail/Mailer.pm »
1 # Copyrights 1995-2024 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 the bundle MailTools. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md for Copyright.
7 # Licensed under the same terms as Perl itself.
8  
9 package Mail::Field;{
10 1 our $VERSION = '2.22';
11 }
12  
13  
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Mail::Field::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 sub _header_pkg_name
21 0 ▶{ my $header = lc shift;
Subroutine _header_pkg_name: not called CC=3 SCAR=24.8
22 0 $header =~ s/((\b|_)\w)/\U$1/g;
23  
24 0 ▶ if(length($header) > 8)
Branch: 1 branch
BranchTrueFalseTotal
if (length $header > 8) { } 0 0 0
25 0 { my @header = split /[-_]+/, $header;
26 0 ▶ my $chars = int((7 + @header) / @header) || 1;
Condition0%0/2
int((7 + @header) / @header) || 1
l!l
00
MC/DC0%0/1
int((7 + @header) / @header) || 1
int((7 + @header) / @header)
Truth table0%0/2
A
A= int((7 + @header) / @header)
Aresult
11
00
27 0 $header = substr join('', map {substr $_,0,$chars} @header), 0, 8;
28 }
29 else
30 0 { $header =~ s/[-_]+//g;
31 }
32  
33 0 'Mail::Field::' . $header;
34 }
35  
36 sub _require_dir
37 0 ▶{ my($class, $dir, $dir_sep) = @_;
Subroutine _require_dir: not called CC=6 SCAR=37.4
38  
39 0 local *DIR;
40 0 ▶ opendir DIR, $dir
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or return 0 0 0
41 or return;
42  
43 0 my @inc;
44 0 foreach my $f (readdir DIR)
45 0 ▶ { $f =~ /^([\w\-]+)/ or next;
Branch: 1 branch
BranchTrueFalseTotal
$f =~ /^([\w\-]+)/ or next 0 0 0
46 0 my $p = $1;
47 0 my $n = "$dir$dir_sep$p";
48  
49 0 ▶ if(-d $n )
Branch: 1 branch
BranchTrueFalseTotal
if (-d $n) { } 0 0 0
50 0 { _require_dir("${class}::$f", $n, $dir_sep);
51 }
52 else
53 0 { $p =~ s/-/_/go;
54 0 eval "require ${class}::$p";
55  
56 # added next warning in 2.14, may be ignored for ancient code
57 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
58 }
59 }
60 0 closedir DIR;
61 }
62  
63 sub import
64 0 ▶{ my $class = shift;
Subroutine import: not called CC=6 SCAR=37.4
65  
66 0 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
67 0 { local $_;
68 eval "require " . _header_pkg_name($_) || die $@
69 0 ▶ for @_;
Branch: 1 branch
BranchTrueFalseTotal
eval 'require ' . _header_pkg_name($_) or die $@ 0 0 0
70 0 return;
71 }
72  
73 0 my ($dir, $dir_sep);
74 0 foreach my $f (grep defined $INC{$_}, keys %INC)
75 0 ▶ { next if $f !~ /^Mail(\W)Field\W/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $f =~ /^Mail(\W)Field\W/i 0 0 0
76 0 $dir_sep = $1;
77 # $dir = ($INC{$f} =~ /(.*Mail\W+Field)/i)[0] . $dir_sep;
78 0 ($dir = $INC{$f}) =~ s/(Mail\W+Field).*/$1$dir_sep/;
79 0 last;
80 }
81  
82 0 _require_dir('Mail::Field', $dir, $dir_sep);
83 }
84  
85 # register a header class, this creates a new method in Mail::Field
86 # which will call new on that class
87 sub register
88 0 ▶{ my $thing = shift;
Subroutine register: not called CC=5 SCAR=31.4
Pod undocumented
89 0 my $method = lc shift;
90 0 ▶ my $class = shift || ref($thing) || $thing;
Condition0%0/6
shift() || ref $thing || $thing
l!l&&r!l&&!r
000
shift() or ref $thing
l!l&&r!l&&!r
000
MC/DC0%0/3
shift() || ref $thing || $thing
shift() ref $thing $thing
Truth table0%0/4
A or B || C
A= shift()B= ref $thingC= $thing
ABCresult
1XX1
01X1
0011
0000
91  
92 0 $method =~ tr/-/_/;
93 0 ▶ $class = _header_pkg_name $method
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "Mail::Field" 0 0 0
94 if $class eq "Mail::Field";
95  
96 0 ▶ croak "Re-register of $method"
Branch: 1 branch
BranchTrueFalseTotal
if "Mail::Field"->can($method) 0 0 0
97 if Mail::Field->can($method);
98  
99 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
100 0 ▶ *{$method} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
101 0 shift;
102 0 ▶ $class->can('stringify') or eval "require $class" or die $@;
Branch: 1 branch
BranchTrueFalseTotal
$class->can("stringify") or eval "require $class" or die $@ 0 0 0
Condition0%0/3
$class->can("stringify") or eval "require $class"
l!l&&r!l&&!r
000
MC/DC0%0/2
$class->can("stringify") or eval "require $class"
$class->can("stringify") eval "require $class"
Truth table0%0/3
A or B
A= $class->can("stringify")B= eval "require $class"
ABresult
1X1
011
000
103 0 $class->_build(@_);
104 0 };
105 }
106  
107 # the *real* constructor
108 # if called with one argument then the `parse' method will be called
109 # otherwise the `create' method is called
110  
111 sub _build
112 0 ▶{ my $self = bless {}, shift;
Subroutine _build: not called CC=2 SCAR=17.9
113 0 ▶ @_==1 ? $self->parse(@_) : $self->create(@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
114 }
115  
116 #-------------
117  
118 sub new
119 0 ▶{ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
120 0 my $field = lc shift;
121 0 $field =~ tr/-/_/;
122 0 $class->$field(@_);
123 }
124  
125  
126 0 ▶sub combine {confess "Combine not implemented" }
Subroutine combine: not called CC=1 SCAR=6.9
Pod documented
127  
128 1 our $AUTOLOAD;
129 sub AUTOLOAD
130 0 ▶{ my $method = $AUTOLOAD;
Subroutine AUTOLOAD: not called CC=5 SCAR=31.2
131 0 $method =~ s/.*:://;
132  
133 0 ▶ $method =~ /^[^A-Z\x00-\x1f\x80-\xff :]+$/
Branch: 1 branch
BranchTrueFalseTotal
$method =~ /^[^A-Z\x00-\x1f\x80-\xff :]+$/ or croak("Undefined subroutine &$Mail::Field::AUTOLOAD called") 0 0 0
134 or croak "Undefined subroutine &$AUTOLOAD called";
135  
136 0 my $class = _header_pkg_name $method;
137  
138 0 ▶ unless(eval "require $class")
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "require $class") 0 0 0
139 0 { my $tag = $method;
140 0 $tag =~ s/_/-/g;
141 $tag = join '-',
142 0 ▶ map { /^[b-df-hj-np-tv-z]+$|^MIME$/i ? uc($_) : ucfirst(lc $_) }
Branch: 1 branch
BranchTrueFalseTotal
/^[b-df-hj-np-tv-z]+$|^MIME$/i ? : 0 0 0
143 split /\-/, $tag;
144  
145 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
146 0 @{"${class}::ISA"} = qw(Mail::Field::Generic);
147 0 ▶ *{"${class}::tag"} = sub { $tag };
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 }
149  
150 0 ▶ Mail::Field->can($method)
Branch: 1 branch
BranchTrueFalseTotal
"Mail::Field"->can($method) or $class->register($method) 0 0 0
151 or $class->register($method);
152  
153 0 goto &$AUTOLOAD;
154 }
155  
156  
157 # Of course, the functionality should have been in the Mail::Header class
158 sub extract
159 0 ▶{ my ($class, $tag, $head) = (shift, shift, shift);
Subroutine extract: not called CC=6 SCAR=37.4
Pod documented
160  
161 0 my $method = lc $tag;
162 0 $method =~ tr/-/_/;
163  
164 0 ▶ if(@_==0 && wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0 and wantarray) 0 0 0
Condition0%0/3
@_ == 0 and wantarray
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 0 and wantarray
@_ == 0 wantarray
Truth table0%0/3
A and B
A= @_ == 0B= wantarray
ABresult
0X0
100
111
165 0 { my @ret;
166 my $text; # need real copy!
167 0 foreach $text ($head->get($tag))
168 0 { chomp $text;
169 0 push @ret, $class->$method($text);
170 }
171 0 return @ret;
172 }
173  
174 0 ▶ my $idx = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
175 0 ▶ my $text = $head->get($tag,$idx)
Branch: 1 branch
BranchTrueFalseTotal
my $text = $head->get($tag, $idx) or return undef 0 0 0
176 or return undef;
177  
178 0 chomp $text;
179 0 $class->$method($text);
180 }
181  
182 #-------------
183  
184 # before 2.00, this method could be called as class method, however
185 # not all extensions supported that.
186 sub create
187 0 ▶{ my ($self, %arg) = @_;
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
188 0 %$self = ();
189 0 $self->set(\%arg);
190 }
191  
192  
193 # before 2.00, this method could be called as class method, however
194 # not all extensions supported that.
195 sub parse
196 0 ▶{ my $class = ref shift;
Subroutine parse: not called CC=1 SCAR=6.9
Pod documented
197 0 confess "parse() not implemented";
198 }
199  
200 #-------------
201  
202 0 ▶sub stringify { confess "stringify() not implemented" }
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
203  
204  
205 sub tag
206 0 ▶{ my $thing = shift;
Subroutine tag: not called CC=3 SCAR=24.8
Pod documented
207 0 ▶ my $tag = ref($thing) || $thing;
Condition0%0/3
ref $thing || $thing
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $thing || $thing
ref $thing $thing
Truth table0%0/3
A || B
A= ref $thingB= $thing
ABresult
1X1
011
000
208 0 $tag =~ s/.*:://;
209 0 $tag =~ s/_/-/g;
210  
211 join '-',
212 0 ▶ map { /^[b-df-hj-np-tv-z]+$|^MIME$/i ? uc($_) : ucfirst(lc $_) }
Branch: 1 branch
BranchTrueFalseTotal
/^[b-df-hj-np-tv-z]+$|^MIME$/i ? : 0 0 0
213 split /\-/, $tag;
214 }
215  
216  
217 0 ▶sub set(@) { confess "set() not implemented" }
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
218  
219 # prevent the calling of AUTOLOAD for DESTROY :-)
220 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
221  
222 #-------------
223  
224 sub text
225 0 ▶{ my $self = shift;
Subroutine text: not called CC=2 SCAR=17.9
Pod documented
226 0 ▶ @_ ? $self->parse(@_) : $self->stringify;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
227 }
228  
229 #-------------
230  
231 1 1;
« Mail/Mailer/rfc822.pm ↑ Summary Mail/Mailer.pm »