Subject: | Specify a maximum number of emails to find |
Hi,
A user recently asked for help with this module on #perl at freenode.
They wanted to stop processing after a specific number of matches. I had a look at your module
and noticed that there's no easy way to bail out of a find early.
I had to suggest that they subclass and override your find method in order to do it.
Would you be interested in implementing such a feature in your module?
The normal way to do it would be to walk the string with while(m//g) {} and using substr with @-
etc. for the replacement, instead of the s///ge, but I think that would be slower and messier
than simply returning the find routine early from within the regexp replacement block, since it is
quite a short routine anyway.
I've attached a simple patch to illustrate.
Subject: | find_match_limit_support.patch |
40c40
< my($self, $r_text) = @_;
---
> my($self, $r_text, $match_limit) = @_;
45,47c45,53
< my($replace, $found) = $self->validate($1);
< $emails_found += $found;
< $replace;
---
> #return early if match limit reached
> if( defined $match_limit && $emails_found >= $match_limit ) {
> return $emails_found;
> }
>
> #otherwise process match
> my($replace, $found) = $self->validate($1);
> $emails_found += $found;
> $replace;
79,80c85,86
< sub find_emails(\$&) {
< my($r_text, $callback) = @_;
---
> sub find_emails(\$&$) {
> my($r_text, $callback, $match_limit) = @_;
82c88
< $finder->find($r_text);
---
> $finder->find($r_text, $match_limit);
129a136
> $num_emails_found = $finder->find(\$text, 10); #limit to 10 addresses
131a139,140
> Takes an optional integer as a second argument, which indicates the maximum number of email
> addresses that will be matched.