URI::Find::Iterator - provides an iterator interface to URI::Find
use URI::Find::Iterator; my $string = "foo http://thegestalt.org/simon/ bar\n"; my $it = URI::Find::Iterator->new($string); while (my ($uri, $orig_match) = $it->match()) { print "Matched $uri\n"; $it->replace("<a href='$uri'>$uri</a>"); } # prints # foo <a href='http://thegestalt.org/simon/'>http://thegestalt.org/simon</a> bar print $it->result();
Inspired by Mark Jason Dominus' talk Programming with Iterators and Generators (available from http://perl.plover.com/yak/iterators/) this is an iterative version of URI::Find that hopefully makes code a little easier to understand and works slightly better with people's brains than callbacks do.
Takes a string checking as an argument. Optionally can also take a class name to extract regexes from (the class must have uri_re and schemeless_uri_re methods).
URI::Find::Iterator->new($string, class => "URI::Find::Schemeless");
would be the canonical example.
Alterantively it could take a straight regexp of your own devising
URI::Find::Iterator->new($string, re => "http://[^ ]+");
Returns the current match as a tuple - the first element of which is a URI::URL object and the second is the original text of the URI found.
Just like URI::Find.
It then advances to the next one.
Replaces the current match with replacement
Returns the string with all replacements.
None that I know of but there are probably loads.
It could possibly be split out into a generic Regex::Iterator module.
Distributed under the same terms as Perl itself.
Copyright (c) 2003, Simon Wistow <simon@thegestalt.org>
URI::Find, http://perl.plover.com/yak/iterators/
To install URI::Find::Iterator, copy and paste the appropriate command in to your terminal.
cpanm
cpanm URI::Find::Iterator
CPAN shell
perl -MCPAN -e shell install URI::Find::Iterator
For more information on module installation, please visit the detailed CPAN module installation guide.