HTML::Parser - SGML parser class |
HTML::Parser - SGML parser class
require HTML::Parser; $p = HTML::Parser->new; # should really a be subclass $p->parse($chunk1); $p->parse($chunk2); #... $p->eof; # signal end of document
# Parse directly from file $p->parse_file("foo.html"); # or open(F, "foo.html") || die; $p->parse_file(\*F);
The HTML::Parser
will tokenize an HTML document when the parse()
method is called by invoking various callback methods. The document to
be parsed can be supplied in arbitrary chunks.
The external interface the an HTML::Parser is:
eof()
to flush any remaining buffered
text. The return value is a reference to the parser object.
parse_file()
is a reference to the parser object.
strict_comment()
method with a TRUE
argument.
The return value from strict_comment()
is the old attribute value.
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:
declaration($decl)
text($text)
A sequence of text in the HTML document can be broken between several invocations of $self->text. The parser will make sure that it does not break a word or a sequence of spaces between two invocations of $self->text().
comment($comment)
The default implementation of these methods do nothing, i.e., the tokens are just ignored.
There is really nothing in the basic parser that is HTML specific, so
it is likely that the parser can parse other kinds of SGML documents.
SGML has many obscure features (not implemented by this module) that
prevent us from renaming this module as SGML::Parser
.
The parser is fairly inefficient if the chunks passed to $p->parse()
are too big. The reason is probably that perl ends up with a lot of
character copying when tokens are removed from the beginning of the
strings. A chunk size of about 256-512 bytes was optimal in a test I
made with some real world HTML documents. (The parser was about 3
times slower with a chunk size of 20K).
the HTML::Entities manpage, the HTML::TokeParser manpage, the HTML::Filter manpage, the HTML::HeadParser manpage, the HTML::LinkExtor manpage
the HTML::TreeBuilder manpage (part of the HTML-Tree distribution)
Copyright 1996-1999 Gisle Aas. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
HTML::Parser - SGML parser class |