in reply to Regular expression problem

This is a case of "don't use regexes to parse HTML" :)

#!perl -w use strict; use HTML::Parser; my $html = <<'END_HTML'; <html> <body> <form action="/foo.pl" method="post"> Username: <input type="text" name="user" onClick="alert('You clicked me!')" /><br /> Password: <input type="password" name="passwd" onChange="alert('You changed me!')" /> </form> </body> </html> END_HTML my $parser = HTML::Parser->new( start_h => [ \&_parser_starttag, 'tagname, attr' ] ); $parser->parse( $html ); sub _parser_starttag { my ($tag, $attr) = @_; if ( exists $attr->{'onchange'} ) { warn( "<$tag> - onChange: ", $attr->{'onchange'}, "\n" ); } elsif ( exists $attr->{'onclick'} ) { warn( "<$tag> - onClick: ", $attr->{'onclick'}, "\n" ); } }

Replies are listed 'Best First'.
Re^2: Regular expression problem
by jockel (Beadle) on Jun 09, 2004 at 09:05 UTC
    Thanks for your answer..

    But this time it wasn't about html tags,,
    It was just my way of sending extra information
    to a sub that prints html code.

    /jocke

      Still, there has got to be a better way than what you are doing. Perhaps you should use the named-parameter-list style of passing parameters to subroutines. Perhaps something like this?

      print_html( onChange => qq{ document.approveform.command.value='approve'; document.approveform.submit(); }, onClick => qq{ if (this.disabled) { alert('some text!'); } } ); sub print_html { my %param = @_; print "onChange: $param{'onChange'}\n" if ( exists $param{'onChange'} ); print "onClick: $param{'onClick'}\n" if ( exists $param{'onClick'} ); }

        Bend like a soft reed in the wind:

        sub print_html { my %param = @_; exists $param{$_} and print "$_: $param{$_}\n" for qw(onChange onClick); }

        jeffa

        I guess i should note for the record that this was my 2112th node, a Rush reference.