Abstract.pm

filter: Statementstmt 100% 78 / 78 Branchbran 100% 32 / 32 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 100% 136 / 136 CC 19 SCAR 14.5
CC
19
Cov
100%
__class_for
9.0
__ANON__
5.0
new
3.0
CRAP 4.3
« Abstract/MailInternet.pm ↑ Summary Abstract/MIMEEntity.pm »
1 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package Email::Abstract;
5 # ABSTRACT: unified interface to mail representations
6 3 $Email::Abstract::VERSION = '3.010';
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Email::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Module::Pluggable 1.5
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 search_path => [__PACKAGE__],
13 except => 'Email::Abstract::Plugin',
14 3 require => 1;
15  
16 3 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 my @plugins = __PACKAGE__->plugins(); # Requires them.
19 my %adapter_for =
20 9 map { $_->target => $_ }
21 grep {
22 3 my $avail = eval { $_->is_available };
23 15 ▶ $@ ? ($@ =~ /Can't locate object method "is_available"/) : $avail;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 3 12 15
24 }
25 @plugins;
26  
27 82 ▶sub object {
Subroutine object: called CC=2 SCAR=6.9
Pod documented
28 82 my ($self) = @_;
29 82 ▶ return unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 45 37 82
30 37 return $self->[0];
31 }
32  
33 13 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
34 13 my ($class, $foreign) = @_;
35  
36 13 ▶ return $foreign if eval { $foreign->isa($class) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $foreign->isa($class) } } 2 11 13
37  
38 11 ▶ $foreign = Email::Simple->new($foreign)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($foreign) 6 5 11
39 unless Scalar::Util::blessed($foreign);
40  
41 11 my $adapter = $class->__class_for($foreign); # dies if none available
42 9 return bless [ $foreign, $adapter ] => $class;
43 }
44  
45 61 ▶sub __class_for {
Subroutine __class_for: called CC=9 SCAR=22.0
46 61 my ($self, $foreign, $method, $skip_super) = @_;
47 61 ▶ $method ||= 'handle';
Condition100%2/2
$method ||= "handle"
l!l
457
MC/DC100%1/1
$method ||= "handle"
$method
Truth table100%2/2
A
A= $method
Aresult
11
00
48  
49 61 my $f_class = ref $foreign;
50 61 ▶ $f_class = $foreign unless $f_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $f_class 17 44 61
51  
52 61 ▶ return $f_class if ref $foreign and $f_class->isa($self);
Branch: 1 branch
BranchTrueFalseTotal
if ref $foreign and $f_class->isa($self) 12 49 61
Condition100%3/3
ref $foreign and $f_class->isa($self)
!ll&&!rl&&r
173212
MC/DC100%2/2
ref $foreign and $f_class->isa($self)
ref $foreign $f_class->isa($self)
Truth table100%3/3
A and B
A= ref $foreignB= $f_class->isa($self)
ABresult
0X0
100
111
53  
54 49 ▶ return $adapter_for{$f_class} if $adapter_for{$f_class};
Branch: 1 branch
BranchTrueFalseTotal
if $adapter_for{$f_class} 44 5 49
55  
56 5 ▶ if (not $skip_super) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skip_super) 4 1 5
57 4 my @bases = @{ mro::get_linear_isa($f_class) };
58 4 shift @bases;
59 4 for my $base (@bases) {
60 3 ▶ return $adapter_for{$base} if $adapter_for{$base};
Branch: 1 branch
BranchTrueFalseTotal
if $adapter_for{$base} 2 1 3
61 }
62 }
63  
64 3 Carp::croak "Don't know how to $method $f_class";
65 }
66  
67 70 ▶sub _adapter_obj_and_args {
Subroutine _adapter_obj_and_args: called CC=3 SCAR=11.0
68 70 my $self = shift;
69  
70 70 ▶ if (my $thing = $self->object) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $thing = $self->object) { } 25 45 70
71 25 return ($self->[1], $thing, @_);
72 } else {
73 45 my $thing = shift;
74 45 ▶ my $adapter = $self->__class_for(
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($thing) ? : 33 12 45
75 Scalar::Util::blessed($thing) ? $thing : 'Email::Simple'
76 );
77 45 return ($adapter, $thing, @_);
78 }
79 }
80  
81 3 for my $func (qw(get_header get_body set_header set_body as_string)) {
82 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
83 66 ▶ *$func = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
84 66 my $self = shift;
85 66 my ($adapter, $thing, @args) = $self->_adapter_obj_and_args(@_);
86  
87 # In the event of Email::Abstract->get_body($email_abstract), convert
88 # it into an object method call.
89 66 ▶ $thing = $thing->object if eval { $thing->isa($self) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $thing->isa($self) } } 12 54 66
90  
91 # I suppose we could work around this by leaving @_ intact and assigning to
92 # it. That seems ... not good. -- rjbs, 2007-07-18
93 66 ▶ unless (Scalar::Util::blessed($thing)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($thing)) 10 56 66
94 10 ▶ Carp::croak "can't alter string in place" if substr($func, 0, 3) eq 'set';
Branch: 1 branch
BranchTrueFalseTotal
if substr($func, 0, 3) eq "set" 4 6 10
95 $thing = Email::Simple->new(
96 6 ▶ ref $thing ? \do{my$str=$$thing} : $thing
Branch: 1 branch
BranchTrueFalseTotal
ref $thing ? : 3 3 6
97 );
98 }
99  
100 62 return $adapter->$func($thing, @args);
101 15 };
102 }
103  
104 4 ▶sub cast {
Subroutine cast: called CC=2 SCAR=6.9
Pod documented
105 4 my $self = shift;
106 4 my ($from_adapter, $from, $to) = $self->_adapter_obj_and_args(@_);
107  
108 4 my $adapter = $self->__class_for($to, 'construct', 1);
109  
110 3 ▶ my $from_string = ref($from) ? $from_adapter->as_string($from) : $from;
Branch: 1 branch
BranchTrueFalseTotal
ref $from ? : 1 2 3
111  
112 3 return $adapter->construct($from_string);
113 }
114  
115 3 1;
116  
117 =pod
118  
119 =encoding UTF-8
120  
121 =head1 NAME
122  
123 Email::Abstract - unified interface to mail representations
124  
125 =head1 VERSION
126  
127 version 3.010
128  
129 =head1 SYNOPSIS
130  
131 my $message = Mail::Message->read($rfc822)
132 || Email::Simple->new($rfc822)
133 || Mail::Internet->new([split /\n/, $rfc822])
134 || ...
135 || $rfc822;
136  
137 my $email = Email::Abstract->new($message);
138  
139 my $subject = $email->get_header("Subject");
140 $email->set_header(Subject => "My new subject");
141  
142 my $body = $email->get_body;
143  
144 $rfc822 = $email->as_string;
145  
146 my $mail_message = $email->cast("Mail::Message");
147  
148 =head1 DESCRIPTION
149  
150 C<Email::Abstract> provides module writers with the ability to write
151 simple, representation-independent mail handling code. For instance, in the
152 cases of C<Mail::Thread> or C<Mail::ListDetector>, a key part of the code
153 involves reading the headers from a mail object. Where previously one would
154 either have to specify the mail class required, or to build a new object from
155 scratch, C<Email::Abstract> can be used to perform certain simple operations on
156 an object regardless of its underlying representation.
157  
158 C<Email::Abstract> currently supports C<Mail::Internet>, C<MIME::Entity>,
159 C<Mail::Message>, C<Email::Simple>, C<Email::MIME>, and C<Courriel>. Other
160 representations are encouraged to create their own C<Email::Abstract::*> class
161 by copying C<Email::Abstract::EmailSimple>. All modules installed under the
162 C<Email::Abstract> hierarchy will be automatically picked up and used.
163  
164 =head1 PERL VERSION
165  
166 This library should run on perls released even a long time ago. It should
167 work on any version of perl released in the last five years.
168  
169 Although it may work on older versions of perl, no guarantee is made that the
170 minimum required version will not be increased. The version may be increased
171 for any reason, and there is no promise that patches will be accepted to
172 lower the minimum required perl.
173  
174 =head1 METHODS
175  
176 All of these methods may be called either as object methods or as class
177 methods. When called as class methods, the email object (of any class
178 supported by Email::Abstract) must be prepended to the list of arguments, like
179 so:
180  
181 my $return = Email::Abstract->method($message, @args);
182  
183 This is provided primarily for backwards compatibility.
184  
185 =head2 new
186  
187 my $email = Email::Abstract->new($message);
188  
189 Given a message, either as a string or as an object for which an adapter is
190 installed, this method will return a Email::Abstract object wrapping the
191 message.
192  
193 If the message is given as a string, it will be used to construct an object,
194 which will then be wrapped.
195  
196 =head2 get_header
197  
198 my $header = $email->get_header($header_name);
199  
200 my @headers = $email->get_header($header_name);
201  
202 This returns the values for the given header. In scalar context, it returns
203 the first value.
204  
205 =head2 set_header
206  
207 $email->set_header($header => @values);
208  
209 This sets the C<$header> header to the given one or more values.
210  
211 =head2 get_body
212  
213 my $body = $email->get_body;
214  
215 This returns the body as a string.
216  
217 =head2 set_body
218  
219 $email->set_body($string);
220  
221 This changes the body of the email to the given string.
222  
223 B<WARNING!> You probably don't want to call this method, despite what you may
224 think. Email message bodies are complicated, and rely on things like content
225 type, encoding, and various MIME requirements. If you call C<set_body> on a
226 message more complicated than a single-part seven-bit plain-text message, you
227 are likely to break something. If you need to do this sort of thing, you
228 should probably use a specific message class from end to end.
229  
230 This method is left in place for backwards compatibility.
231  
232 =head2 as_string
233  
234 my $string = $email->as_string;
235  
236 This returns the whole email as a decoded string.
237  
238 =head2 cast
239  
240 my $mime_entity = $email->cast('MIME::Entity');
241  
242 This method will convert a message from one message class to another. It will
243 throw an exception if no adapter for the target class is known, or if the
244 adapter does not provide a C<construct> method.
245  
246 =head2 object
247  
248 my $message = $email->object;
249  
250 This method returns the message object wrapped by Email::Abstract. If called
251 as a class method, it returns false.
252  
253 Note that, because strings are converted to message objects before wrapping,
254 this method will return an object when the Email::Abstract was constructed from
255 a string.
256  
257 =head1 AUTHORS
258  
259 =over 4
260  
261 =item *
262  
263 Ricardo SIGNES <cpan@semiotic.systems>
264  
265 =item *
266  
267 Simon Cozens <simon@cpan.org>
268  
269 =item *
270  
271 Casey West <casey@geeknest.com>
272  
273 =back
274  
275 =head1 CONTRIBUTORS
276  
277 =for stopwords Dave Rolsky Ricardo Signes William Yardley
278  
279 =over 4
280  
281 =item *
282  
283 Dave Rolsky <autarch@urth.org>
284  
285 =item *
286  
287 Ricardo Signes <rjbs@semiotic.systems>
288  
289 =item *
290  
291 William Yardley <pep@veggiechinese.net>
292  
293 =back
294  
295 =head1 COPYRIGHT AND LICENSE
296  
297 This software is copyright (c) 2004 by Simon Cozens.
298  
299 This is free software; you can redistribute it and/or modify it under
300 the same terms as the Perl 5 programming language system itself.
301  
302 =cut
303  
304 __END__
« Abstract/MailInternet.pm ↑ Summary Abstract/MIMEEntity.pm »