PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Email/Address.pm

https://github.com/gitpan/Email-Address
Perl | 850 lines | 206 code | 75 blank | 569 comment | 27 complexity | e8bf549606aa4101b270b4932539151a MD5 | raw file
Possible License(s): AGPL-1.0
  1. use strict;
  2. use warnings;
  3. package Email::Address;
  4. # ABSTRACT: RFC 2822 Address Parsing and Creation
  5. $Email::Address::VERSION = '1.905';
  6. our $COMMENT_NEST_LEVEL ||= 2;
  7. our $STRINGIFY ||= 'format';
  8. our $COLLAPSE_SPACES = 1 unless defined $COLLAPSE_SPACES; # I miss //=
  9. #pod =head1 SYNOPSIS
  10. #pod
  11. #pod use Email::Address;
  12. #pod
  13. #pod my @addresses = Email::Address->parse($line);
  14. #pod my $address = Email::Address->new(Casey => 'casey@localhost');
  15. #pod
  16. #pod print $address->format;
  17. #pod
  18. #pod =head1 VERSION
  19. #pod
  20. #pod version 1.898
  21. #pod
  22. #pod =head1 DESCRIPTION
  23. #pod
  24. #pod This class implements a regex-based RFC 2822 parser that locates email
  25. #pod addresses in strings and returns a list of C<Email::Address> objects found.
  26. #pod Alternatively you may construct objects manually. The goal of this software is
  27. #pod to be correct, and very very fast.
  28. #pod
  29. #pod =cut
  30. my $CTL = q{\x00-\x1F\x7F};
  31. my $special = q{()<>\\[\\]:;@\\\\,."};
  32. my $text = qr/[^\x0A\x0D]/;
  33. my $quoted_pair = qr/\\$text/;
  34. my $ctext = qr/(?>[^()\\]+)/;
  35. my ($ccontent, $comment) = (q{})x2;
  36. for (1 .. $COMMENT_NEST_LEVEL) {
  37. $ccontent = qr/$ctext|$quoted_pair|$comment/;
  38. $comment = qr/\s*\((?:\s*$ccontent)*\s*\)\s*/;
  39. }
  40. my $cfws = qr/$comment|\s+/;
  41. my $atext = qq/[^$CTL$special\\s]/;
  42. my $atom = qr/$cfws*$atext+$cfws*/;
  43. my $dot_atom_text = qr/$atext+(?:\.$atext+)*/;
  44. my $dot_atom = qr/$cfws*$dot_atom_text$cfws*/;
  45. my $qtext = qr/[^\\"]/;
  46. my $qcontent = qr/$qtext|$quoted_pair/;
  47. my $quoted_string = qr/$cfws*"$qcontent*"$cfws*/;
  48. my $word = qr/$atom|$quoted_string/;
  49. # XXX: This ($phrase) used to just be: my $phrase = qr/$word+/; It was changed
  50. # to resolve bug 22991, creating a significant slowdown. Given current speed
  51. # problems. Once 16320 is resolved, this section should be dealt with.
  52. # -- rjbs, 2006-11-11
  53. #my $obs_phrase = qr/$word(?:$word|\.|$cfws)*/;
  54. # XXX: ...and the above solution caused endless problems (never returned) when
  55. # examining this address, now in a test:
  56. # admin+=E6=96=B0=E5=8A=A0=E5=9D=A1_Weblog-- ATAT --test.socialtext.com
  57. # So we disallow the hateful CFWS in this context for now. Of modern mail
  58. # agents, only Apple Web Mail 2.0 is known to produce obs-phrase.
  59. # -- rjbs, 2006-11-19
  60. my $simple_word = qr/$atom|\.|\s*"$qcontent+"\s*/;
  61. my $obs_phrase = qr/$simple_word+/;
  62. my $phrase = qr/$obs_phrase|(?:$word+)/;
  63. my $local_part = qr/$dot_atom|$quoted_string/;
  64. my $dtext = qr/[^\[\]\\]/;
  65. my $dcontent = qr/$dtext|$quoted_pair/;
  66. my $domain_literal = qr/$cfws*\[(?:\s*$dcontent)*\s*\]$cfws*/;
  67. my $domain = qr/$dot_atom|$domain_literal/;
  68. my $display_name = $phrase;
  69. #pod =head2 Package Variables
  70. #pod
  71. #pod B<ACHTUNG!> Email isn't easy (if even possible) to parse with a regex, I<at
  72. #pod least> if you're on a C<perl> prior to 5.10.0. Providing regular expressions
  73. #pod for use by other programs isn't a great idea, because it makes it hard to
  74. #pod improve the parser without breaking the "it's a regex" feature. Using these
  75. #pod regular expressions is not encouraged, and methods like C<<
  76. #pod Email::Address->is_addr_spec >> should be provided in the future.
  77. #pod
  78. #pod Several regular expressions used in this package are useful to others.
  79. #pod For convenience, these variables are declared as package variables that
  80. #pod you may access from your program.
  81. #pod
  82. #pod These regular expressions conform to the rules specified in RFC 2822.
  83. #pod
  84. #pod You can access these variables using the full namespace. If you want
  85. #pod short names, define them yourself.
  86. #pod
  87. #pod my $addr_spec = $Email::Address::addr_spec;
  88. #pod
  89. #pod =over 4
  90. #pod
  91. #pod =item $Email::Address::addr_spec
  92. #pod
  93. #pod This regular expression defined what an email address is allowed to
  94. #pod look like.
  95. #pod
  96. #pod =item $Email::Address::angle_addr
  97. #pod
  98. #pod This regular expression defines an C<$addr_spec> wrapped in angle
  99. #pod brackets.
  100. #pod
  101. #pod =item $Email::Address::name_addr
  102. #pod
  103. #pod This regular expression defines what an email address can look like
  104. #pod with an optional preceding display name, also known as the C<phrase>.
  105. #pod
  106. #pod =item $Email::Address::mailbox
  107. #pod
  108. #pod This is the complete regular expression defining an RFC 2822 email
  109. #pod address with an optional preceding display name and optional
  110. #pod following comment.
  111. #pod
  112. #pod =back
  113. #pod
  114. #pod =cut
  115. our $addr_spec = qr/$local_part\@$domain/;
  116. our $angle_addr = qr/$cfws*<$addr_spec>$cfws*/;
  117. our $name_addr = qr/(?>$display_name?)$angle_addr/;
  118. our $mailbox = qr/(?:$name_addr|$addr_spec)$comment*/;
  119. sub _PHRASE () { 0 }
  120. sub _ADDRESS () { 1 }
  121. sub _COMMENT () { 2 }
  122. sub _ORIGINAL () { 3 }
  123. sub _IN_CACHE () { 4 }
  124. sub __dump {
  125. return {
  126. phrase => $_[0][_PHRASE],
  127. address => $_[0][_ADDRESS],
  128. comment => $_[0][_COMMENT],
  129. original => $_[0][_ORIGINAL],
  130. }
  131. }
  132. #pod =head2 Class Methods
  133. #pod
  134. #pod =over
  135. #pod
  136. #pod =item parse
  137. #pod
  138. #pod my @addrs = Email::Address->parse(
  139. #pod q[me@local, Casey <me@local>, "Casey" <me@local> (West)]
  140. #pod );
  141. #pod
  142. #pod This method returns a list of C<Email::Address> objects it finds in the input
  143. #pod string. B<Please note> that it returns a list, and expects that it may find
  144. #pod multiple addresses. The behavior in scalar context is undefined.
  145. #pod
  146. #pod The specification for an email address allows for infinitely nestable comments.
  147. #pod That's nice in theory, but a little over done. By default this module allows
  148. #pod for two (C<2>) levels of nested comments. If you think you need more, modify
  149. #pod the C<$Email::Address::COMMENT_NEST_LEVEL> package variable to allow more.
  150. #pod
  151. #pod $Email::Address::COMMENT_NEST_LEVEL = 10; # I'm deep
  152. #pod
  153. #pod The reason for this hardly-limiting limitation is simple: efficiency.
  154. #pod
  155. #pod Long strings of whitespace can be problematic for this module to parse, a bug
  156. #pod which has not yet been adequately addressed. The default behavior is now to
  157. #pod collapse multiple spaces into a single space, which avoids this problem. To
  158. #pod prevent this behavior, set C<$Email::Address::COLLAPSE_SPACES> to zero. This
  159. #pod variable will go away when the bug is resolved properly.
  160. #pod
  161. #pod In accordance with RFC 822 and its descendants, this module demands that email
  162. #pod addresses be ASCII only. Any non-ASCII content in the parsed addresses will
  163. #pod cause the parser to return no results.
  164. #pod
  165. #pod =cut
  166. our (%PARSE_CACHE, %FORMAT_CACHE, %NAME_CACHE);
  167. my $NOCACHE;
  168. sub __get_cached_parse {
  169. return if $NOCACHE;
  170. my ($class, $line) = @_;
  171. return @{$PARSE_CACHE{$line}} if exists $PARSE_CACHE{$line};
  172. return;
  173. }
  174. sub __cache_parse {
  175. return if $NOCACHE;
  176. my ($class, $line, $addrs) = @_;
  177. $PARSE_CACHE{$line} = $addrs;
  178. }
  179. sub parse {
  180. my ($class, $line) = @_;
  181. return unless $line;
  182. $line =~ s/[ \t]+/ /g if $COLLAPSE_SPACES;
  183. if (my @cached = $class->__get_cached_parse($line)) {
  184. return @cached;
  185. }
  186. my (@mailboxes) = ($line =~ /$mailbox/go);
  187. my @addrs;
  188. foreach (@mailboxes) {
  189. my $original = $_;
  190. my @comments = /($comment)/go;
  191. s/$comment//go if @comments;
  192. my ($user, $host, $com);
  193. ($user, $host) = ($1, $2) if s/<($local_part)\@($domain)>\s*\z//o;
  194. if (! defined($user) || ! defined($host)) {
  195. s/($local_part)\@($domain)//o;
  196. ($user, $host) = ($1, $2);
  197. }
  198. next if $user =~ /\P{ASCII}/;
  199. next if $host =~ /\P{ASCII}/;
  200. my ($phrase) = /($display_name)/o;
  201. for ( $phrase, $host, $user, @comments ) {
  202. next unless defined $_;
  203. s/^\s+//;
  204. s/\s+$//;
  205. $_ = undef unless length $_;
  206. }
  207. my $new_comment = join q{ }, @comments;
  208. push @addrs,
  209. $class->new($phrase, "$user\@$host", $new_comment, $original);
  210. $addrs[-1]->[_IN_CACHE] = [ \$line, $#addrs ]
  211. }
  212. $class->__cache_parse($line, \@addrs);
  213. return @addrs;
  214. }
  215. #pod =item new
  216. #pod
  217. #pod my $address = Email::Address->new(undef, 'casey@local');
  218. #pod my $address = Email::Address->new('Casey West', 'casey@local');
  219. #pod my $address = Email::Address->new(undef, 'casey@local', '(Casey)');
  220. #pod
  221. #pod Constructs and returns a new C<Email::Address> object. Takes four
  222. #pod positional arguments: phrase, email, and comment, and original string.
  223. #pod
  224. #pod The original string should only really be set using C<parse>.
  225. #pod
  226. #pod =cut
  227. sub new {
  228. my ($class, $phrase, $email, $comment, $orig) = @_;
  229. $phrase =~ s/\A"(.+)"\z/$1/ if $phrase;
  230. bless [ $phrase, $email, $comment, $orig ] => $class;
  231. }
  232. #pod =item purge_cache
  233. #pod
  234. #pod Email::Address->purge_cache;
  235. #pod
  236. #pod One way this module stays fast is with internal caches. Caches live
  237. #pod in memory and there is the remote possibility that you will have a
  238. #pod memory problem. On the off chance that you think you're one of those
  239. #pod people, this class method will empty those caches.
  240. #pod
  241. #pod I've loaded over 12000 objects and not encountered a memory problem.
  242. #pod
  243. #pod =cut
  244. sub purge_cache {
  245. %NAME_CACHE = ();
  246. %FORMAT_CACHE = ();
  247. %PARSE_CACHE = ();
  248. }
  249. #pod =item disable_cache
  250. #pod
  251. #pod =item enable_cache
  252. #pod
  253. #pod Email::Address->disable_cache if memory_low();
  254. #pod
  255. #pod If you'd rather not cache address parses at all, you can disable (and
  256. #pod re-enable) the Email::Address cache with these methods. The cache is enabled
  257. #pod by default.
  258. #pod
  259. #pod =cut
  260. sub disable_cache {
  261. my ($class) = @_;
  262. $class->purge_cache;
  263. $NOCACHE = 1;
  264. }
  265. sub enable_cache {
  266. $NOCACHE = undef;
  267. }
  268. #pod =back
  269. #pod
  270. #pod =head2 Instance Methods
  271. #pod
  272. #pod =over 4
  273. #pod
  274. #pod =item phrase
  275. #pod
  276. #pod my $phrase = $address->phrase;
  277. #pod $address->phrase( "Me oh my" );
  278. #pod
  279. #pod Accessor and mutator for the phrase portion of an address.
  280. #pod
  281. #pod =item address
  282. #pod
  283. #pod my $addr = $address->address;
  284. #pod $addr->address( "me@PROTECTED.com" );
  285. #pod
  286. #pod Accessor and mutator for the address portion of an address.
  287. #pod
  288. #pod =item comment
  289. #pod
  290. #pod my $comment = $address->comment;
  291. #pod $address->comment( "(Work address)" );
  292. #pod
  293. #pod Accessor and mutator for the comment portion of an address.
  294. #pod
  295. #pod =item original
  296. #pod
  297. #pod my $orig = $address->original;
  298. #pod
  299. #pod Accessor for the original address found when parsing, or passed
  300. #pod to C<new>.
  301. #pod
  302. #pod =item host
  303. #pod
  304. #pod my $host = $address->host;
  305. #pod
  306. #pod Accessor for the host portion of an address's address.
  307. #pod
  308. #pod =item user
  309. #pod
  310. #pod my $user = $address->user;
  311. #pod
  312. #pod Accessor for the user portion of an address's address.
  313. #pod
  314. #pod =cut
  315. BEGIN {
  316. my %_INDEX = (
  317. phrase => _PHRASE,
  318. address => _ADDRESS,
  319. comment => _COMMENT,
  320. original => _ORIGINAL,
  321. );
  322. for my $method (keys %_INDEX) {
  323. no strict 'refs';
  324. my $index = $_INDEX{ $method };
  325. *$method = sub {
  326. if ($_[1]) {
  327. if ($_[0][_IN_CACHE]) {
  328. my $replicant = bless [ @{$_[0]} ] => ref $_[0];
  329. $PARSE_CACHE{ ${ $_[0][_IN_CACHE][0] } }[ $_[0][_IN_CACHE][1] ]
  330. = $replicant;
  331. $_[0][_IN_CACHE] = undef;
  332. }
  333. $_[0]->[ $index ] = $_[1];
  334. } else {
  335. $_[0]->[ $index ];
  336. }
  337. };
  338. }
  339. }
  340. sub host { ($_[0]->[_ADDRESS] =~ /\@($domain)/o)[0] }
  341. sub user { ($_[0]->[_ADDRESS] =~ /($local_part)\@/o)[0] }
  342. #pod =pod
  343. #pod
  344. #pod =item format
  345. #pod
  346. #pod my $printable = $address->format;
  347. #pod
  348. #pod Returns a properly formatted RFC 2822 address representing the
  349. #pod object.
  350. #pod
  351. #pod =cut
  352. sub format {
  353. my $cache_str = do { no warnings 'uninitialized'; "@{$_[0]}" };
  354. return $FORMAT_CACHE{$cache_str} if exists $FORMAT_CACHE{$cache_str};
  355. $FORMAT_CACHE{$cache_str} = $_[0]->_format;
  356. }
  357. sub _format {
  358. my ($self) = @_;
  359. unless (
  360. defined $self->[_PHRASE] && length $self->[_PHRASE]
  361. ||
  362. defined $self->[_COMMENT] && length $self->[_COMMENT]
  363. ) {
  364. return defined $self->[_ADDRESS] ? $self->[_ADDRESS] : '';
  365. }
  366. my $comment = defined $self->[_COMMENT] ? $self->[_COMMENT] : '';
  367. $comment = "($comment)" if length $comment and $comment !~ /\A\(.*\)\z/;
  368. my $format = sprintf q{%s <%s> %s},
  369. $self->_enquoted_phrase,
  370. (defined $self->[_ADDRESS] ? $self->[_ADDRESS] : ''),
  371. $comment;
  372. $format =~ s/^\s+//;
  373. $format =~ s/\s+$//;
  374. return $format;
  375. }
  376. sub _enquoted_phrase {
  377. my ($self) = @_;
  378. my $phrase = $self->[_PHRASE];
  379. return '' unless defined $phrase and length $phrase;
  380. # if it's encoded -- rjbs, 2007-02-28
  381. return $phrase if $phrase =~ /\A=\?.+\?=\z/;
  382. $phrase =~ s/\A"(.+)"\z/$1/;
  383. $phrase =~ s/([\\"])/\\$1/g;
  384. return qq{"$phrase"};
  385. }
  386. #pod =item name
  387. #pod
  388. #pod my $name = $address->name;
  389. #pod
  390. #pod This method tries very hard to determine the name belonging to the address.
  391. #pod First the C<phrase> is checked. If that doesn't work out the C<comment>
  392. #pod is looked into. If that still doesn't work out, the C<user> portion of
  393. #pod the C<address> is returned.
  394. #pod
  395. #pod This method does B<not> try to massage any name it identifies and instead
  396. #pod leaves that up to someone else. Who is it to decide if someone wants their
  397. #pod name capitalized, or if they're Irish?
  398. #pod
  399. #pod =cut
  400. sub name {
  401. my $cache_str = do { no warnings 'uninitialized'; "@{$_[0]}" };
  402. return $NAME_CACHE{$cache_str} if exists $NAME_CACHE{$cache_str};
  403. my ($self) = @_;
  404. my $name = q{};
  405. if ( $name = $self->[_PHRASE] ) {
  406. $name =~ s/^"//;
  407. $name =~ s/"$//;
  408. $name =~ s/($quoted_pair)/substr $1, -1/goe;
  409. } elsif ( $name = $self->[_COMMENT] ) {
  410. $name =~ s/^\(//;
  411. $name =~ s/\)$//;
  412. $name =~ s/($quoted_pair)/substr $1, -1/goe;
  413. $name =~ s/$comment/ /go;
  414. } else {
  415. ($name) = $self->[_ADDRESS] =~ /($local_part)\@/o;
  416. }
  417. $NAME_CACHE{$cache_str} = $name;
  418. }
  419. #pod =back
  420. #pod
  421. #pod =head2 Overloaded Operators
  422. #pod
  423. #pod =over 4
  424. #pod
  425. #pod =item stringify
  426. #pod
  427. #pod print "I have your email address, $address.";
  428. #pod
  429. #pod Objects stringify to C<format> by default. It's possible that you don't
  430. #pod like that idea. Okay, then, you can change it by modifying
  431. #pod C<$Email:Address::STRINGIFY>. Please consider modifying this package
  432. #pod variable using C<local>. You might step on someone else's toes if you
  433. #pod don't.
  434. #pod
  435. #pod {
  436. #pod local $Email::Address::STRINGIFY = 'host';
  437. #pod print "I have your address, $address.";
  438. #pod # geeknest.com
  439. #pod }
  440. #pod print "I have your address, $address.";
  441. #pod # "Casey West" <casey@geeknest.com>
  442. #pod
  443. #pod Modifying this package variable is now deprecated. Subclassing is now the
  444. #pod recommended approach.
  445. #pod
  446. #pod =cut
  447. sub as_string {
  448. warn 'altering $Email::Address::STRINGIFY is deprecated; subclass instead'
  449. if $STRINGIFY ne 'format';
  450. $_[0]->can($STRINGIFY)->($_[0]);
  451. }
  452. use overload '""' => 'as_string', fallback => 1;
  453. #pod =pod
  454. #pod
  455. #pod =back
  456. #pod
  457. #pod =cut
  458. 1;
  459. =pod
  460. =encoding UTF-8
  461. =head1 NAME
  462. Email::Address - RFC 2822 Address Parsing and Creation
  463. =head1 VERSION
  464. version 1.905
  465. =head1 SYNOPSIS
  466. use Email::Address;
  467. my @addresses = Email::Address->parse($line);
  468. my $address = Email::Address->new(Casey => 'casey@localhost');
  469. print $address->format;
  470. =head1 DESCRIPTION
  471. This class implements a regex-based RFC 2822 parser that locates email
  472. addresses in strings and returns a list of C<Email::Address> objects found.
  473. Alternatively you may construct objects manually. The goal of this software is
  474. to be correct, and very very fast.
  475. =head2 Package Variables
  476. B<ACHTUNG!> Email isn't easy (if even possible) to parse with a regex, I<at
  477. least> if you're on a C<perl> prior to 5.10.0. Providing regular expressions
  478. for use by other programs isn't a great idea, because it makes it hard to
  479. improve the parser without breaking the "it's a regex" feature. Using these
  480. regular expressions is not encouraged, and methods like C<<
  481. Email::Address->is_addr_spec >> should be provided in the future.
  482. Several regular expressions used in this package are useful to others.
  483. For convenience, these variables are declared as package variables that
  484. you may access from your program.
  485. These regular expressions conform to the rules specified in RFC 2822.
  486. You can access these variables using the full namespace. If you want
  487. short names, define them yourself.
  488. my $addr_spec = $Email::Address::addr_spec;
  489. =over 4
  490. =item $Email::Address::addr_spec
  491. This regular expression defined what an email address is allowed to
  492. look like.
  493. =item $Email::Address::angle_addr
  494. This regular expression defines an C<$addr_spec> wrapped in angle
  495. brackets.
  496. =item $Email::Address::name_addr
  497. This regular expression defines what an email address can look like
  498. with an optional preceding display name, also known as the C<phrase>.
  499. =item $Email::Address::mailbox
  500. This is the complete regular expression defining an RFC 2822 email
  501. address with an optional preceding display name and optional
  502. following comment.
  503. =back
  504. =head2 Class Methods
  505. =over
  506. =item parse
  507. my @addrs = Email::Address->parse(
  508. q[me@local, Casey <me@local>, "Casey" <me@local> (West)]
  509. );
  510. This method returns a list of C<Email::Address> objects it finds in the input
  511. string. B<Please note> that it returns a list, and expects that it may find
  512. multiple addresses. The behavior in scalar context is undefined.
  513. The specification for an email address allows for infinitely nestable comments.
  514. That's nice in theory, but a little over done. By default this module allows
  515. for two (C<2>) levels of nested comments. If you think you need more, modify
  516. the C<$Email::Address::COMMENT_NEST_LEVEL> package variable to allow more.
  517. $Email::Address::COMMENT_NEST_LEVEL = 10; # I'm deep
  518. The reason for this hardly-limiting limitation is simple: efficiency.
  519. Long strings of whitespace can be problematic for this module to parse, a bug
  520. which has not yet been adequately addressed. The default behavior is now to
  521. collapse multiple spaces into a single space, which avoids this problem. To
  522. prevent this behavior, set C<$Email::Address::COLLAPSE_SPACES> to zero. This
  523. variable will go away when the bug is resolved properly.
  524. In accordance with RFC 822 and its descendants, this module demands that email
  525. addresses be ASCII only. Any non-ASCII content in the parsed addresses will
  526. cause the parser to return no results.
  527. =item new
  528. my $address = Email::Address->new(undef, 'casey@local');
  529. my $address = Email::Address->new('Casey West', 'casey@local');
  530. my $address = Email::Address->new(undef, 'casey@local', '(Casey)');
  531. Constructs and returns a new C<Email::Address> object. Takes four
  532. positional arguments: phrase, email, and comment, and original string.
  533. The original string should only really be set using C<parse>.
  534. =item purge_cache
  535. Email::Address->purge_cache;
  536. One way this module stays fast is with internal caches. Caches live
  537. in memory and there is the remote possibility that you will have a
  538. memory problem. On the off chance that you think you're one of those
  539. people, this class method will empty those caches.
  540. I've loaded over 12000 objects and not encountered a memory problem.
  541. =item disable_cache
  542. =item enable_cache
  543. Email::Address->disable_cache if memory_low();
  544. If you'd rather not cache address parses at all, you can disable (and
  545. re-enable) the Email::Address cache with these methods. The cache is enabled
  546. by default.
  547. =back
  548. =head2 Instance Methods
  549. =over 4
  550. =item phrase
  551. my $phrase = $address->phrase;
  552. $address->phrase( "Me oh my" );
  553. Accessor and mutator for the phrase portion of an address.
  554. =item address
  555. my $addr = $address->address;
  556. $addr->address( "me@PROTECTED.com" );
  557. Accessor and mutator for the address portion of an address.
  558. =item comment
  559. my $comment = $address->comment;
  560. $address->comment( "(Work address)" );
  561. Accessor and mutator for the comment portion of an address.
  562. =item original
  563. my $orig = $address->original;
  564. Accessor for the original address found when parsing, or passed
  565. to C<new>.
  566. =item host
  567. my $host = $address->host;
  568. Accessor for the host portion of an address's address.
  569. =item user
  570. my $user = $address->user;
  571. Accessor for the user portion of an address's address.
  572. =item format
  573. my $printable = $address->format;
  574. Returns a properly formatted RFC 2822 address representing the
  575. object.
  576. =item name
  577. my $name = $address->name;
  578. This method tries very hard to determine the name belonging to the address.
  579. First the C<phrase> is checked. If that doesn't work out the C<comment>
  580. is looked into. If that still doesn't work out, the C<user> portion of
  581. the C<address> is returned.
  582. This method does B<not> try to massage any name it identifies and instead
  583. leaves that up to someone else. Who is it to decide if someone wants their
  584. name capitalized, or if they're Irish?
  585. =back
  586. =head2 Overloaded Operators
  587. =over 4
  588. =item stringify
  589. print "I have your email address, $address.";
  590. Objects stringify to C<format> by default. It's possible that you don't
  591. like that idea. Okay, then, you can change it by modifying
  592. C<$Email:Address::STRINGIFY>. Please consider modifying this package
  593. variable using C<local>. You might step on someone else's toes if you
  594. don't.
  595. {
  596. local $Email::Address::STRINGIFY = 'host';
  597. print "I have your address, $address.";
  598. # geeknest.com
  599. }
  600. print "I have your address, $address.";
  601. # "Casey West" <casey@geeknest.com>
  602. Modifying this package variable is now deprecated. Subclassing is now the
  603. recommended approach.
  604. =back
  605. =head2 Did I Mention Fast?
  606. On his 1.8GHz Apple MacBook, rjbs gets these results:
  607. $ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 5
  608. Rate Mail::Address Email::Address
  609. Mail::Address 2.59/s -- -44%
  610. Email::Address 4.59/s 77% --
  611. $ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 25
  612. Rate Mail::Address Email::Address
  613. Mail::Address 2.58/s -- -67%
  614. Email::Address 7.84/s 204% --
  615. $ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 50
  616. Rate Mail::Address Email::Address
  617. Mail::Address 2.57/s -- -70%
  618. Email::Address 8.53/s 232% --
  619. ...unfortunately, a known bug causes a loss of speed the string to parse has
  620. certain known characteristics, and disabling cache will also degrade
  621. performance.
  622. =head1 VERSION
  623. version 1.898
  624. =head1 ACKNOWLEDGEMENTS
  625. Thanks to Kevin Riggle and Tatsuhiko Miyagawa for tests for annoying
  626. phrase-quoting bugs!
  627. =head1 AUTHORS
  628. =over 4
  629. =item *
  630. Casey West
  631. =item *
  632. Ricardo SIGNES <rjbs@cpan.org>
  633. =back
  634. =head1 COPYRIGHT AND LICENSE
  635. This software is copyright (c) 2004 by Casey West.
  636. This is free software; you can redistribute it and/or modify it under
  637. the same terms as the Perl 5 programming language system itself.
  638. =cut
  639. __END__
  640. #pod =head2 Did I Mention Fast?
  641. #pod
  642. #pod On his 1.8GHz Apple MacBook, rjbs gets these results:
  643. #pod
  644. #pod $ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 5
  645. #pod Rate Mail::Address Email::Address
  646. #pod Mail::Address 2.59/s -- -44%
  647. #pod Email::Address 4.59/s 77% --
  648. #pod
  649. #pod $ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 25
  650. #pod Rate Mail::Address Email::Address
  651. #pod Mail::Address 2.58/s -- -67%
  652. #pod Email::Address 7.84/s 204% --
  653. #pod
  654. #pod $ perl -Ilib bench/ea-vs-ma.pl bench/corpus.txt 50
  655. #pod Rate Mail::Address Email::Address
  656. #pod Mail::Address 2.57/s -- -70%
  657. #pod Email::Address 8.53/s 232% --
  658. #pod
  659. #pod ...unfortunately, a known bug causes a loss of speed the string to parse has
  660. #pod certain known characteristics, and disabling cache will also degrade
  661. #pod performance.
  662. #pod
  663. #pod =head1 ACKNOWLEDGEMENTS
  664. #pod
  665. #pod Thanks to Kevin Riggle and Tatsuhiko Miyagawa for tests for annoying
  666. #pod phrase-quoting bugs!
  667. #pod
  668. #pod =cut