use HTML::LinkExtor;
use LWP::Simple;

sub Iterator (&) {
  $_[0];
}

sub traverse {
  my $interesting_links = sub { @_ };
  $interesting_links = shift if ref $_[0] eq 'CODE';

  my @queue = @_;
  my %seen;

  return Iterator {
    while (@queue) {
      my $url = shift @queue;
      $url =~ s/#.*$//;
      next if $seen{$url}++;

      my ($content_type) = head($url);
      if ($content_type eq 'text/html') {
        my $html = get($url);
        push @queue, $interesting_links->(get_links($url, $html))
      }
      return $url;
    }
    return;                     # exhausted
  }
}


sub get_links {
  my ($base, $html) = @_;
  my @links;
  my $more_links = sub {
    my ($tag, %attrs) = @_;
    push @links, values %attrs;
  };

  HTML::LinkExtor->new($more_links, $base)->parse($html);
  return @links;
}

sub NEXTVAL { $_[0]->() }

my $top = 'http://perl.plover.com/';
my $interesting = sub { my @a = grep /^\Q$top/o, @_; print "Matched ", scalar @a, " urls\n";@a};
my $urls = traverse($interesting, $top);
for (1..($ARGV[0] || 10)) {
  my $url = NEXTVAL($urls);
  print "$url\n";
}
