#!/usr/bin/perl
#
# eg5: grab a webpage and parse the links in it
#
use strict;
use warnings;

use Function::Parameters;
use LWP::Simple;
use HTML::Parser;
use URI::URL;

my $url;
my @links = ();

#
# deal with a start tag with it's attributes
#
fun findlinks( $tag, $attr )
{
	return unless $tag eq "a";
	my $link = $attr->{href};
	return unless defined $link;
	$link = url( $link, $url )->abs;
	push @links, $link;
}

# main program
die "Usage: eg7 [url]\n" unless @ARGV < 2;

$url = shift @ARGV || "http://www.doc.ic.ac.uk/~dcw/perl2014/";

my $contents = get( $url ) || die "eg7: can't fetch URL $url\n";

my $parser = new HTML::Parser(
	start_h => [ \&findlinks, 'tagname,attr'] );

$parser->parse( $contents );

# now @links contains the links - print them out.

foreach (@links)
{
	print "link: <$_>\n";
}
