Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

In the documentation for the HTML::Parser module it says that in order for the parser to do anything interesting, that one must create a subclass where you override the functions...

The one I'm interesteded in is $self->text($text). I'm just a bit confused as to how to use it to look at the text in the HTML document.

How do I do the overriding? I'm used to it in C++, but this is just a little confusing.

Thanks,
Sam

Replies are listed 'Best First'.
Re: Overriding via a subclass?
by chromatic (Archbishop) on Sep 25, 2000 at 09:53 UTC
    It's pretty simple. All you need to do is say that your module isa HTML::Parser:
    package MyParser; use HTML::Parser; @MyParser::ISA = qw( HTML::Parser ); sub text { # do what you will here }
    Methods called on instances of your class that aren't found in your package will be found in parent classes (Perl examines the contents of your @ISA). If you provide your own, it won't go loooking up the inheritance chain.
Re: Overriding via a subclass?
by dchetlin (Friar) on Sep 25, 2000 at 18:10 UTC
    You may have an old version of HTML::Parser. As of version 3, you do not have to subclass it to get interesting things to happen.

    use HTML::Parser; my $parser = HTML::Parser::->new(api_version => 3); $parser->handler(text => sub { print "This is the text: $_[0]" }, "text"); $parser->parse($html_to_parse);

    Subclassing can still be done, but there's rarely a reason to do it. This way is much more flexible.

    -dlc