creating subclasses / HTML::Parser 
Author Message
 creating subclasses / HTML::Parser

: I'm trying to use HTML::Parser to spit out HTML comments.  The perldoc
: for it says:

:        In order to make the parser do anything interesting, you
:        must make a subclass where you override one or more of the
:        following methods as appropriate

: and then:

:        $self->comment($comment)
:            This method is called as comments are recognized.  The
:            leading and trailing "--" sequences have been stripped
:            off the comment text.

: so i wrote this script:

[snip]

: but it doesn't do anything.  it never prints "hi there i'm alive" so i'm
: guessing that i'm not properly creating the subclass.  

Good guess ;-)

You make a new package for your derived class (myParser), and

---------------------
#!/usr/bin/perl -w

require HTML::Parser;

######################################

sub comment {
  my $this = shift;       # uninteresting, don't want to print this ;-)
  print "hi there i'm alive\n";

Quote:
}

######################################
package main;

$html=<<ENDHTML;
<HTML>
<BODY>
<!-- leave me alone. I'm a comment -->
</BODY>
</HTML>
ENDHTML

$p = myParser->new;   # call my own class with comment() overridden
$p->parse($html);
---------------------

--
    Tad McClellan                          SGML Consulting

    Fort Worth, Texas



Fri, 16 Jun 2000 03:00:00 GMT  
 creating subclasses / HTML::Parser

Tad> You make a new package for your derived class (myParser), and

[example using two files deleted]

You can also do the subclassing all within one program, as I've
illustrated in a few of my WebTechniques columns, archived online at:

        http://www.stonehenge.com/merlyn/WebTechniques/

In particular, use the search engine on that page to look for all

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $20,990.69 collected, $186,159.85 spent; just 245 more days

--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me



Sat, 17 Jun 2000 03:00:00 GMT  
 creating subclasses / HTML::Parser

Quote:

> so i wrote this script:

> #!/usr/local/bin/perl -w

> require HTML::Parser;

> . . .

> sub comment {
>   print "hi there i'm alive\n";

> }

> $p = HTML::Parser->new;
> $p->{'comment'} = \&comment;
> $p->parse($html);
> exit;

> but it doesn't do anything.  it never prints "hi there i'm alive" so i'm
> guessing that i'm not properly creating the subclass.  

Dude! Not creating a subclass at all is about as improper as you can
get.

To create a subclass of HTML::Parser you can do this:

require HTML::Parser;

package MyParser;              # this will be the derived "class".

  sub comment {  # this definition overrides the one in HTML::Parser.
     . . .
  }

package main;
my $p = new MyParser . . .;

John Porter



Sat, 17 Jun 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. creating a subclass of HTML::Parser

2. HTML::TokeParser vs HTML::Parser subclass?

3. subclass HTML::Parser

4. HTML Parsers/ HTML::Parser

5. Problems creating a new module based off HTML::Parser

6. Creating ad-hoc subclasses at runtime?

7. HTML::Parse v. HTML::Parser

8. HTML::Parser: Parsing HTML tables and Frames???

9. HTML::Parser - HTML::TableExtract Help

10. Removing HTML tags with HTML::Parser - my code is bloated

11. Incompatible versions of HTML::Parser and HTML::TreeBuilder

12. HTML::Parser and HTML::Entites

 

 
Powered by phpBB® Forum Software