E-Bitch has asked for the wisdom of the Perl Monks concerning the following question:

Okay, so, I grabbed the example from HTML::LinkExtor pod, and, here's the problem. I put everything into a subroutine (my first excursion into subs), and everything works great the first call through. (in other words, subname($eh, $what) works, but when you execute the identical code a second time, it doesnt work.). So here is the symptom. The subroutine returns an array, and the array is populated the first time just fine, but the second+ time around, it wont get populated. Any Ideas?
#!/usr/bin/perl -I/tmp/perl/libwww-perl-5.53/lib/ use 5.005; use strict; use LWP::UserAgent; use HTML::Parser; use HTML::LinkExtor; use URI; use URI::URL; my $ua = LWP::UserAgent->new; sub extractlinks { my($url, $linktype) = @_; my @sublinks = (); sub callback { my($tag, %attr) = @_; return if $tag ne $linktype; push(@sublinks, values %attr); } my $p = HTML::LinkExtor->new(\&callback); my $res = $ua->request(HTTP::Request->new(GET => $url), sub {$p->p +arse($_[0])}); my $base = $res->base; return map { $_ = url($_, $base)->abs; } @sublinks; } my @links = extractlinks("http://www.ebitch.cc", "a"); my @totallinks; push @totallinks, @links; for(my $i = 0; $i < $#links; $i++) { push @totallinks, extractlinks($links[$i], "a"); } print join("\n", @totallinks), "\n";
When running with -w mode, I get the following errors (which I know have something to do with this, but I'm not sure what.)
Variable "$linktype" will not stay shared at lnkchk.pl line 22. Variable "@sublinks" will not stay shared at lnkchk.pl line 23.


thanks in advance,
E-Bitch

Edit: chipmunk 2001-06-27

Replies are listed 'Best First'.
Re: Subroutine not working quite right
by Abigail (Deacon) on Jun 28, 2001 at 01:26 UTC
    Your problem is the nested subroutine. It's a named subroutine, and it gets compiled once. It's not a reference to a sub, so no closure is created - and that's exactly what you want here. You're likely to find that the following works better:
    sub extractlinks { my($url, $linktype) = @_; my @sublinks = (); my $callback = sub { my($tag, %attr) = @_; return if $tag ne $linktype; push(@sublinks, values %attr); } my $p = HTML::LinkExtor->new($callback); my $res = $ua->request(HTTP::Request->new(GET => $url), sub {$p->parse($_[0])}); my $base = $res->base; return map {$_ = url($_, $base)->abs; } @sublinks; }

    -- Abigail

(jeffa) Re: Subroutine not working quite right
by jeffa (Bishop) on Jun 28, 2001 at 01:25 UTC
    Just a hunch, not tested - how about trying an array reference?
    return [ map { $_ = url($_, $base)->abs; } @sublinks ];
    UPDATE: Abigail has you covered, and grinder explained the same answer in the ChatterBox. But i still recommend that you return a reference to the array instead, especially if you are immediately pushing it to another array.

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--