in reply to Email::Find question

Email::Find has, I think, a curious design. The text returned by the callback function is used to modify the contents of the original input text passed to the "find()" method, and "find()" itself simply returns "true" or "false" (1 or 0) for success or failure.

The easiest way to get what you want (but perhaps not satisfactory in terms of OO/modular design) would be to use an array that is "global" to the main caller and callback function:

#!/usr/bin/perl use strict; use warnings; use Email::Find; my $text = "this is some text test\@email.com foo bar"; my @found; my $finder = Email::Find->new( \&find_email ); $finder->find(\$text); print "Found says @found\n"; exit; sub find_email { my ( $email, $orig_email ) = @_; my $ret = $email->format(); print "sub says " ,$ret, "\n"; push @found, $ret; return; } # end-sub
(Note that by returning undef, the callback will be removing email addresses from the original text string as they are processed.)

Replies are listed 'Best First'.
Re^2: Email::Find question
by ikegami (Patriarch) on Mar 12, 2007 at 08:46 UTC

    The same, wrapped up in a convenient function:

    #!/usr/bin/perl use strict; use warnings; use Email::Find qw( ); sub extract_emails { my @emails; Email::Find->new(sub { my ($email, $orig_email) = @_; push @emails, $email->format(); return $orig_email; })->find(\$_[0]); return @emails; } my $text = "this is some text test\@email.com foo bar"; my @found = extract_emails($text);