| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
10
|
|
|
10
|
|
415596
|
use v5.12.0; |
|
|
10
|
|
|
|
|
106
|
|
|
2
|
10
|
|
|
10
|
|
52
|
use warnings; |
|
|
10
|
|
|
|
|
20
|
|
|
|
10
|
|
|
|
|
11746
|
|
|
3
|
|
|
|
|
|
|
package Email::Address 1.913; |
|
4
|
|
|
|
|
|
|
# ABSTRACT: RFC 2822 Address Parsing and Creation |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $COMMENT_NEST_LEVEL ||= 1; |
|
7
|
|
|
|
|
|
|
our $STRINGIFY ||= 'format'; |
|
8
|
|
|
|
|
|
|
our $COLLAPSE_SPACES //= 1; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#pod =head1 SYNOPSIS |
|
11
|
|
|
|
|
|
|
#pod |
|
12
|
|
|
|
|
|
|
#pod use Email::Address; |
|
13
|
|
|
|
|
|
|
#pod |
|
14
|
|
|
|
|
|
|
#pod my @addresses = Email::Address->parse($line); |
|
15
|
|
|
|
|
|
|
#pod my $address = Email::Address->new(Casey => 'casey@localhost'); |
|
16
|
|
|
|
|
|
|
#pod |
|
17
|
|
|
|
|
|
|
#pod print $address->format; |
|
18
|
|
|
|
|
|
|
#pod |
|
19
|
|
|
|
|
|
|
#pod =head1 DESCRIPTION |
|
20
|
|
|
|
|
|
|
#pod |
|
21
|
|
|
|
|
|
|
#pod This class implements a regex-based RFC 2822 parser that locates email |
|
22
|
|
|
|
|
|
|
#pod addresses in strings and returns a list of C objects found. |
|
23
|
|
|
|
|
|
|
#pod Alternatively you may construct objects manually. The goal of this software is |
|
24
|
|
|
|
|
|
|
#pod to be correct, and very very fast. |
|
25
|
|
|
|
|
|
|
#pod |
|
26
|
|
|
|
|
|
|
#pod Version 1.909 and earlier of this module had vulnerabilies |
|
27
|
|
|
|
|
|
|
#pod (L) |
|
28
|
|
|
|
|
|
|
#pod and (L) |
|
29
|
|
|
|
|
|
|
#pod which allowed specially constructed email to cause a denial of service. The |
|
30
|
|
|
|
|
|
|
#pod reported vulnerabilities and some other pathalogical cases (meaning they really |
|
31
|
|
|
|
|
|
|
#pod shouldn't occur in normal email) have been addressed in version 1.910 and newer. |
|
32
|
|
|
|
|
|
|
#pod If you're running version 1.909 or older, you should update! |
|
33
|
|
|
|
|
|
|
#pod |
|
34
|
|
|
|
|
|
|
#pod Alternatively, you could switch to L|Email::Address::XS> |
|
35
|
|
|
|
|
|
|
#pod which has a backward compatible API. B |
|
36
|
|
|
|
|
|
|
#pod |
|
37
|
|
|
|
|
|
|
#pod =cut |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $CTL = q{\x00-\x1F\x7F}; |
|
40
|
|
|
|
|
|
|
my $special = q{()<>\\[\\]:;@\\\\,."}; |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $text = qr/[^\x0A\x0D]/; |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $quoted_pair = qr/\\$text/; |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $ctext = qr/(?>[^()\\]+)/; |
|
47
|
|
|
|
|
|
|
my ($ccontent, $comment) = (q{})x2; |
|
48
|
|
|
|
|
|
|
for (1 .. $COMMENT_NEST_LEVEL) { |
|
49
|
|
|
|
|
|
|
$ccontent = qr/$ctext|$quoted_pair|$comment/; |
|
50
|
|
|
|
|
|
|
$comment = qr/(?>\s*\((?:\s*$ccontent)*\s*\)\s*)/; |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
my $cfws = qr/$comment|(?>\s+)/; |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my $atext = qq/[^$CTL$special\\s]/; |
|
55
|
|
|
|
|
|
|
my $atom = qr/(?>$cfws*$atext+$cfws*)/; |
|
56
|
|
|
|
|
|
|
my $dot_atom_text = qr/(?>$atext+(?:\.$atext+)*)/; |
|
57
|
|
|
|
|
|
|
my $dot_atom = qr/(?>$cfws*$dot_atom_text$cfws*)/; |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my $qtext = qr/[^\\"]/; |
|
60
|
|
|
|
|
|
|
my $qcontent = qr/$qtext|$quoted_pair/; |
|
61
|
|
|
|
|
|
|
my $quoted_string = qr/(?>$cfws*"$qcontent*"$cfws*)/; |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my $word = qr/$atom|$quoted_string/; |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# XXX: This ($phrase) used to just be: my $phrase = qr/$word+/; It was changed |
|
66
|
|
|
|
|
|
|
# to resolve bug 22991, creating a significant slowdown. Given current speed |
|
67
|
|
|
|
|
|
|
# problems. Once 16320 is resolved, this section should be dealt with. |
|
68
|
|
|
|
|
|
|
# -- rjbs, 2006-11-11 |
|
69
|
|
|
|
|
|
|
#my $obs_phrase = qr/$word(?:$word|\.|$cfws)*/; |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# XXX: ...and the above solution caused endless problems (never returned) when |
|
72
|
|
|
|
|
|
|
# examining this address, now in a test: |
|
73
|
|
|
|
|
|
|
# admin+=E6=96=B0=E5=8A=A0=E5=9D=A1_Weblog-- ATAT --test.socialtext.com |
|
74
|
|
|
|
|
|
|
# So we disallow the hateful CFWS in this context for now. Of modern mail |
|
75
|
|
|
|
|
|
|
# agents, only Apple Web Mail 2.0 is known to produce obs-phrase. |
|
76
|
|
|
|
|
|
|
# -- rjbs, 2006-11-19 |
|
77
|
|
|
|
|
|
|
my $simple_word = qr/(?>$atom|\.|\s*"$qcontent+"\s*)/; |
|
78
|
|
|
|
|
|
|
my $obs_phrase = qr/(?>$simple_word+)/; |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
my $phrase = qr/$obs_phrase|(?>$word+)/; |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my $local_part = qr/$dot_atom|$quoted_string/; |
|
83
|
|
|
|
|
|
|
my $dtext = qr/[^\[\]\\]/; |
|
84
|
|
|
|
|
|
|
my $dcontent = qr/$dtext|$quoted_pair/; |
|
85
|
|
|
|
|
|
|
my $domain_literal = qr/(?>$cfws*\[(?:\s*$dcontent)*\s*\]$cfws*)/; |
|
86
|
|
|
|
|
|
|
my $domain = qr/$dot_atom|$domain_literal/; |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
my $display_name = $phrase; |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
#pod =head2 Package Variables |
|
91
|
|
|
|
|
|
|
#pod |
|
92
|
|
|
|
|
|
|
#pod B Email isn't easy (if even possible) to parse with a regex, I
|
|
93
|
|
|
|
|
|
|
#pod least> if you're on a C prior to 5.10.0. Providing regular expressions |
|
94
|
|
|
|
|
|
|
#pod for use by other programs isn't a great idea, because it makes it hard to |
|
95
|
|
|
|
|
|
|
#pod improve the parser without breaking the "it's a regex" feature. Using these |
|
96
|
|
|
|
|
|
|
#pod regular expressions is not encouraged, and methods like C<< |
|
97
|
|
|
|
|
|
|
#pod Email::Address->is_addr_spec >> should be provided in the future. |
|
98
|
|
|
|
|
|
|
#pod |
|
99
|
|
|
|
|
|
|
#pod Several regular expressions used in this package are useful to others. |
|
100
|
|
|
|
|
|
|
#pod For convenience, these variables are declared as package variables that |
|
101
|
|
|
|
|
|
|
#pod you may access from your program. |
|
102
|
|
|
|
|
|
|
#pod |
|
103
|
|
|
|
|
|
|
#pod These regular expressions conform to the rules specified in RFC 2822. |
|
104
|
|
|
|
|
|
|
#pod |
|
105
|
|
|
|
|
|
|
#pod You can access these variables using the full namespace. If you want |
|
106
|
|
|
|
|
|
|
#pod short names, define them yourself. |
|
107
|
|
|
|
|
|
|
#pod |
|
108
|
|
|
|
|
|
|
#pod my $addr_spec = $Email::Address::addr_spec; |
|
109
|
|
|
|
|
|
|
#pod |
|
110
|
|
|
|
|
|
|
#pod =over 4 |
|
111
|
|
|
|
|
|
|
#pod |
|
112
|
|
|
|
|
|
|
#pod =item $Email::Address::addr_spec |
|
113
|
|
|
|
|
|
|
#pod |
|
114
|
|
|
|
|
|
|
#pod This regular expression defined what an email address is allowed to |
|
115
|
|
|
|
|
|
|
#pod look like. |
|
116
|
|
|
|
|
|
|
#pod |
|
117
|
|
|
|
|
|
|
#pod =item $Email::Address::angle_addr |
|
118
|
|
|
|
|
|
|
#pod |
|
119
|
|
|
|
|
|
|
#pod This regular expression defines an C<$addr_spec> wrapped in angle |
|
120
|
|
|
|
|
|
|
#pod brackets. |
|
121
|
|
|
|
|
|
|
#pod |
|
122
|
|
|
|
|
|
|
#pod =item $Email::Address::name_addr |
|
123
|
|
|
|
|
|
|
#pod |
|
124
|
|
|
|
|
|
|
#pod This regular expression defines what an email address can look like |
|
125
|
|
|
|
|
|
|
#pod with an optional preceding display name, also known as the C. |
|
126
|
|
|
|
|
|
|
#pod |
|
127
|
|
|
|
|
|
|
#pod =item $Email::Address::mailbox |
|
128
|
|
|
|
|
|
|
#pod |
|
129
|
|
|
|
|
|
|
#pod This is the complete regular expression defining an RFC 2822 email |
|
130
|
|
|
|
|
|
|
#pod address with an optional preceding display name and optional |
|
131
|
|
|
|
|
|
|
#pod following comment. |
|
132
|
|
|
|
|
|
|
#pod |
|
133
|
|
|
|
|
|
|
#pod =back |
|
134
|
|
|
|
|
|
|
#pod |
|
135
|
|
|
|
|
|
|
#pod =cut |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
our $addr_spec = qr/$local_part\@$domain/; |
|
138
|
|
|
|
|
|
|
our $angle_addr = qr/(?>$cfws*<$addr_spec>$cfws*)/; |
|
139
|
|
|
|
|
|
|
our $name_addr = qr/(?>$display_name?)$angle_addr/; |
|
140
|
|
|
|
|
|
|
our $mailbox = qr/(?:$name_addr|$addr_spec)(?>$comment*)/; |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub _PHRASE () { 0 } |
|
143
|
|
|
|
|
|
|
sub _ADDRESS () { 1 } |
|
144
|
|
|
|
|
|
|
sub _COMMENT () { 2 } |
|
145
|
|
|
|
|
|
|
sub _ORIGINAL () { 3 } |
|
146
|
|
|
|
|
|
|
sub _IN_CACHE () { 4 } |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub __dump { |
|
149
|
|
|
|
|
|
|
return { |
|
150
|
0
|
|
|
0
|
|
0
|
phrase => $_[0][_PHRASE], |
|
151
|
|
|
|
|
|
|
address => $_[0][_ADDRESS], |
|
152
|
|
|
|
|
|
|
comment => $_[0][_COMMENT], |
|
153
|
|
|
|
|
|
|
original => $_[0][_ORIGINAL], |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
} |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
#pod =head2 Class Methods |
|
158
|
|
|
|
|
|
|
#pod |
|
159
|
|
|
|
|
|
|
#pod =over |
|
160
|
|
|
|
|
|
|
#pod |
|
161
|
|
|
|
|
|
|
#pod =item parse |
|
162
|
|
|
|
|
|
|
#pod |
|
163
|
|
|
|
|
|
|
#pod my @addrs = Email::Address->parse( |
|
164
|
|
|
|
|
|
|
#pod q[me@local, Casey , "Casey" (West)] |
|
165
|
|
|
|
|
|
|
#pod ); |
|
166
|
|
|
|
|
|
|
#pod |
|
167
|
|
|
|
|
|
|
#pod This method returns a list of C objects it finds in the input |
|
168
|
|
|
|
|
|
|
#pod string. B that it returns a list, and expects that it may find |
|
169
|
|
|
|
|
|
|
#pod multiple addresses. The behavior in scalar context is undefined. |
|
170
|
|
|
|
|
|
|
#pod |
|
171
|
|
|
|
|
|
|
#pod The specification for an email address allows for infinitely nestable comments. |
|
172
|
|
|
|
|
|
|
#pod That's nice in theory, but a little over done. By default this module allows |
|
173
|
|
|
|
|
|
|
#pod for one (C<1>) level of nested comments. If you think you need more, modify the |
|
174
|
|
|
|
|
|
|
#pod C<$Email::Address::COMMENT_NEST_LEVEL> package variable to allow more. |
|
175
|
|
|
|
|
|
|
#pod |
|
176
|
|
|
|
|
|
|
#pod $Email::Address::COMMENT_NEST_LEVEL = 10; # I'm deep |
|
177
|
|
|
|
|
|
|
#pod |
|
178
|
|
|
|
|
|
|
#pod The reason for this hardly-limiting limitation is simple: efficiency. |
|
179
|
|
|
|
|
|
|
#pod |
|
180
|
|
|
|
|
|
|
#pod Long strings of whitespace can be problematic for this module to parse, a bug |
|
181
|
|
|
|
|
|
|
#pod which has not yet been adequately addressed. The default behavior is now to |
|
182
|
|
|
|
|
|
|
#pod collapse multiple spaces into a single space, which avoids this problem. To |
|
183
|
|
|
|
|
|
|
#pod prevent this behavior, set C<$Email::Address::COLLAPSE_SPACES> to zero. This |
|
184
|
|
|
|
|
|
|
#pod variable will go away when the bug is resolved properly. |
|
185
|
|
|
|
|
|
|
#pod |
|
186
|
|
|
|
|
|
|
#pod In accordance with RFC 822 and its descendants, this module demands that email |
|
187
|
|
|
|
|
|
|
#pod addresses be ASCII only. Any non-ASCII content in the parsed addresses will |
|
188
|
|
|
|
|
|
|
#pod cause the parser to return no results. |
|
189
|
|
|
|
|
|
|
#pod |
|
190
|
|
|
|
|
|
|
#pod =cut |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
our (%PARSE_CACHE, %FORMAT_CACHE, %NAME_CACHE); |
|
193
|
|
|
|
|
|
|
my $NOCACHE; |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub __get_cached_parse { |
|
196
|
118
|
50
|
|
118
|
|
330
|
return if $NOCACHE; |
|
197
|
|
|
|
|
|
|
|
|
198
|
118
|
|
|
|
|
268
|
my ($class, $line) = @_; |
|
199
|
|
|
|
|
|
|
|
|
200
|
118
|
100
|
|
|
|
413
|
return @{$PARSE_CACHE{$line}} if exists $PARSE_CACHE{$line}; |
|
|
2
|
|
|
|
|
9
|
|
|
201
|
116
|
|
|
|
|
458
|
return; |
|
202
|
|
|
|
|
|
|
} |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub __cache_parse { |
|
205
|
116
|
50
|
|
116
|
|
325
|
return if $NOCACHE; |
|
206
|
|
|
|
|
|
|
|
|
207
|
116
|
|
|
|
|
259
|
my ($class, $line, $addrs) = @_; |
|
208
|
|
|
|
|
|
|
|
|
209
|
116
|
|
|
|
|
373
|
$PARSE_CACHE{$line} = $addrs; |
|
210
|
|
|
|
|
|
|
} |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
sub parse { |
|
213
|
119
|
|
|
119
|
1
|
408054
|
my ($class, $line) = @_; |
|
214
|
119
|
100
|
|
|
|
445
|
return unless $line; |
|
215
|
|
|
|
|
|
|
|
|
216
|
118
|
50
|
|
|
|
1161
|
$line =~ s/[ \t]+/ /g if $COLLAPSE_SPACES; |
|
217
|
|
|
|
|
|
|
|
|
218
|
118
|
100
|
|
|
|
546
|
if (my @cached = $class->__get_cached_parse($line)) { |
|
219
|
2
|
|
|
|
|
5
|
return @cached; |
|
220
|
|
|
|
|
|
|
} |
|
221
|
|
|
|
|
|
|
|
|
222
|
116
|
|
|
|
|
215
|
my %mailboxes; |
|
223
|
116
|
|
|
|
|
214
|
my $str = $line; |
|
224
|
116
|
100
|
|
|
|
8259
|
$str =~ s!($name_addr(?>$comment*))!$mailboxes{pos($str)} = $1; ',' x length $1!ego |
|
|
198
|
|
|
|
|
896
|
|
|
|
198
|
|
|
|
|
1824
|
|
|
225
|
|
|
|
|
|
|
if $str =~ /$angle_addr/; |
|
226
|
116
|
|
|
|
|
3883
|
$str =~ s!($addr_spec(?>$comment*))!$mailboxes{pos($str)} = $1; ',' x length $1!ego; |
|
|
46
|
|
|
|
|
179
|
|
|
|
46
|
|
|
|
|
276
|
|
|
227
|
116
|
|
|
|
|
696
|
my @mailboxes = map { $mailboxes{$_} } sort { $a <=> $b } keys %mailboxes; |
|
|
244
|
|
|
|
|
673
|
|
|
|
185
|
|
|
|
|
608
|
|
|
228
|
|
|
|
|
|
|
|
|
229
|
116
|
|
|
|
|
316
|
my @addrs; |
|
230
|
116
|
|
|
|
|
337
|
foreach (@mailboxes) { |
|
231
|
244
|
|
|
|
|
427
|
my $original = $_; |
|
232
|
|
|
|
|
|
|
|
|
233
|
244
|
|
|
|
|
863
|
my @comments = /($comment)/go; |
|
234
|
244
|
100
|
|
|
|
648
|
s/$comment//go if @comments; |
|
235
|
|
|
|
|
|
|
|
|
236
|
244
|
|
|
|
|
429
|
my ($user, $host, $com); |
|
237
|
244
|
100
|
|
|
|
3925
|
($user, $host) = ($1, $2) if s/<($local_part)\@($domain)>\s*\z//o; |
|
238
|
244
|
100
|
66
|
|
|
1102
|
if (! defined($user) || ! defined($host)) { |
|
239
|
46
|
|
|
|
|
1691
|
s/($local_part)\@($domain)//o; |
|
240
|
46
|
|
|
|
|
188
|
($user, $host) = ($1, $2); |
|
241
|
|
|
|
|
|
|
} |
|
242
|
|
|
|
|
|
|
|
|
243
|
10
|
100
|
|
10
|
|
5949
|
next if $user =~ /\P{ASCII}/; |
|
|
10
|
|
|
|
|
145
|
|
|
|
10
|
|
|
|
|
158
|
|
|
|
244
|
|
|
|
|
728
|
|
|
244
|
240
|
100
|
|
|
|
582
|
next if $host =~ /\P{ASCII}/; |
|
245
|
|
|
|
|
|
|
|
|
246
|
239
|
|
|
|
|
3291
|
my ($phrase) = /($display_name)/o; |
|
247
|
|
|
|
|
|
|
|
|
248
|
239
|
|
|
|
|
584
|
for ( $phrase, $host, $user, @comments ) { |
|
249
|
727
|
100
|
|
|
|
1316
|
next unless defined $_; |
|
250
|
651
|
|
|
|
|
1215
|
s/^\s+//; |
|
251
|
651
|
|
|
|
|
1537
|
s/\s+$//; |
|
252
|
651
|
50
|
|
|
|
1395
|
$_ = undef unless length $_; |
|
253
|
|
|
|
|
|
|
} |
|
254
|
|
|
|
|
|
|
|
|
255
|
239
|
100
|
|
|
|
570
|
$phrase =~ s/\\(.)/$1/g if $phrase; |
|
256
|
|
|
|
|
|
|
|
|
257
|
239
|
|
|
|
|
466
|
my $new_comment = join q{ }, @comments; |
|
258
|
239
|
|
|
|
|
926
|
push @addrs, |
|
259
|
|
|
|
|
|
|
$class->new($phrase, "$user\@$host", $new_comment, $original); |
|
260
|
239
|
|
|
|
|
1085
|
$addrs[-1]->[_IN_CACHE] = [ \$line, $#addrs ] |
|
261
|
|
|
|
|
|
|
} |
|
262
|
|
|
|
|
|
|
|
|
263
|
116
|
|
|
|
|
497
|
$class->__cache_parse($line, \@addrs); |
|
264
|
116
|
|
|
|
|
625
|
return @addrs; |
|
265
|
|
|
|
|
|
|
} |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
#pod =item new |
|
268
|
|
|
|
|
|
|
#pod |
|
269
|
|
|
|
|
|
|
#pod my $address = Email::Address->new(undef, 'casey@local'); |
|
270
|
|
|
|
|
|
|
#pod my $address = Email::Address->new('Casey West', 'casey@local'); |
|
271
|
|
|
|
|
|
|
#pod my $address = Email::Address->new(undef, 'casey@local', '(Casey)'); |
|
272
|
|
|
|
|
|
|
#pod |
|
273
|
|
|
|
|
|
|
#pod Constructs and returns a new C object. Takes four |
|
274
|
|
|
|
|
|
|
#pod positional arguments: phrase, email, and comment, and original string. |
|
275
|
|
|
|
|
|
|
#pod |
|
276
|
|
|
|
|
|
|
#pod The original string should only really be set using C. |
|
277
|
|
|
|
|
|
|
#pod |
|
278
|
|
|
|
|
|
|
#pod =cut |
|
279
|
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
sub new { |
|
281
|
693
|
|
|
693
|
1
|
324876
|
my ($class, $phrase, $email, $comment, $orig) = @_; |
|
282
|
693
|
100
|
|
|
|
2421
|
$phrase =~ s/\A"(.+)"\z/$1/ if $phrase; |
|
283
|
|
|
|
|
|
|
|
|
284
|
693
|
|
|
|
|
2636
|
bless [ $phrase, $email, $comment, $orig ] => $class; |
|
285
|
|
|
|
|
|
|
} |
|
286
|
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
#pod =item purge_cache |
|
288
|
|
|
|
|
|
|
#pod |
|
289
|
|
|
|
|
|
|
#pod Email::Address->purge_cache; |
|
290
|
|
|
|
|
|
|
#pod |
|
291
|
|
|
|
|
|
|
#pod One way this module stays fast is with internal caches. Caches live |
|
292
|
|
|
|
|
|
|
#pod in memory and there is the remote possibility that you will have a |
|
293
|
|
|
|
|
|
|
#pod memory problem. On the off chance that you think you're one of those |
|
294
|
|
|
|
|
|
|
#pod people, this class method will empty those caches. |
|
295
|
|
|
|
|
|
|
#pod |
|
296
|
|
|
|
|
|
|
#pod I've loaded over 12000 objects and not encountered a memory problem. |
|
297
|
|
|
|
|
|
|
#pod |
|
298
|
|
|
|
|
|
|
#pod =cut |
|
299
|
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
sub purge_cache { |
|
301
|
0
|
|
|
0
|
1
|
0
|
%NAME_CACHE = (); |
|
302
|
0
|
|
|
|
|
0
|
%FORMAT_CACHE = (); |
|
303
|
0
|
|
|
|
|
0
|
%PARSE_CACHE = (); |
|
304
|
|
|
|
|
|
|
} |
|
305
|
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
#pod =item disable_cache |
|
307
|
|
|
|
|
|
|
#pod |
|
308
|
|
|
|
|
|
|
#pod =item enable_cache |
|
309
|
|
|
|
|
|
|
#pod |
|
310
|
|
|
|
|
|
|
#pod Email::Address->disable_cache if memory_low(); |
|
311
|
|
|
|
|
|
|
#pod |
|
312
|
|
|
|
|
|
|
#pod If you'd rather not cache address parses at all, you can disable (and |
|
313
|
|
|
|
|
|
|
#pod re-enable) the Email::Address cache with these methods. The cache is enabled |
|
314
|
|
|
|
|
|
|
#pod by default. |
|
315
|
|
|
|
|
|
|
#pod |
|
316
|
|
|
|
|
|
|
#pod =cut |
|
317
|
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
sub disable_cache { |
|
319
|
0
|
|
|
0
|
1
|
0
|
my ($class) = @_; |
|
320
|
0
|
|
|
|
|
0
|
$class->purge_cache; |
|
321
|
0
|
|
|
|
|
0
|
$NOCACHE = 1; |
|
322
|
|
|
|
|
|
|
} |
|
323
|
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
sub enable_cache { |
|
325
|
0
|
|
|
0
|
1
|
0
|
$NOCACHE = undef; |
|
326
|
|
|
|
|
|
|
} |
|
327
|
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
#pod =back |
|
329
|
|
|
|
|
|
|
#pod |
|
330
|
|
|
|
|
|
|
#pod =head2 Instance Methods |
|
331
|
|
|
|
|
|
|
#pod |
|
332
|
|
|
|
|
|
|
#pod =over 4 |
|
333
|
|
|
|
|
|
|
#pod |
|
334
|
|
|
|
|
|
|
#pod =item phrase |
|
335
|
|
|
|
|
|
|
#pod |
|
336
|
|
|
|
|
|
|
#pod my $phrase = $address->phrase; |
|
337
|
|
|
|
|
|
|
#pod $address->phrase( "Me oh my" ); |
|
338
|
|
|
|
|
|
|
#pod |
|
339
|
|
|
|
|
|
|
#pod Accessor and mutator for the phrase portion of an address. |
|
340
|
|
|
|
|
|
|
#pod |
|
341
|
|
|
|
|
|
|
#pod =item address |
|
342
|
|
|
|
|
|
|
#pod |
|
343
|
|
|
|
|
|
|
#pod my $addr = $address->address; |
|
344
|
|
|
|
|
|
|
#pod $addr->address( "me@PROTECTED.com" ); |
|
345
|
|
|
|
|
|
|
#pod |
|
346
|
|
|
|
|
|
|
#pod Accessor and mutator for the address portion of an address. |
|
347
|
|
|
|
|
|
|
#pod |
|
348
|
|
|
|
|
|
|
#pod =item comment |
|
349
|
|
|
|
|
|
|
#pod |
|
350
|
|
|
|
|
|
|
#pod my $comment = $address->comment; |
|
351
|
|
|
|
|
|
|
#pod $address->comment( "(Work address)" ); |
|
352
|
|
|
|
|
|
|
#pod |
|
353
|
|
|
|
|
|
|
#pod Accessor and mutator for the comment portion of an address. |
|
354
|
|
|
|
|
|
|
#pod |
|
355
|
|
|
|
|
|
|
#pod =item original |
|
356
|
|
|
|
|
|
|
#pod |
|
357
|
|
|
|
|
|
|
#pod my $orig = $address->original; |
|
358
|
|
|
|
|
|
|
#pod |
|
359
|
|
|
|
|
|
|
#pod Accessor for the original address found when parsing, or passed |
|
360
|
|
|
|
|
|
|
#pod to C. |
|
361
|
|
|
|
|
|
|
#pod |
|
362
|
|
|
|
|
|
|
#pod =item host |
|
363
|
|
|
|
|
|
|
#pod |
|
364
|
|
|
|
|
|
|
#pod my $host = $address->host; |
|
365
|
|
|
|
|
|
|
#pod |
|
366
|
|
|
|
|
|
|
#pod Accessor for the host portion of an address's address. |
|
367
|
|
|
|
|
|
|
#pod |
|
368
|
|
|
|
|
|
|
#pod =item user |
|
369
|
|
|
|
|
|
|
#pod |
|
370
|
|
|
|
|
|
|
#pod my $user = $address->user; |
|
371
|
|
|
|
|
|
|
#pod |
|
372
|
|
|
|
|
|
|
#pod Accessor for the user portion of an address's address. |
|
373
|
|
|
|
|
|
|
#pod |
|
374
|
|
|
|
|
|
|
#pod =cut |
|
375
|
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
BEGIN { |
|
377
|
10
|
|
|
10
|
|
60
|
my %_INDEX = ( |
|
378
|
|
|
|
|
|
|
phrase => _PHRASE, |
|
379
|
|
|
|
|
|
|
address => _ADDRESS, |
|
380
|
|
|
|
|
|
|
comment => _COMMENT, |
|
381
|
|
|
|
|
|
|
original => _ORIGINAL, |
|
382
|
|
|
|
|
|
|
); |
|
383
|
|
|
|
|
|
|
|
|
384
|
10
|
|
|
|
|
42
|
for my $method (keys %_INDEX) { |
|
385
|
10
|
|
|
10
|
|
216439
|
no strict 'refs'; |
|
|
10
|
|
|
|
|
26
|
|
|
|
10
|
|
|
|
|
1536
|
|
|
386
|
40
|
|
|
|
|
74
|
my $index = $_INDEX{ $method }; |
|
387
|
|
|
|
|
|
|
*$method = sub { |
|
388
|
229
|
100
|
|
229
|
|
181032
|
if ($_[1]) { |
|
389
|
1
|
50
|
|
|
|
3
|
if ($_[0][_IN_CACHE]) { |
|
390
|
1
|
|
|
|
|
2
|
my $replicant = bless [ @{$_[0]} ] => ref $_[0]; |
|
|
1
|
|
|
|
|
4
|
|
|
391
|
1
|
|
|
|
|
3
|
$PARSE_CACHE{ ${ $_[0][_IN_CACHE][0] } }[ $_[0][_IN_CACHE][1] ] |
|
|
1
|
|
|
|
|
4
|
|
|
392
|
|
|
|
|
|
|
= $replicant; |
|
393
|
1
|
|
|
|
|
3
|
$_[0][_IN_CACHE] = undef; |
|
394
|
|
|
|
|
|
|
} |
|
395
|
1
|
|
|
|
|
2
|
$_[0]->[ $index ] = $_[1]; |
|
396
|
|
|
|
|
|
|
} else { |
|
397
|
228
|
|
|
|
|
964
|
$_[0]->[ $index ]; |
|
398
|
|
|
|
|
|
|
} |
|
399
|
40
|
|
|
|
|
1177
|
}; |
|
400
|
|
|
|
|
|
|
} |
|
401
|
|
|
|
|
|
|
} |
|
402
|
|
|
|
|
|
|
|
|
403
|
0
|
|
|
0
|
1
|
0
|
sub host { ($_[0]->[_ADDRESS] =~ /\@($domain)/o)[0] } |
|
404
|
0
|
|
|
0
|
1
|
0
|
sub user { ($_[0]->[_ADDRESS] =~ /($local_part)\@/o)[0] } |
|
405
|
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
#pod =pod |
|
407
|
|
|
|
|
|
|
#pod |
|
408
|
|
|
|
|
|
|
#pod =item format |
|
409
|
|
|
|
|
|
|
#pod |
|
410
|
|
|
|
|
|
|
#pod my $printable = $address->format; |
|
411
|
|
|
|
|
|
|
#pod |
|
412
|
|
|
|
|
|
|
#pod Returns a properly formatted RFC 2822 address representing the |
|
413
|
|
|
|
|
|
|
#pod object. |
|
414
|
|
|
|
|
|
|
#pod |
|
415
|
|
|
|
|
|
|
#pod =cut |
|
416
|
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
sub format { |
|
418
|
10
|
|
|
10
|
1
|
71
|
my $cache_str = do { no warnings 'uninitialized'; "@{$_[0]}" }; |
|
|
10
|
|
|
2014
|
|
25
|
|
|
|
10
|
|
|
|
|
3697
|
|
|
|
2014
|
|
|
|
|
580703
|
|
|
|
2014
|
|
|
|
|
2628
|
|
|
|
2014
|
|
|
|
|
6224
|
|
|
419
|
2014
|
100
|
|
|
|
8488
|
return $FORMAT_CACHE{$cache_str} if exists $FORMAT_CACHE{$cache_str}; |
|
420
|
334
|
|
|
|
|
805
|
$FORMAT_CACHE{$cache_str} = $_[0]->_format; |
|
421
|
|
|
|
|
|
|
} |
|
422
|
|
|
|
|
|
|
|
|
423
|
|
|
|
|
|
|
sub _format { |
|
424
|
334
|
|
|
334
|
|
640
|
my ($self) = @_; |
|
425
|
|
|
|
|
|
|
|
|
426
|
334
|
100
|
100
|
|
|
1287
|
unless (length $self->[_PHRASE] || length $self->[_COMMENT]) { |
|
427
|
86
|
|
100
|
|
|
529
|
return $self->[_ADDRESS] // ''; |
|
428
|
|
|
|
|
|
|
} |
|
429
|
|
|
|
|
|
|
|
|
430
|
248
|
|
100
|
|
|
791
|
my $comment = $self->[_COMMENT] // ''; |
|
431
|
248
|
100
|
100
|
|
|
685
|
$comment = "($comment)" if length $comment and $comment !~ /\A\(.*\)\z/; |
|
432
|
|
|
|
|
|
|
|
|
433
|
248
|
|
50
|
|
|
556
|
my $format = sprintf q{%s <%s> %s}, |
|
434
|
|
|
|
|
|
|
$self->_enquoted_phrase, |
|
435
|
|
|
|
|
|
|
($self->[_ADDRESS] // ''), |
|
436
|
|
|
|
|
|
|
$comment; |
|
437
|
|
|
|
|
|
|
|
|
438
|
248
|
|
|
|
|
753
|
$format =~ s/^\s+//; |
|
439
|
248
|
|
|
|
|
1080
|
$format =~ s/\s+$//; |
|
440
|
|
|
|
|
|
|
|
|
441
|
248
|
|
|
|
|
1216
|
return $format; |
|
442
|
|
|
|
|
|
|
} |
|
443
|
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
sub _enquoted_phrase { |
|
445
|
248
|
|
|
248
|
|
450
|
my ($self) = @_; |
|
446
|
|
|
|
|
|
|
|
|
447
|
248
|
|
|
|
|
445
|
my $phrase = $self->[_PHRASE]; |
|
448
|
|
|
|
|
|
|
|
|
449
|
248
|
100
|
|
|
|
519
|
return '' unless length $phrase; |
|
450
|
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
# if it's encoded -- rjbs, 2007-02-28 |
|
452
|
247
|
100
|
|
|
|
826
|
return $phrase if $phrase =~ /\A=\?.+\?=\z/; |
|
453
|
|
|
|
|
|
|
|
|
454
|
246
|
|
|
|
|
444
|
$phrase =~ s/\A"(.+)"\z/$1/; |
|
455
|
246
|
|
|
|
|
579
|
$phrase =~ s/([\\"])/\\$1/g; |
|
456
|
|
|
|
|
|
|
|
|
457
|
246
|
|
|
|
|
1573
|
return qq{"$phrase"}; |
|
458
|
|
|
|
|
|
|
} |
|
459
|
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
#pod =item name |
|
461
|
|
|
|
|
|
|
#pod |
|
462
|
|
|
|
|
|
|
#pod my $name = $address->name; |
|
463
|
|
|
|
|
|
|
#pod |
|
464
|
|
|
|
|
|
|
#pod This method tries very hard to determine the name belonging to the address. |
|
465
|
|
|
|
|
|
|
#pod First the C is checked. If that doesn't work out the C |
|
466
|
|
|
|
|
|
|
#pod is looked into. If that still doesn't work out, the C portion of |
|
467
|
|
|
|
|
|
|
#pod the C is returned. |
|
468
|
|
|
|
|
|
|
#pod |
|
469
|
|
|
|
|
|
|
#pod This method does B try to massage any name it identifies and instead |
|
470
|
|
|
|
|
|
|
#pod leaves that up to someone else. Who is it to decide if someone wants their |
|
471
|
|
|
|
|
|
|
#pod name capitalized, or if they're Irish? |
|
472
|
|
|
|
|
|
|
#pod |
|
473
|
|
|
|
|
|
|
#pod =cut |
|
474
|
|
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
sub name { |
|
476
|
10
|
|
|
10
|
1
|
73
|
my $cache_str = do { no warnings 'uninitialized'; "@{$_[0]}" }; |
|
|
10
|
|
|
663
|
|
23
|
|
|
|
10
|
|
|
|
|
2931
|
|
|
|
663
|
|
|
|
|
913
|
|
|
|
663
|
|
|
|
|
973
|
|
|
|
663
|
|
|
|
|
1943
|
|
|
477
|
663
|
100
|
|
|
|
2415
|
return $NAME_CACHE{$cache_str} if exists $NAME_CACHE{$cache_str}; |
|
478
|
|
|
|
|
|
|
|
|
479
|
314
|
|
|
|
|
576
|
my ($self) = @_; |
|
480
|
314
|
|
|
|
|
517
|
my $name = q{}; |
|
481
|
314
|
100
|
|
|
|
866
|
if ( $name = $self->[_PHRASE] ) { |
|
|
|
50
|
|
|
|
|
|
|
482
|
233
|
|
|
|
|
449
|
$name =~ s/^"//; |
|
483
|
233
|
|
|
|
|
435
|
$name =~ s/"$//; |
|
484
|
233
|
|
|
|
|
514
|
$name =~ s/($quoted_pair)/substr $1, -1/goe; |
|
|
0
|
|
|
|
|
0
|
|
|
485
|
|
|
|
|
|
|
} elsif ( $name = $self->[_COMMENT] ) { |
|
486
|
0
|
|
|
|
|
0
|
$name =~ s/^\(//; |
|
487
|
0
|
|
|
|
|
0
|
$name =~ s/\)$//; |
|
488
|
0
|
|
|
|
|
0
|
$name =~ s/($quoted_pair)/substr $1, -1/goe; |
|
|
0
|
|
|
|
|
0
|
|
|
489
|
0
|
|
|
|
|
0
|
$name =~ s/$comment/ /go; |
|
490
|
|
|
|
|
|
|
} else { |
|
491
|
81
|
|
|
|
|
1018
|
($name) = $self->[_ADDRESS] =~ /($local_part)\@/o; |
|
492
|
|
|
|
|
|
|
} |
|
493
|
314
|
|
|
|
|
1097
|
$NAME_CACHE{$cache_str} = $name; |
|
494
|
|
|
|
|
|
|
} |
|
495
|
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
#pod =back |
|
497
|
|
|
|
|
|
|
#pod |
|
498
|
|
|
|
|
|
|
#pod =head2 Overloaded Operators |
|
499
|
|
|
|
|
|
|
#pod |
|
500
|
|
|
|
|
|
|
#pod =over 4 |
|
501
|
|
|
|
|
|
|
#pod |
|
502
|
|
|
|
|
|
|
#pod =item stringify |
|
503
|
|
|
|
|
|
|
#pod |
|
504
|
|
|
|
|
|
|
#pod print "I have your email address, $address."; |
|
505
|
|
|
|
|
|
|
#pod |
|
506
|
|
|
|
|
|
|
#pod Objects stringify to C by default. It's possible that you don't |
|
507
|
|
|
|
|
|
|
#pod like that idea. Okay, then, you can change it by modifying |
|
508
|
|
|
|
|
|
|
#pod C<$Email:Address::STRINGIFY>. Please consider modifying this package |
|
509
|
|
|
|
|
|
|
#pod variable using C. You might step on someone else's toes if you |
|
510
|
|
|
|
|
|
|
#pod don't. |
|
511
|
|
|
|
|
|
|
#pod |
|
512
|
|
|
|
|
|
|
#pod { |
|
513
|
|
|
|
|
|
|
#pod local $Email::Address::STRINGIFY = 'host'; |
|
514
|
|
|
|
|
|
|
#pod print "I have your address, $address."; |
|
515
|
|
|
|
|
|
|
#pod # geeknest.com |
|
516
|
|
|
|
|
|
|
#pod } |
|
517
|
|
|
|
|
|
|
#pod print "I have your address, $address."; |
|
518
|
|
|
|
|
|
|
#pod # "Casey West" |
|
519
|
|
|
|
|
|
|
#pod |
|
520
|
|
|
|
|
|
|
#pod Modifying this package variable is now deprecated. Subclassing is now the |
|
521
|
|
|
|
|
|
|
#pod recommended approach. |
|
522
|
|
|
|
|
|
|
#pod |
|
523
|
|
|
|
|
|
|
#pod =cut |
|
524
|
|
|
|
|
|
|
|
|
525
|
|
|
|
|
|
|
sub as_string { |
|
526
|
669
|
50
|
|
669
|
0
|
3979
|
warn 'altering $Email::Address::STRINGIFY is deprecated; subclass instead' |
|
527
|
|
|
|
|
|
|
if $STRINGIFY ne 'format'; |
|
528
|
|
|
|
|
|
|
|
|
529
|
669
|
|
|
|
|
2306
|
$_[0]->can($STRINGIFY)->($_[0]); |
|
530
|
|
|
|
|
|
|
} |
|
531
|
|
|
|
|
|
|
|
|
532
|
10
|
|
|
10
|
|
7224
|
use overload '""' => 'as_string', fallback => 1; |
|
|
10
|
|
|
|
|
6868
|
|
|
|
10
|
|
|
|
|
66
|
|
|
533
|
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
#pod =pod |
|
535
|
|
|
|
|
|
|
#pod |
|
536
|
|
|
|
|
|
|
#pod =back |
|
537
|
|
|
|
|
|
|
#pod |
|
538
|
|
|
|
|
|
|
#pod =cut |
|
539
|
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
1; |
|
541
|
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
=pod |
|
543
|
|
|
|
|
|
|
|
|
544
|
|
|
|
|
|
|
=encoding UTF-8 |
|
545
|
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
=head1 NAME |
|
547
|
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
Email::Address - RFC 2822 Address Parsing and Creation |
|
549
|
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
=head1 VERSION |
|
551
|
|
|
|
|
|
|
|
|
552
|
|
|
|
|
|
|
version 1.913 |
|
553
|
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
555
|
|
|
|
|
|
|
|
|
556
|
|
|
|
|
|
|
use Email::Address; |
|
557
|
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
my @addresses = Email::Address->parse($line); |
|
559
|
|
|
|
|
|
|
my $address = Email::Address->new(Casey => 'casey@localhost'); |
|
560
|
|
|
|
|
|
|
|
|
561
|
|
|
|
|
|
|
print $address->format; |
|
562
|
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
564
|
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
This class implements a regex-based RFC 2822 parser that locates email |
|
566
|
|
|
|
|
|
|
addresses in strings and returns a list of C objects found. |
|
567
|
|
|
|
|
|
|
Alternatively you may construct objects manually. The goal of this software is |
|
568
|
|
|
|
|
|
|
to be correct, and very very fast. |
|
569
|
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
Version 1.909 and earlier of this module had vulnerabilies |
|
571
|
|
|
|
|
|
|
(L) |
|
572
|
|
|
|
|
|
|
and (L) |
|
573
|
|
|
|
|
|
|
which allowed specially constructed email to cause a denial of service. The |
|
574
|
|
|
|
|
|
|
reported vulnerabilities and some other pathalogical cases (meaning they really |
|
575
|
|
|
|
|
|
|
shouldn't occur in normal email) have been addressed in version 1.910 and newer. |
|
576
|
|
|
|
|
|
|
If you're running version 1.909 or older, you should update! |
|
577
|
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
Alternatively, you could switch to L|Email::Address::XS> |
|
579
|
|
|
|
|
|
|
which has a backward compatible API. B |
|
580
|
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
=head2 Package Variables |
|
582
|
|
|
|
|
|
|
|
|
583
|
|
|
|
|
|
|
B Email isn't easy (if even possible) to parse with a regex, I
|
|
584
|
|
|
|
|
|
|
least> if you're on a C prior to 5.10.0. Providing regular expressions |
|
585
|
|
|
|
|
|
|
for use by other programs isn't a great idea, because it makes it hard to |
|
586
|
|
|
|
|
|
|
improve the parser without breaking the "it's a regex" feature. Using these |
|
587
|
|
|
|
|
|
|
regular expressions is not encouraged, and methods like C<< |
|
588
|
|
|
|
|
|
|
Email::Address->is_addr_spec >> should be provided in the future. |
|
589
|
|
|
|
|
|
|
|
|
590
|
|
|
|
|
|
|
Several regular expressions used in this package are useful to others. |
|
591
|
|
|
|
|
|
|
For convenience, these variables are declared as package variables that |
|
592
|
|
|
|
|
|
|
you may access from your program. |
|
593
|
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
These regular expressions conform to the rules specified in RFC 2822. |
|
595
|
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
You can access these variables using the full namespace. If you want |
|
597
|
|
|
|
|
|
|
short names, define them yourself. |
|
598
|
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
my $addr_spec = $Email::Address::addr_spec; |
|
600
|
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
=over 4 |
|
602
|
|
|
|
|
|
|
|
|
603
|
|
|
|
|
|
|
=item $Email::Address::addr_spec |
|
604
|
|
|
|
|
|
|
|
|
605
|
|
|
|
|
|
|
This regular expression defined what an email address is allowed to |
|
606
|
|
|
|
|
|
|
look like. |
|
607
|
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
=item $Email::Address::angle_addr |
|
609
|
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
This regular expression defines an C<$addr_spec> wrapped in angle |
|
611
|
|
|
|
|
|
|
brackets. |
|
612
|
|
|
|
|
|
|
|
|
613
|
|
|
|
|
|
|
=item $Email::Address::name_addr |
|
614
|
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
This regular expression defines what an email address can look like |
|
616
|
|
|
|
|
|
|
with an optional preceding display name, also known as the C. |
|
617
|
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
=item $Email::Address::mailbox |
|
619
|
|
|
|
|
|
|
|
|
620
|
|
|
|
|
|
|
This is the complete regular expression defining an RFC 2822 email |
|
621
|
|
|
|
|
|
|
address with an optional preceding display name and optional |
|
622
|
|
|
|
|
|
|
following comment. |
|
623
|
|
|
|
|
|
|
|
|
624
|
|
|
|
|
|
|
=back |
|
625
|
|
|
|
|
|
|
|
|
626
|
|
|
|
|
|
|
=head2 Class Methods |
|
627
|
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
=over |
|
629
|
|
|
|
|
|
|
|
|
630
|
|
|
|
|
|
|
=item parse |
|
631
|
|
|
|
|
|
|
|
|
632
|
|
|
|
|
|
|
my @addrs = Email::Address->parse( |
|
633
|
|
|
|
|
|
|
q[me@local, Casey , "Casey" (West)] |
|
634
|
|
|
|
|
|
|
); |
|
635
|
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
This method returns a list of C objects it finds in the input |
|
637
|
|
|
|
|
|
|
string. B that it returns a list, and expects that it may find |
|
638
|
|
|
|
|
|
|
multiple addresses. The behavior in scalar context is undefined. |
|
639
|
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
The specification for an email address allows for infinitely nestable comments. |
|
641
|
|
|
|
|
|
|
That's nice in theory, but a little over done. By default this module allows |
|
642
|
|
|
|
|
|
|
for one (C<1>) level of nested comments. If you think you need more, modify the |
|
643
|
|
|
|
|
|
|
C<$Email::Address::COMMENT_NEST_LEVEL> package variable to allow more. |
|
644
|
|
|
|
|
|
|
|
|
645
|
|
|
|
|
|
|
$Email::Address::COMMENT_NEST_LEVEL = 10; # I'm deep |
|
646
|
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
The reason for this hardly-limiting limitation is simple: efficiency. |
|
648
|
|
|
|
|
|
|
|
|
649
|
|
|
|
|
|
|
Long strings of whitespace can be problematic for this module to parse, a bug |
|
650
|
|
|
|
|
|
|
which has not yet been adequately addressed. The default behavior is now to |
|
651
|
|
|
|
|
|
|
collapse multiple spaces into a single space, which avoids this problem. To |
|
652
|
|
|
|
|
|
|
prevent this behavior, set C<$Email::Address::COLLAPSE_SPACES> to zero. This |
|
653
|
|
|
|
|
|
|
variable will go away when the bug is resolved properly. |
|
654
|
|
|
|
|
|
|
|
|
655
|
|
|
|
|
|
|
In accordance with RFC 822 and its descendants, this module demands that email |
|
656
|
|
|
|
|
|
|
addresses be ASCII only. Any non-ASCII content in the parsed addresses will |
|
657
|
|
|
|
|
|
|
cause the parser to return no results. |
|
658
|
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
=item new |
|
660
|
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
my $address = Email::Address->new(undef, 'casey@local'); |
|
662
|
|
|
|
|
|
|
my $address = Email::Address->new('Casey West', 'casey@local'); |
|
663
|
|
|
|
|
|
|
my $address = Email::Address->new(undef, 'casey@local', '(Casey)'); |
|
664
|
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
Constructs and returns a new C object. Takes four |
|
666
|
|
|
|
|
|
|
positional arguments: phrase, email, and comment, and original string. |
|
667
|
|
|
|
|
|
|
|
|
668
|
|
|
|
|
|
|
The original string should only really be set using C. |
|
669
|
|
|
|
|
|
|
|
|
670
|
|
|
|
|
|
|
=item purge_cache |
|
671
|
|
|
|
|
|
|
|
|
672
|
|
|
|
|
|
|
Email::Address->purge_cache; |
|
673
|
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
One way this module stays fast is with internal caches. Caches live |
|
675
|
|
|
|
|
|
|
in memory and there is the remote possibility that you will have a |
|
676
|
|
|
|
|
|
|
memory problem. On the off chance that you think you're one of those |
|
677
|
|
|
|
|
|
|
people, this class method will empty those caches. |
|
678
|
|
|
|
|
|
|
|
|
679
|
|
|
|
|
|
|
I've loaded over 12000 objects and not encountered a memory problem. |
|
680
|
|
|
|
|
|
|
|
|
681
|
|
|
|
|
|
|
=item disable_cache |
|
682
|
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
=item enable_cache |
|
684
|
|
|
|
|
|
|
|
|
685
|
|
|
|
|
|
|
Email::Address->disable_cache if memory_low(); |
|
686
|
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
If you'd rather not cache address parses at all, you can disable (and |
|
688
|
|
|
|
|
|
|
re-enable) the Email::Address cache with these methods. The cache is enabled |
|
689
|
|
|
|
|
|
|
by default. |
|
690
|
|
|
|
|
|
|
|
|
691
|
|
|
|
|
|
|
=back |
|
692
|
|
|
|
|
|
|
|
|
693
|
|
|
|
|
|
|
=head2 Instance Methods |
|
694
|
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
=over 4 |
|
696
|
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
=item phrase |
|
698
|
|
|
|
|
|
|
|
|
699
|
|
|
|
|
|
|
my $phrase = $address->phrase; |
|
700
|
|
|
|
|
|
|
$address->phrase( "Me oh my" ); |
|
701
|
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
Accessor and mutator for the phrase portion of an address. |
|
703
|
|
|
|
|
|
|
|
|
704
|
|
|
|
|
|
|
=item address |
|
705
|
|
|
|
|
|
|
|
|
706
|
|
|
|
|
|
|
my $addr = $address->address; |
|
707
|
|
|
|
|
|
|
$addr->address( "me@PROTECTED.com" ); |
|
708
|
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
Accessor and mutator for the address portion of an address. |
|
710
|
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
=item comment |
|
712
|
|
|
|
|
|
|
|
|
713
|
|
|
|
|
|
|
my $comment = $address->comment; |
|
714
|
|
|
|
|
|
|
$address->comment( "(Work address)" ); |
|
715
|
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
Accessor and mutator for the comment portion of an address. |
|
717
|
|
|
|
|
|
|
|
|
718
|
|
|
|
|
|
|
=item original |
|
719
|
|
|
|
|
|
|
|
|
720
|
|
|
|
|
|
|
my $orig = $address->original; |
|
721
|
|
|
|
|
|
|
|
|
722
|
|
|
|
|
|
|
Accessor for the original address found when parsing, or passed |
|
723
|
|
|
|
|
|
|
to C. |
|
724
|
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
=item host |
|
726
|
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
my $host = $address->host; |
|
728
|
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
Accessor for the host portion of an address's address. |
|
730
|
|
|
|
|
|
|
|
|
731
|
|
|
|
|
|
|
=item user |
|
732
|
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
my $user = $address->user; |
|
734
|
|
|
|
|
|
|
|
|
735
|
|
|
|
|
|
|
Accessor for the user portion of an address's address. |
|
736
|
|
|
|
|
|
|
|
|
737
|
|
|
|
|
|
|
=item format |
|
738
|
|
|
|
|
|
|
|
|
739
|
|
|
|
|
|
|
my $printable = $address->format; |
|
740
|
|
|
|
|
|
|
|
|
741
|
|
|
|
|
|
|
Returns a properly formatted RFC 2822 address representing the |
|
742
|
|
|
|
|
|
|
object. |
|
743
|
|
|
|
|
|
|
|
|
744
|
|
|
|
|
|
|
=item name |
|
745
|
|
|
|
|
|
|
|
|
746
|
|
|
|
|
|
|
my $name = $address->name; |
|
747
|
|
|
|
|
|
|
|
|
748
|
|
|
|
|
|
|
This method tries very hard to determine the name belonging to the address. |
|
749
|
|
|
|
|
|
|
First the C is checked. If that doesn't work out the C |
|
750
|
|
|
|
|
|
|
is looked into. If that still doesn't work out, the C portion of |
|
751
|
|
|
|
|
|
|
the C is returned. |
|
752
|
|
|
|
|
|
|
|
|
753
|
|
|
|
|
|
|
This method does B try to massage any name it identifies and instead |
|
754
|
|
|
|
|
|
|
leaves that up to someone else. Who is it to decide if someone wants their |
|
755
|
|
|
|
|
|
|
name capitalized, or if they're Irish? |
|
756
|
|
|
|
|
|
|
|
|
757
|
|
|
|
|
|
|
=back |
|
758
|
|
|
|
|
|
|
|
|
759
|
|
|
|
|
|
|
=head2 Overloaded Operators |
|
760
|
|
|
|
|
|
|
|
|
761
|
|
|
|
|
|
|
=over 4 |
|
762
|
|
|
|
|
|
|
|
|
763
|
|
|
|
|
|
|
=item stringify |
|
764
|
|
|
|
|
|
|
|
|
765
|
|
|
|
|
|
|
print "I have your email address, $address."; |
|
766
|
|
|
|
|
|
|
|
|
767
|
|
|
|
|
|
|
Objects stringify to C by default. It's possible that you don't |
|
768
|
|
|
|
|
|
|
like that idea. Okay, then, you can change it by modifying |
|
769
|
|
|
|
|
|
|
C<$Email:Address::STRINGIFY>. Please consider modifying this package |
|
770
|
|
|
|
|
|
|
variable using C. You might step on someone else's toes if you |
|
771
|
|
|
|
|
|
|
don't. |
|
772
|
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
{ |
|
774
|
|
|
|
|
|
|
local $Email::Address::STRINGIFY = 'host'; |
|
775
|
|
|
|
|
|
|
print "I have your address, $address."; |
|
776
|
|
|
|
|
|
|
# geeknest.com |
|
777
|
|
|
|
|
|
|
} |
|
778
|
|
|
|
|
|
|
print "I have your address, $address."; |
|
779
|
|
|
|
|
|
|
# "Casey West" |
|
780
|
|
|
|
|
|
|
|
|
781
|
|
|
|
|
|
|
Modifying this package variable is now deprecated. Subclassing is now the |
|
782
|
|
|
|
|
|
|
recommended approach. |
|
783
|
|
|
|
|
|
|
|
|
784
|
|
|
|
|
|
|
=back |
|
785
|
|
|
|
|
|
|
|
|
786
|
|
|
|
|
|
|
=head2 Did I Mention Fast? |
|
787
|
|
|
|
|
|
|
|
|
788
|
|
|
|
|
|
|
On his 1.8GHz Apple MacBook, rjbs gets these results: |
|
789
|
|
|
|
|
|
|
|
|
790
|
|
|
|
|
|
|
$ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 5 |
|
791
|
|
|
|
|
|
|
Rate Mail::Address Email::Address |
|
792
|
|
|
|
|
|
|
Mail::Address 2.59/s -- -44% |
|
793
|
|
|
|
|
|
|
Email::Address 4.59/s 77% -- |
|
794
|
|
|
|
|
|
|
|
|
795
|
|
|
|
|
|
|
$ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 25 |
|
796
|
|
|
|
|
|
|
Rate Mail::Address Email::Address |
|
797
|
|
|
|
|
|
|
Mail::Address 2.58/s -- -67% |
|
798
|
|
|
|
|
|
|
Email::Address 7.84/s 204% -- |
|
799
|
|
|
|
|
|
|
|
|
800
|
|
|
|
|
|
|
$ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 50 |
|
801
|
|
|
|
|
|
|
Rate Mail::Address Email::Address |
|
802
|
|
|
|
|
|
|
Mail::Address 2.57/s -- -70% |
|
803
|
|
|
|
|
|
|
Email::Address 8.53/s 232% -- |
|
804
|
|
|
|
|
|
|
|
|
805
|
|
|
|
|
|
|
...unfortunately, a known bug causes a loss of speed the string to parse has |
|
806
|
|
|
|
|
|
|
certain known characteristics, and disabling cache will also degrade |
|
807
|
|
|
|
|
|
|
performance. |
|
808
|
|
|
|
|
|
|
|
|
809
|
|
|
|
|
|
|
=head1 PERL VERSION |
|
810
|
|
|
|
|
|
|
|
|
811
|
|
|
|
|
|
|
This library should run on perls released even a long time ago. It should work |
|
812
|
|
|
|
|
|
|
on any version of perl released in the last five years. |
|
813
|
|
|
|
|
|
|
|
|
814
|
|
|
|
|
|
|
Although it may work on older versions of perl, no guarantee is made that the |
|
815
|
|
|
|
|
|
|
minimum required version will not be increased. The version may be increased |
|
816
|
|
|
|
|
|
|
for any reason, and there is no promise that patches will be accepted to lower |
|
817
|
|
|
|
|
|
|
the minimum required perl. |
|
818
|
|
|
|
|
|
|
|
|
819
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
820
|
|
|
|
|
|
|
|
|
821
|
|
|
|
|
|
|
Thanks to Kevin Riggle and Tatsuhiko Miyagawa for tests for annoying |
|
822
|
|
|
|
|
|
|
phrase-quoting bugs! |
|
823
|
|
|
|
|
|
|
|
|
824
|
|
|
|
|
|
|
=head1 AUTHORS |
|
825
|
|
|
|
|
|
|
|
|
826
|
|
|
|
|
|
|
=over 4 |
|
827
|
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
=item * |
|
829
|
|
|
|
|
|
|
|
|
830
|
|
|
|
|
|
|
Casey West |
|
831
|
|
|
|
|
|
|
|
|
832
|
|
|
|
|
|
|
=item * |
|
833
|
|
|
|
|
|
|
|
|
834
|
|
|
|
|
|
|
Ricardo SIGNES |
|
835
|
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
=back |
|
837
|
|
|
|
|
|
|
|
|
838
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
|
839
|
|
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
=for stopwords Alex Vandiver David Golden Steinbrunner Glenn Fowler Jim Brandt Kevin Falcone Pali Ricardo Signes Ruslan Zakirov sunnavy William Yardley |
|
841
|
|
|
|
|
|
|
|
|
842
|
|
|
|
|
|
|
=over 4 |
|
843
|
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
=item * |
|
845
|
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
Alex Vandiver |
|
847
|
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
=item * |
|
849
|
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
David Golden |
|
851
|
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
=item * |
|
853
|
|
|
|
|
|
|
|
|
854
|
|
|
|
|
|
|
David Steinbrunner |
|
855
|
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
=item * |
|
857
|
|
|
|
|
|
|
|
|
858
|
|
|
|
|
|
|
Glenn Fowler |
|
859
|
|
|
|
|
|
|
|
|
860
|
|
|
|
|
|
|
=item * |
|
861
|
|
|
|
|
|
|
|
|
862
|
|
|
|
|
|
|
Jim Brandt |
|
863
|
|
|
|
|
|
|
|
|
864
|
|
|
|
|
|
|
=item * |
|
865
|
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
Kevin Falcone |
|
867
|
|
|
|
|
|
|
|
|
868
|
|
|
|
|
|
|
=item * |
|
869
|
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
Pali |
|
871
|
|
|
|
|
|
|
|
|
872
|
|
|
|
|
|
|
=item * |
|
873
|
|
|
|
|
|
|
|
|
874
|
|
|
|
|
|
|
Ricardo Signes |
|
875
|
|
|
|
|
|
|
|
|
876
|
|
|
|
|
|
|
=item * |
|
877
|
|
|
|
|
|
|
|
|
878
|
|
|
|
|
|
|
Ruslan Zakirov |
|
879
|
|
|
|
|
|
|
|
|
880
|
|
|
|
|
|
|
=item * |
|
881
|
|
|
|
|
|
|
|
|
882
|
|
|
|
|
|
|
sunnavy |
|
883
|
|
|
|
|
|
|
|
|
884
|
|
|
|
|
|
|
=item * |
|
885
|
|
|
|
|
|
|
|
|
886
|
|
|
|
|
|
|
William Yardley |
|
887
|
|
|
|
|
|
|
|
|
888
|
|
|
|
|
|
|
=back |
|
889
|
|
|
|
|
|
|
|
|
890
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
891
|
|
|
|
|
|
|
|
|
892
|
|
|
|
|
|
|
This software is copyright (c) 2004 by Casey West. |
|
893
|
|
|
|
|
|
|
|
|
894
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
895
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
896
|
|
|
|
|
|
|
|
|
897
|
|
|
|
|
|
|
=cut |
|
898
|
|
|
|
|
|
|
|
|
899
|
|
|
|
|
|
|
__END__ |