HTML::Parser sample code
ef@kwinternet.com
Thu, 08 Jul 1999 20:09:39 -0400
Hi,
I am trying to figure out something.
in the code below, I can't figure out what this line means
@imgs = map { $_ = url($_, $base)->abs; } @imgs;
While I sort of know what "map" does I have never used it before so really
don't get what this line means, esp with the $base)->abs part in there.
Could someone spell it out for me?
Thanks very much,
Eric
# COPYRIGHT Copyright 1996-1998 Gisle Aas. Modified by Eric Frazier Tuesday,
July 06, 1999
use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL;
$url = "http://www.yahoo.com"; # for instance
$ua = new LWP::UserAgent;
# Set up a callback that collect image links
my @imgs = ();
sub callback {
my($tag, %attr) = @_;
return if $tag ne 'a'; # we only look closer at <img ...>
push(@imgs, values %attr);
}
# Make the parser. Unfortunately, we don't know the base yet
# (it might be diffent from $url)
$p = HTML::LinkExtor->new(\&callback);
# Request document and parse it as it arrives
$res = $ua->request(HTTP::Request->new(GET => $url),
sub {$p->parse($_[0])});
# Expand all image URLs to absolute ones
my $base = $res->base;
@imgs = map { $_ = url($_, $base)->abs; } @imgs;
# Print them out
print join("\n", @imgs), "\n";