#!/usr/bin/perl -w

use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL;

use strict;

my $agent = new LWP::UserAgent;

my @links;
sub callback {
	my ($type, %attributes) = @_;
	push @links, $attributes{'href'} if defined $attributes{'href'};
	push @links, $attributes{'img'} if defined $attributes{'img'};
	}

# Opět pro seznam URL na příkazové řádce zavoláme
# LWP::UserAgent::request a jako callback předáme data
# HTML::LinkExtorovi. Ten ale nyní plní HREF a IMG do pole @links; na
# konci zpracování pole vytiskneme, s absolutní cestou
for my $url (@ARGV) {
	@links = ();
	print STDERR "Will fetch $url.\n";
	my $extor = new HTML::LinkExtor( \&callback );
	my $response = $agent->request(
		HTTP::Request->new(GET => $url),
		sub { $extor->parse($_[0]) }
		);
	my $base = $response->base;
	print "Base was $base.\n";
	for (@links) {
		print url($_, $base)->abs, "\n";
	}
}

