[Chicago-talk] "Higher Order Perl" example question (page 191)
Andy_Bach@wiwb.uscourts.gov
Andy_Bach at wiwb.uscourts.gov
Tue Dec 6 10:42:14 PST 2005
Hey folks,
Sent this to MJD but he's off in London so ... in the iterator/web spider
section, he building up various web page 'walking' iterators. traverse()
returns an "Iterator" . Originally the IT creator just took a list of
URLs, which it put in a queue. 'kicking' (his term for doing a 'NEXTVAL'
on an iterator) the iterator too the first URL off the queue, pushed any
URLs it contained (if it is of content-type text/html) on the queue and
returned the URL.
He then adds a callback as the first param to the traverse() IT generator.
This is to let the URL only return 'interesting' (passed by the callback
sub) URLs something like:
my $top = 'http://perl.plover.com/';
my $interesting = sub { grep /^\Q$top/o, @_ };
and so you'd create a new IT by:
my $urls = traverse($interesting, $top);
The change to the traverse() sub is:
sub traverse {
my $interesting_links = sub { @_ };
$interesting_links = shift if ref $_[0] eq 'CODE';
....
push @queue, $interesting_links->(get_links($url, $html));
## used to be
# push @queue, (get_links($url, $html);
so, if I *don't* pass a sub ref as the first param, $interesting_links
will be a ref to a sub of the list of URLs? How is that going to work w/
get_links()?
... ahh, that's wrong. $interesting_links will be a ref to a sub that
returns its own '@_', that is, the URLs returned by get_links() if there's
no passed-in call back. I was confused on when that @_ was going to be
... used? invoked? something.
Never mind ;-> <whew> it is a very interesting book. Its a bit (at page
191) all over the place, but all the places are quite useful and
informative, so far. Not easy, by any means, this is not the first
example I've had got back over and try and figure out how what he says has
happened actually got implemented. That is probably a good thing.
a
Andy Bach, Sys. Mangler
Internet: andy_bach at wiwb.uscourts.gov
VOICE: (608) 261-5738 FAX 264-5932
"yeah, but does it have a cute little puppy that wags it's tail
and scampers around on the screen?"
BillG on linux
More information about the Chicago-talk
mailing list