The minimal change consists of changing
my $url = "http://www.nukeforums.com/forums/viewforum.php?f=17"; my $ua = LWP::RobotUA->new; my $lp = HTML::LinkExtor->new(\&wanted_links); my @links; get_threads($url); foreach my $page (@links) { ... }
to
my $ua = LWP::RobotUA->new; my $lp = HTML::LinkExtor->new(\&wanted_links); my @links; foreach my $forum_id (17, 3) { my $url = "http://www.nukeforums.com/forums/viewforum.php?f=$forum +_id"; @links = (); # yuck! my $links = get_threads($url); foreach my $page (@$links) { ... } }

As you can see, I don't like your use of the global variable @links. We're forced to provide and initialize a variable that should be local to get_threads. Here's the fix:

#!/usr/bin/perl use strict; use warnings; use LWP::RobotUA; use HTML::LinkExtor; use HTML::TokeParser; use URI::URL; use Data::Dumper; # for show and troubleshooting my $ua = LWP::RobotUA->new(); foreach my $forum_id (17, 3) { my $url = "http://www.nukeforums.com/forums/viewforum.php?f=$forum +_id"; my $links = get_threads($url); foreach my $page (@$links) { ... } } sub get_thread { ... } sub get_threads { my $page = shift; my @links; my $lp = HTML::LinkExtor->new(sub { my($tag, %attr) = @_; return unless exists $attr{'href'}; return if $attr{'href'} !~ /^viewtopic\.php\?t=/; push @links, values %attr; }); my $request = HTTP::Request->new(GET => $url); my $response = $ua->request($request, sub {$lp->parse($_[0])}); # Expand URLs to absolute ones my $base = $response->base; return [ map { url($_, $base)->abs } @links ]; }

Update: Added the minimal change.

Edited by planetscape - Reparented from Reaped: LWP & HTML::LinkExtor running recursively against a bulletin board to Re^3: proof of concept how to run this code

( keep:0 edit:17 reap:2 )


In reply to Re^4: proof of concept how to run this code by ikegami
in thread proof of concept how to run this code by perl_lover_girl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.