in reply to A question about HTML::TokeParser

Your source of error has been identified by others, ie., the token parser is not scoped/recursive, and your inner loop looking for options caused side effect on the outer loop, which exited prematurely.

Being the HTML Token Parser, it's good at parsing tokens. :-) You could rewrite your loop using the token parser, instead of the get_tag.

... while (my $token = $parser->get_token) { next unless $token->[1] =~ /(?:select|input|textarea)/; if ($token->[0] eq 'S') # start tag { $count++; my $tag = $token->[1]; my $name = $token->[2]{name}; # fetch name of input my $value = $token->[2]{value}; my $maxlength = $token->[2]{maxlength}; my $required = $token->[2]{required}; my $allowed; if ($tag eq 'select') { while (my $option = $parser->get_token) { last if $option->[0] eq 'E' && $option->[1] eq 'select'; next unless $option->[0] eq 'S' && $option->[1] eq 'option'; push @{$allowed}, $option->[2]{value}; } } else { $allowed = [ $token->[2]{allowed} ]; } $DEBUG && print "$count\t$tag\t$name\t\n"; if ($tag eq 'select') { print Dumper($allowed); } } ... }
And the debug output shows:
1 input text1 2 textarea textarea1 3 input radio1 4 input radio1 5 input check1 6 input check1 7 select list1 $VAR1 = [ '1', '2', '3' ]; 8 input text2

Replies are listed 'Best First'.
Re: Re: A question about HTML::TokeParser
by theguvnor (Chaplain) on Oct 14, 2003 at 13:09 UTC

    First thanks to everyone who responded. Secondly, apologies for popping up to ask a question and then not responding sooner.. had only intermittent access over the Canadian Thanksgiving weekend. Thirdly, an extra ++ to Roger for providing a working re-write. After seeing the first couple responses, I had begun to think (again, had only limited access to actually play on the weekend) of how I could maybe use the get_token method, but wasn't sure - you provided some proof.

    Thanks again to everyone!

    [Jon]