in reply to Why is this link parser only working the first time?

If you had turned warnings on you would have receieved a message about "cannot stay shared". I tried to explain the error in RE (3): BrainPain-Help, I never got any feedback on it so I have no idea whether it is or isn't understandable.

Anyways to fix your problem is simplicity, instead of writing:

sub attachment_link_extractor { # ...
write that as
my $link_extractor = sub { # ...
And then just pass the variable in to the constructor to LinkExtor.

Replies are listed 'Best First'.
Re: Re (tilly) 1: Why is this link parser only working the first time?
by dave_aiello (Pilgrim) on Jan 20, 2001 at 01:19 UTC
    tilly:

    Thanks very much. This is an excellent solution because the code change is limited to a single line.

    What used to be:

    sub attachment_link_extractor { my ($tag, %attr) = @_; push(@links, values %attr) if (($tag eq 'a') && ($attr{href} =~ m/attachments/)); } my $p = HTML::LinkExtor->new(\&attachment_link_extractor);
    ... now becomes ...
    $attachment_link_extractor = sub { my ($tag, %attr) = @_; push(@links, values %attr) if (($tag eq 'a') && ($attr{href} =~ m/attachments/)); }; my $p = HTML::LinkExtor->new($attachment_link_extractor);
    The only part of the your suggested change that confused me was the need to put a semicolon after the bracket that closes the subroutine. I don't remember ever writing Perl that way before, but, I keep forgetting that the subroutine itself is part of an assignment statement.

    Dave Aiello
    Chatham Township Data Corporation

      Sorry, I should have remembered the semi-colon. That is the kind of detail I stopped thinking about, they just automatically go in when I write, and if I edit then run I expect to have Perl catch.

      You are right about why you need it. In the language used in perlsyn, the subroutine is now within a "simple statement" and you need to terminate the statement.

      As for writing Perl like that, I do it all of the time and recommend it whenever I get the chance. :-)