Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man3 / HTML::Parser.3
.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. | will give a
.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
.\" expand to `' in nroff, nothing in troff, for use with C<>.
.tr \(*W-|\(bv\*(Tr
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
'br\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. nr % 0
. rr F
.\}
.\"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "Parser 3"
.TH Parser 3 "2003-04-16" "perl v5.8.0" "User Contributed Perl Documentation"
.SH "NAME"
HTML::Parser \- HTML parser class
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\& use HTML::Parser ();
.Ve
.PP
.Vb 6
\& # Create parser object
\& $p = HTML::Parser->new( api_version => 3,
\& start_h => [\e&start, "tagname, attr"],
\& end_h => [\e&end, "tagname"],
\& marked_sections => 1,
\& );
.Ve
.PP
.Vb 5
\& # Parse document text chunk by chunk
\& $p->parse($chunk1);
\& $p->parse($chunk2);
\& #...
\& $p->eof; # signal end of document
.Ve
.PP
.Vb 5
\& # Parse directly from file
\& $p->parse_file("foo.html");
\& # or
\& open(F, "foo.html") || die;
\& $p->parse_file(*F);
.Ve
.PP
HTML::Parser version 2 style subclassing and method callbacks:
.PP
.Vb 3
\& {
\& package MyParser;
\& use base 'HTML::Parser';
.Ve
.PP
.Vb 4
\& sub start {
\& my($self, $tagname, $attr, $attrseq, $origtext) = @_;
\& #...
\& }
.Ve
.PP
.Vb 4
\& sub end {
\& my($self, $tagname, $origtext) = @_;
\& #...
\& }
.Ve
.PP
.Vb 5
\& sub text {
\& my($self, $origtext, $is_cdata) = @_;
\& #...
\& }
\& }
.Ve
.PP
.Vb 2
\& my $p = MyParser->new;
\& $p->parse_file("foo.html");
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
Objects of the \f(CW\*(C`HTML::Parser\*(C'\fR class will recognize markup and
separate it from plain text (alias data content) in \s-1HTML\s0
documents. As different kinds of markup and text are recognized, the
corresponding event handlers are invoked.
.PP
\&\f(CW\*(C`HTML::Parser\*(C'\fR in not a generic \s-1SGML\s0 parser. We have tried to
make it able to deal with the \s-1HTML\s0 that is actually \*(L"out there\*(R", and
it normally parses as closely as possible to the way the popular web
browsers do it instead of strictly following one of the many \s-1HTML\s0
specifications from W3C. Where there is disagreement there is often
an option that you can enable to get the official behaviour.
.PP
The document to be parsed may be supplied in arbitrary chunks. This
makes on-the-fly parsing as documents are received from the network
possible.
.PP
If event driven parsing does not feel right for your application, you
might want to use \f(CW\*(C`HTML::PullParser\*(C'\fR. It is a
\&\f(CW\*(C`HTML::Parser\*(C'\fR subclass that allows a more conventional program
structure.
.SH "METHODS"
.IX Header "METHODS"
The following method is used to construct a new \f(CW\*(C`HTML::Parser\*(C'\fR object:
.ie n .IP "$p = HTML::Parser\->new( %options_and_handlers )" 4
.el .IP "$p = HTML::Parser\->new( \f(CW%options_and_handlers\fR )" 4
.IX Item "$p = HTML::Parser->new( %options_and_handlers )"
This class method creates a new \f(CW\*(C`HTML::Parser\*(C'\fR object and
returns it. Key/value pair arguments may be provided to assign event
handlers or initialize parser options. The handlers and parser
options can also be set or modified later by method calls described below.
.Sp
If a top level key is in the form \*(L"<event>_h\*(R" (e.g., \*(L"text_h\*(R"} then it
assigns a handler to that event, otherwise it initializes a parser
option. The event handler specification value must be an array
reference. Multiple handlers may also be assigned with the 'handlers
=> [%handlers]' option. See examples below.
.Sp
If \fInew()\fR is called without any arguments, it will create a parser that
uses callback methods compatible with version 2 of \f(CW\*(C`HTML::Parser\*(C'\fR.
See the section on \*(L"version 2 compatibility\*(R" below for details.
.Sp
Special constructor option 'api_version => 2' can be used to
initialize version 2 callbacks while still setting other options and
handlers. The 'api_version => 3' option can be used if you don't want
to set any options and don't want to fall back to v2 compatible
mode.
.Sp
Examples:
.Sp
.Vb 2
\& $p = HTML::Parser->new(api_version => 3,
\& text_h => [ sub {...}, "dtext" ]);
.Ve
.Sp
This creates a new parser object with a text event handler subroutine
that receives the original text with general entities decoded.
.Sp
.Vb 2
\& $p = HTML::Parser->new(api_version => 3,
\& start_h => [ 'my_start', "self,tokens" ]);
.Ve
.Sp
This creates a new parser object with a start event handler method
that receives the \f(CW$p\fR and the tokens array.
.Sp
.Vb 4
\& $p = HTML::Parser->new(api_version => 3,
\& handlers => { text => [\e@array, "event,text"],
\& comment => [\e@array, "event,text"],
\& });
.Ve
.Sp
This creates a new parser object that stores the event type and the
original text in \f(CW@array\fR for text and comment events.
.PP
The following methods feed the \s-1HTML\s0 document
to the \f(CW\*(C`HTML::Parser\*(C'\fR object:
.ie n .IP "$p\->parse( $string )" 4
.el .IP "$p\->parse( \f(CW$string\fR )" 4
.IX Item "$p->parse( $string )"
Parse \f(CW$string\fR as the next chunk of the \s-1HTML\s0 document. The return
value is normally a reference to the parser object (i.e. \f(CW$p\fR).
Handlers invoked should not attempt modify the \f(CW$string\fR in-place until
\&\f(CW$p\fR\->parse returns.
.Sp
If an invoked event handler aborts parsing by calling \f(CW$p\fR\->eof, then
\&\f(CW$p\fR\->\fIparse()\fR will return a \s-1FALSE\s0 value.
.ie n .IP "$p\->parse( $code_ref )" 4
.el .IP "$p\->parse( \f(CW$code_ref\fR )" 4
.IX Item "$p->parse( $code_ref )"
If a code reference is passed in as the argument to parse then the
chunks to parse is obtained by invoking this function repeatedly.
Parsing continues until the function returns an empty (or undefined)
result. When this happens \f(CW$p\fR\->eof is automatically signalled.
.Sp
Parsing will also abort if one of the event handlers call \f(CW$p\fR\->eof.
.Sp
The effect of this is the same as:
.Sp
.Vb 8
\& while (1) {
\& my $chunk = &$code_ref();
\& if (!defined($chunk) || !length($chunk)) {
\& $p->eof;
\& return $p;
\& }
\& $p->parse($chunk) || return undef;
\& }
.Ve
.Sp
But it is more efficient as this loop runs internally in \s-1XS\s0 code.
.ie n .IP "$p\->parse_file( $file )" 4
.el .IP "$p\->parse_file( \f(CW$file\fR )" 4
.IX Item "$p->parse_file( $file )"
Parse text directly from a file. The \f(CW$file\fR argument can be a
filename, an open file handle, or a reference to a an open file
handle.
.Sp
If \f(CW$file\fR contains a filename and the file can't be opened, then the
method returns an undefined value and $! tells why it failed.
Otherwise the return value is a reference to the parser object.
.Sp
If a file handle is passed as the \f(CW$file\fR argument, then the file will
normally be read until \s-1EOF\s0, but not closed.
.Sp
If an invoked event handler aborts parsing by calling \f(CW$p\fR\->eof,
then \f(CW$p\fR\->\fIparse_file()\fR may not have read the entire file.
.Sp
On systems with multi-byte line terminators, the values passed for the
offset and length argspecs may be too low if \fIparse_file()\fR is called on
a file handle that is not in binary mode.
.Sp
If a filename is passed in, then \fIparse_file()\fR will open the file in
binary mode.
.IP "$p\->eof" 4
.IX Item "$p->eof"
Signals the end of the \s-1HTML\s0 document. Calling the \f(CW$p\fR\->eof method
outside a handler callback will flush any remaining buffered text
(which triggers the \f(CW\*(C`text\*(C'\fR event if there is any remaining text).
.Sp
Calling \f(CW$p\fR\->eof inside a handler will terminate parsing at that point
and cause \f(CW$p\fR\->parse to return a \s-1FALSE\s0 value. This also terminates
parsing by \f(CW$p\fR\->\fIparse_file()\fR.
.Sp
After \f(CW$p\fR\->eof has been called, the \fIparse()\fR and \fIparse_file()\fR methods
can be invoked to feed new documents with the parser object.
.Sp
The return value from \fIeof()\fR is a reference to the parser object.
.PP
Most parser options are controlled by boolean attributes.
Each boolean attribute is enabled by calling the corresponding method
with a \s-1TRUE\s0 argument and disabled with a \s-1FALSE\s0 argument. The
attribute value is left unchanged if no argument is given. The return
value from each method is the old attribute value.
.PP
Methods that can be used to get and/or set parser options are:
.IP "$p\->strict_comment( [$bool] )" 4
.IX Item "$p->strict_comment( [$bool] )"
By default, comments are terminated by the first occurrence of \*(L"\-\->\*(R".
This is the behaviour of most popular browsers (like Netscape and
\&\s-1MSIE\s0), but it is not correct according to the official \s-1HTML\s0
standard. Officially, you need an even number of \*(L"\-\-\*(R" tokens before
the closing \*(L">\*(R" is recognized and there may not be anything but
whitespace between an even and an odd \*(L"\-\-\*(R".
.Sp
The official behaviour is enabled by enabling this attribute.
.IP "$p\->strict_names( [$bool] )" 4
.IX Item "$p->strict_names( [$bool] )"
By default, almost anything is allowed in tag and attribute names.
This is the behaviour of most popular browsers and allows us to parse
some broken tags with invalid attr values like:
.Sp
.Vb 1
\& <IMG SRC=newprevlstGr.gif ALT=[PREV LIST] BORDER=0>
.Ve
.Sp
By default, \*(L"\s-1LIST\s0]\*(R" is parsed as a boolean attribute, not as
part of the \s-1ALT\s0 value as was clearly intended. This is also what
Netscape sees.
.Sp
The official behaviour is enabled by enabling this attribute. If
enabled, it will cause the tag above to be reported as text
since \*(L"\s-1LIST\s0]\*(R" is not a legal attribute name.
.ie n .IP "$p\->boolean_attribute_value( $val )" 4
.el .IP "$p\->boolean_attribute_value( \f(CW$val\fR )" 4
.IX Item "$p->boolean_attribute_value( $val )"
This method sets the value reported for boolean attributes inside \s-1HTML\s0
start tags. By default, the name of the attribute is also used as its
value. This affects the values reported for \f(CW\*(C`tokens\*(C'\fR and \f(CW\*(C`attr\*(C'\fR
argspecs.
.IP "$p\->xml_mode( [$bool] )" 4
.IX Item "$p->xml_mode( [$bool] )"
Enabling this attribute changes the parser to allow some \s-1XML\s0
constructs such as \fIempty element tags\fR and \fI\s-1XML\s0 processing
instructions\fR. It disables forcing tag and attribute names to lower
case when they are reported by the \f(CW\*(C`tagname\*(C'\fR and \f(CW\*(C`attr\*(C'\fR argspecs,
and suppress special treatment of elements that are parsed as \s-1CDATA\s0
for \s-1HTML\s0.
.Sp
\&\fIEmpty element tags\fR look like start tags, but end with the character
sequence \*(L"/>\*(R". When recognized by \f(CW\*(C`HTML::Parser\*(C'\fR they cause an
artificial end event in addition to the start event. The \f(CW\*(C`text\*(C'\fR for
the artificial end event will be empty and the \f(CW\*(C`tokenpos\*(C'\fR array will
be undefined even though the only element in the token array will have
the correct tag name.
.Sp
\&\fI\s-1XML\s0 processing instructions\fR are terminated by \*(L"?>\*(R" instead of a
simple \*(L">\*(R" as is the case for \s-1HTML\s0.
.IP "$p\->unbroken_text( [$bool] )" 4
.IX Item "$p->unbroken_text( [$bool] )"
By default, blocks of text are given to the text handler as soon as
possible (but the parser makes sure to always break text at the
boundary between whitespace and non-whitespace so single words and
entities always can be decoded safely). This might create breaks that
make it hard to do transformations on the text. When this attribute is
enabled, blocks of text are always reported in one piece. This will
delay the text event until the following (non\-text) event has been
recognized by the parser.
.Sp
Note that the \f(CW\*(C`offset\*(C'\fR argspec will give you the offset of the first
segment of text and \f(CW\*(C`length\*(C'\fR is the combined length of the segments.
Since there might be ignored tags in between, these numbers can't be
used to directly index in the original document file.
.IP "$p\->marked_sections( [$bool] )" 4
.IX Item "$p->marked_sections( [$bool] )"
By default, section markings like <![CDATA[...]]> are treated like
ordinary text. When this attribute is enabled section markings are
honoured.
.Sp
There are currently no events associated with the marked section
markup, but the text can be returned as \f(CW\*(C`skipped_text\*(C'\fR.
.IP "$p\->attr_encoded( [$bool] )" 4
.IX Item "$p->attr_encoded( [$bool] )"
By default, the \f(CW\*(C`attr\*(C'\fR and \f(CW@attr\fR argspecs will have general
entities for attribute values decoded. Enabling this attribute leaves
entities alone.
.IP "$p\->case_sensitive( [$bool] )" 4
.IX Item "$p->case_sensitive( [$bool] )"
By default, tagnames and attribute names are down\-cased. Enabling this
attribute leave them as found in the \s-1HTML\s0 source document.
.PP
As markup and text is recognized, handlers are invoked. The following
method is used to set up handlers for different events:
.IP "$p\->handler( event => \e&subroutine, argspec )" 4
.IX Item "$p->handler( event => &subroutine, argspec )"
.PD 0
.IP "$p\->handler( event => method_name, argspec )" 4
.IX Item "$p->handler( event => method_name, argspec )"
.IP "$p\->handler( event => \e@accum, argspec )" 4
.IX Item "$p->handler( event => @accum, argspec )"
.ie n .IP "$p\->handler( event => """" );" 4
.el .IP "$p\->handler( event => ``'' );" 4
.IX Item "$p->handler( event => """" );"
.IP "$p\->handler( event => undef );" 4
.IX Item "$p->handler( event => undef );"
.IP "$p\->handler( event );" 4
.IX Item "$p->handler( event );"
.PD
This method assigns a subroutine, method, or array to handle an event.
.Sp
Event is one of \f(CW\*(C`text\*(C'\fR, \f(CW\*(C`start\*(C'\fR, \f(CW\*(C`end\*(C'\fR, \f(CW\*(C`declaration\*(C'\fR, \f(CW\*(C`comment\*(C'\fR,
\&\f(CW\*(C`process\*(C'\fR, \f(CW\*(C`start_document\*(C'\fR, \f(CW\*(C`end_document\*(C'\fR or \f(CW\*(C`default\*(C'\fR.
.Sp
\&\fISubroutine\fR is a reference to a subroutine which is called to handle
the event.
.Sp
\&\fIMethod_name\fR is the name of a method of \f(CW$p\fR which is called to handle
the event.
.Sp
\&\fIAccum\fR is a array that will hold the event information as
sub\-arrays.
.Sp
If the second argument is "", the event is ignored.
If it is undef, the default handler is invoked for the event.
.Sp
\&\fIArgspec\fR is a string that describes the information to be reported
for the event. Any requested information that does not apply to a
specific event is passed as \f(CW\*(C`undef\*(C'\fR. If argspec is omitted, then it
is left unchanged since last update.
.Sp
The return value from \f(CW$p\fR\->handle is the old callback routine or a
reference to the accumulator array.
.Sp
Any return values from handler callback routines/methods are always
ignored. A handler callback can request parsing to be aborted by
invoking the \f(CW$p\fR\->eof method. A handler callback is not allowed to
invoke the \f(CW$p\fR\->\fIparse()\fR or \f(CW$p\fR\->\fIparse_file()\fR method. An exception will
be raised if it tries.
.Sp
Examples:
.Sp
.Vb 1
\& $p->handler(start => "start", 'self, attr, attrseq, text' );
.Ve
.Sp
This causes the \*(L"start\*(R" method of object \f(CW$p\fR to be called for 'start' events.
The callback signature is \f(CW$p\fR\->start(\e%attr, \e@attr_seq, \f(CW$text\fR).
.Sp
.Vb 1
\& $p->handler(start => \e&start, 'attr, attrseq, text' );
.Ve
.Sp
This causes subroutine \fIstart()\fR to be called for 'start' events.
The callback signature is start(\e%attr, \e@attr_seq, \f(CW$text\fR).
.Sp
.Vb 1
\& $p->handler(start => \e@accum, '"S", attr, attrseq, text' );
.Ve
.Sp
This causes 'start' event information to be saved in \f(CW@accum\fR.
The array elements will be ['S', \e%attr, \e@attr_seq, \f(CW$text\fR].
.Sp
.Vb 1
\& $p->handler(start => "");
.Ve
.Sp
This causes 'start' events to be ignored. It also supresses
invokations of any default handler for start events. It is in most
cases equivalent to \f(CW$p\fR\->handler(start => sub {}), but is more
efficient. It is different from the empty-sub-handler in that
\&\f(CW\*(C`skipped_text\*(C'\fR is not reset by it.
.Sp
.Vb 1
\& $p->handler(start => undef);
.Ve
.Sp
This causes no handler to be assosiated with start events.
If there is a default handler it will be invoked.
.PP
Filters based on tags can be set up to limit the number of events
reported. The main bottleneck during parsing is often the huge number
of callbacks made from the parser. Applying filters can improve
performance significantly.
.PP
The following methods control filters:
.IP "$p\->ignore_tags( \s-1TAG\s0, ... )" 4
.IX Item "$p->ignore_tags( TAG, ... )"
Any \f(CW\*(C`start\*(C'\fR and \f(CW\*(C`end\*(C'\fR events involving any of the tags given are
suppressed.
.IP "$p\->report_tags( \s-1TAG\s0, ... )" 4
.IX Item "$p->report_tags( TAG, ... )"
Any \f(CW\*(C`start\*(C'\fR and \f(CW\*(C`end\*(C'\fR events involving any of the tags \fInot\fR given
are suppressed.
.IP "$p\->ignore_elements( \s-1TAG\s0, ... )" 4
.IX Item "$p->ignore_elements( TAG, ... )"
Both the \f(CW\*(C`start\*(C'\fR and the \f(CW\*(C`end\*(C'\fR event as well as any events that
would be reported in between are suppressed. The ignored elements can
contain nested occurences of itself. Example:
.Sp
.Vb 1
\& $p->ignore_elements(qw(script style));
.Ve
.Sp
The \f(CW\*(C`script\*(C'\fR and \f(CW\*(C`style\*(C'\fR tags will always nest properly since their
content is parsed in \s-1CDATA\s0 mode. For most other tags
\&\f(CW\*(C`ignore_elements\*(C'\fR must be used with caution since \s-1HTML\s0 is often not
\&\fIwell formed\fR.
.Sh "Argspec"
.IX Subsection "Argspec"
Argspec is a string containing a comma separated list that describes
the information reported by the event. The following argspec
identifier names can be used:
.ie n .IP """self""" 4
.el .IP "\f(CWself\fR" 4
.IX Item "self"
Self causes the current object to be passed to the handler. If the
handler is a method, this must be the first element in the argspec.
.Sp
An alternative to passing self as an argspec is to register closures
that capture \f(CW$self\fR by themselves as handlers. Unfortunately this
creates a circular references which prevents the HTML::Parser object
from being garbage collected. Using the \f(CW\*(C`self\*(C'\fR argspec avoids this
problem.
.ie n .IP """tokens""" 4
.el .IP "\f(CWtokens\fR" 4
.IX Item "tokens"
Tokens causes a reference to an array of token strings to be passed.
The strings are exactly as they were found in the original text,
no decoding or case changes are applied.
.Sp
For \f(CW\*(C`declaration\*(C'\fR events, the array contains each word, comment, and
delimited string starting with the declaration type.
.Sp
For \f(CW\*(C`comment\*(C'\fR events, this contains each sub\-comment. If
\&\f(CW$p\fR\->strict_comments is disabled, there will be only one sub\-comment.
.Sp
For \f(CW\*(C`start\*(C'\fR events, this contains the original tag name followed by
the attribute name/value pairs. The value of boolean attributes will
be either the value set by \f(CW$p\fR\->boolean_attribute_value or the
attribute name if no value has been set by
\&\f(CW$p\fR\->boolean_attribute_value.
.Sp
For \f(CW\*(C`end\*(C'\fR events, this contains the original tag name (always one token).
.Sp
For \f(CW\*(C`process\*(C'\fR events, this contains the process instructions (always one
token).
.Sp
This passes \f(CW\*(C`undef\*(C'\fR for \f(CW\*(C`text\*(C'\fR events.
.ie n .IP """tokenpos""" 4
.el .IP "\f(CWtokenpos\fR" 4
.IX Item "tokenpos"
Tokenpos causes a reference to an array of token positions to be
passed. For each string that appears in \f(CW\*(C`tokens\*(C'\fR, this array
contains two numbers. The first number is the offset of the start of
the token in the original \f(CW\*(C`text\*(C'\fR and the second number is the length
of the token.
.Sp
Boolean attributes in a \f(CW\*(C`start\*(C'\fR event will have (0,0) for the
attribute value offset and length.
.Sp
This passes undef if there are no tokens in the event (e.g., \f(CW\*(C`text\*(C'\fR)
and for artifical \f(CW\*(C`end\*(C'\fR events triggered by empty element tags.
.Sp
If you are using these offsets and lengths to modify \f(CW\*(C`text\*(C'\fR, you
should either work from right to left, or be very careful to calculate
the changes to the offsets.
.ie n .IP """token0""" 4
.el .IP "\f(CWtoken0\fR" 4
.IX Item "token0"
Token0 causes the original text of the first token string to be
passed. This should always be the same as \f(CW$tokens\fR\->[0].
.Sp
For \f(CW\*(C`declaration\*(C'\fR events, this is the declaration type.
.Sp
For \f(CW\*(C`start\*(C'\fR and \f(CW\*(C`end\*(C'\fR events, this is the tag name.
.Sp
For \f(CW\*(C`process\*(C'\fR and non-strict \f(CW\*(C`comment\*(C'\fR events, this is everything
inside the tag.
.Sp
This passes undef if there are no tokens in the event.
.ie n .IP """tagname""" 4
.el .IP "\f(CWtagname\fR" 4
.IX Item "tagname"
This is the element name (or \fIgeneric identifier\fR in \s-1SGML\s0 jargon) for
start and end tags. Since \s-1HTML\s0 is case insensitive this name is
forced to lower case to ease string matching.
.Sp
Since \s-1XML\s0 is case sensitive, the tagname case is not changed when
\&\f(CW\*(C`xml_mode\*(C'\fR is enabled. Same happens if the \f(CW\*(C`case_sensitive\*(C'\fR attribute
is set.
.Sp
The declaration type of declaration elements is also passed as a tagname,
even if that is a bit strange.
In fact, in the current implementation tagname is
identical to \f(CW\*(C`token0\*(C'\fR except that the name may be forced to lower case.
.ie n .IP """tag""" 4
.el .IP "\f(CWtag\fR" 4
.IX Item "tag"
Same as \f(CW\*(C`tagname\*(C'\fR, but prefixed with \*(L"/\*(R" if it belongs to an \f(CW\*(C`end\*(C'\fR
event and \*(L"!\*(R" for a declaration. The \f(CW\*(C`tag\*(C'\fR does not have any prefix
for \f(CW\*(C`start\*(C'\fR events, and is in this case identical to \f(CW\*(C`tagname\*(C'\fR.
.ie n .IP """attr""" 4
.el .IP "\f(CWattr\fR" 4
.IX Item "attr"
Attr causes a reference to a hash of attribute name/value pairs to be
passed.
.Sp
Boolean attributes' values are either the value set by
\&\f(CW$p\fR\->boolean_attribute_value or the attribute name if no value has been
set by \f(CW$p\fR\->boolean_attribute_value.
.Sp
This passes undef except for \f(CW\*(C`start\*(C'\fR events.
.Sp
Unless \f(CW\*(C`xml_mode\*(C'\fR or \f(CW\*(C`case_sensitive\*(C'\fR is enabled, the attribute
names are forced to lower case.
.Sp
General entities are decoded in the attribute values and
one layer of matching quotes enclosing the attribute values are removed.
.ie n .IP """attrseq""" 4
.el .IP "\f(CWattrseq\fR" 4
.IX Item "attrseq"
Attrseq causes a reference to an array of attribute names to be
passed. This can be useful if you want to walk the \f(CW\*(C`attr\*(C'\fR hash in
the original sequence.
.Sp
This passes undef except for \f(CW\*(C`start\*(C'\fR events.
.Sp
Unless \f(CW\*(C`xml_mode\*(C'\fR or \f(CW\*(C`case_sensitive\*(C'\fR is enabled, the attribute
names are forced to lower case.
.ie n .IP "@attr" 4
.el .IP "\f(CW@attr\fR" 4
.IX Item "@attr"
Basically same as \f(CW\*(C`attr\*(C'\fR, but keys and values are passed as
individual arguments and the original sequence of the attributes is
kept. The parameters passed will be the same as the \f(CW@attr\fR calculated
here:
.Sp
.Vb 1
\& @attr = map { $_ => $attr->{$_} } @$attrseq;
.Ve
.Sp
assuming \f(CW$attr\fR and \f(CW$attrseq\fR here are the hash and array passed as the
result of \f(CW\*(C`attr\*(C'\fR and \f(CW\*(C`attrseq\*(C'\fR argspecs.
.Sp
This pass no values for events besides \f(CW\*(C`start\*(C'\fR.
.ie n .IP """text""" 4
.el .IP "\f(CWtext\fR" 4
.IX Item "text"
Text causes the source text (including markup element delimiters) to be
passed.
.ie n .IP """dtext""" 4
.el .IP "\f(CWdtext\fR" 4
.IX Item "dtext"
Dtext causes the decoded text to be passed. General entities are
automatically decoded unless the event was inside a \s-1CDATA\s0 section or
was between literal start and end tags (\f(CW\*(C`script\*(C'\fR, \f(CW\*(C`style\*(C'\fR, \f(CW\*(C`xmp\*(C'\fR,
and \f(CW\*(C`plaintext\*(C'\fR).
.Sp
The Unicode character set is assumed for entity decoding. With perl
version < 5.7.1 only the Latin1 range is supported, and entities for
characters outside the 0..255 range is left unchanged.
.Sp
This passes undef except for \f(CW\*(C`text\*(C'\fR events.
.ie n .IP """is_cdata""" 4
.el .IP "\f(CWis_cdata\fR" 4
.IX Item "is_cdata"
Is_cdata causes a \s-1TRUE\s0 value to be passed if the event is inside a \s-1CDATA\s0
section or is between literal start and end tags (\f(CW\*(C`script\*(C'\fR,
\&\f(CW\*(C`style\*(C'\fR, \f(CW\*(C`xmp\*(C'\fR, and \f(CW\*(C`plaintext\*(C'\fR).
.Sp
When the flag is \s-1FALSE\s0 for a text event, then you should normally
either use \f(CW\*(C`dtext\*(C'\fR or decode the entities yourself before the text is
processed further.
.ie n .IP """skipped_text""" 4
.el .IP "\f(CWskipped_text\fR" 4
.IX Item "skipped_text"
Skipped_text returns the concatenated text of all the events that has
been skipped since the last time an event was reported. Events might
be skipped because no handler is registered for them or because some
filter applies. Skipped text also include marked section markup,
since there is no events that can catch them.
.Sp
If an \f(CW""\fR\-handler is registered for an event, then the text for this
event is not included in \f(CW\*(C`skipped_text\*(C'\fR. Skipped text both before
and after the \f(CW""\fR\-event is included in the next reported
\&\f(CW\*(C`skipped_text\*(C'\fR.
.ie n .IP """offset""" 4
.el .IP "\f(CWoffset\fR" 4
.IX Item "offset"
Offset causes the byte position in the \s-1HTML\s0 document of the start of
the event to be passed. The first byte in the document is 0.
.ie n .IP """length""" 4
.el .IP "\f(CWlength\fR" 4
.IX Item "length"
Length causes the number of bytes of the source text of the event to
be passed.
.ie n .IP """offset_end""" 4
.el .IP "\f(CWoffset_end\fR" 4
.IX Item "offset_end"
Offset_end causes the byte position in the \s-1HTML\s0 document of the end of
the event to be passed. This is the same as \f(CW\*(C`offset\*(C'\fR + \f(CW\*(C`length\*(C'\fR.
.ie n .IP """event""" 4
.el .IP "\f(CWevent\fR" 4
.IX Item "event"
Event causes the event name to be passed.
.Sp
The event name is one of \f(CW\*(C`text\*(C'\fR, \f(CW\*(C`start\*(C'\fR, \f(CW\*(C`end\*(C'\fR, \f(CW\*(C`declaration\*(C'\fR,
\&\f(CW\*(C`comment\*(C'\fR, \f(CW\*(C`process\*(C'\fR, \f(CW\*(C`start_document\*(C'\fR, \f(CW\*(C`end_document\*(C'\fR or \f(CW\*(C`default\*(C'\fR.
.ie n .IP """line""" 4
.el .IP "\f(CWline\fR" 4
.IX Item "line"
Line causes the line number of the start of the event to be passed.
The first line in the document is 1. Line counting doesn't start
until at least one handler requests this value to be reported.
.ie n .IP """column""" 4
.el .IP "\f(CWcolumn\fR" 4
.IX Item "column"
Column causes the column number of the start of the event to be passed.
The first column on a line is 0.
.ie n .IP "'...'" 4
.el .IP "\f(CW'...'\fR" 4
.IX Item "'...'"
A literal string of 0 to 255 characters enclosed
in single (') or double (") quotes is passed as entered.
.ie n .IP """undef""" 4
.el .IP "\f(CWundef\fR" 4
.IX Item "undef"
Pass an undefined value. Useful as padding where the same handler
routine is registered for multiple events.
.PP
The whole argspec string can be wrapped up in \f(CW'@{...}'\fR to signal
that resulting event array should be flatten. This only makes a
difference if an array reference is used as the handler target.
Consider this example:
.PP
.Vb 2
\& $p->handler(text => [], 'text');
\& $p->handler(text => [], '@{text}']);
.Ve
.PP
With two text events; \f(CW"foo"\fR, \f(CW"bar"\fR; then the first one will end
up with [[\*(L"foo\*(R"], [\*(L"bar\*(R"]] and the second one with [\*(L"foo\*(R", \*(L"bar\*(R"] in
the handler target array.
.Sh "Events"
.IX Subsection "Events"
Handlers for the following events can be registered:
.ie n .IP """text""" 4
.el .IP "\f(CWtext\fR" 4
.IX Item "text"
This event is triggered when plain text (characters) is recognized.
The text may contain multiple lines. A sequence of text may be broken
between several text events unless \f(CW$p\fR\->unbroken_text is enabled.
.Sp
The parser will make sure that it does not break a word or a sequence
of whitespace between two text events.
.ie n .IP """start""" 4
.el .IP "\f(CWstart\fR" 4
.IX Item "start"
This event is triggered when a start tag is recognized.
.Sp
Example:
.Sp
.Vb 1
\& <A HREF="http://www.perl.com/">
.Ve
.ie n .IP """end""" 4
.el .IP "\f(CWend\fR" 4
.IX Item "end"
This event is triggered when an end tag is recognized.
.Sp
Example:
.Sp
.Vb 1
\& </A>
.Ve
.ie n .IP """declaration""" 4
.el .IP "\f(CWdeclaration\fR" 4
.IX Item "declaration"
This event is triggered when a \fImarkup declaration\fR is recognized.
.Sp
For typical \s-1HTML\s0 documents, the only declaration you are
likely to find is <!DOCTYPE ...>.
.Sp
Example:
.Sp
.Vb 2
\& <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
\& "http://www.w3.org/TR/html40/strict.dtd">
.Ve
.Sp
DTDs inside <!DOCTYPE ...> will confuse HTML::Parser.
.ie n .IP """comment""" 4
.el .IP "\f(CWcomment\fR" 4
.IX Item "comment"
This event is triggered when a markup comment is recognized.
.Sp
Example:
.Sp
.Vb 1
\& <!-- This is a comment -- -- So is this -->
.Ve
.ie n .IP """process""" 4
.el .IP "\f(CWprocess\fR" 4
.IX Item "process"
This event is triggered when a processing instructions markup is
recognized.
.Sp
The format and content of processing instructions is system and
application dependent.
.Sp
Examples:
.Sp
.Vb 2
\& <? HTML processing instructions >
\& <? XML processing instructions ?>
.Ve
.ie n .IP """start_document""" 4
.el .IP "\f(CWstart_document\fR" 4
.IX Item "start_document"
This event is triggered before any other events for a new document. A
handler for it can be used to initialize stuff. There is no document
text associated with this event.
.ie n .IP """end_document""" 4
.el .IP "\f(CWend_document\fR" 4
.IX Item "end_document"
This event is triggered when \f(CW$p\fR\->eof called and after any remaining
text is flushed. There is no document text associated with this event.
.ie n .IP """default""" 4
.el .IP "\f(CWdefault\fR" 4
.IX Item "default"
This event is triggered for events that do not have a specific
handler. You can set up a handler for this event to catch stuff you
did not want to catch explicitly.
.SH "VERSION 2 COMPATIBILITY"
.IX Header "VERSION 2 COMPATIBILITY"
When an \f(CW\*(C`HTML::Parser\*(C'\fR object is constructed with no arguments, a set
of handlers is automatically provided that is compatible with the old
HTML::Parser version 2 callback methods.
.PP
This is equivalent to the following method calls:
.PP
.Vb 14
\& $p->handler(start => "start", "self, tagname, attr, attrseq, text");
\& $p->handler(end => "end", "self, tagname, text");
\& $p->handler(text => "text", "self, text, is_cdata");
\& $p->handler(process => "process", "self, token0, text");
\& $p->handler(comment =>
\& sub {
\& my($self, $tokens) = @_;
\& for (@$tokens) {$self->comment($_);}},
\& "self, tokens");
\& $p->handler(declaration =>
\& sub {
\& my $self = shift;
\& $self->declaration(substr($_[0], 2, -1));},
\& "self, text");
.Ve
.PP
Setup of these handlers can also be requested with the \*(L"api_version =>
2\*(R" constructor option.
.SH "SUBCLASSING"
.IX Header "SUBCLASSING"
The \f(CW\*(C`HTML::Parser\*(C'\fR class is subclassable. Parser objects are plain
hashes and \f(CW\*(C`HTML::Parser\*(C'\fR reserves only hash keys that start with
\&\*(L"_hparser\*(R". The parser state can be set up by invoking the \fIinit()\fR
method which takes the same arguments as \fInew()\fR.
.SH "EXAMPLES"
.IX Header "EXAMPLES"
The first simple example shows how you might strip out comments from
an \s-1HTML\s0 document. We achieve this by setting up a comment handler that
does nothing and a default handler that will print out anything else:
.PP
.Vb 4
\& use HTML::Parser;
\& HTML::Parser->new(default_h => [sub { print shift }, 'text'],
\& comment_h => [""],
\& )->parse_file(shift || die) || die $!;
.Ve
.PP
An alternative implementation is:
.PP
.Vb 5
\& use HTML::Parser;
\& HTML::Parser->new(end_document_h => [sub { print shift },
\& 'skipped_text'],
\& comment_h => [""],
\& )->parse_file(shift || die) || die $!;
.Ve
.PP
This will in most cases be much more efficient since only a single
callback will be made.
.PP
The next example prints out the text that is inside the <title>
element of an \s-1HTML\s0 document. Here we start by setting up a start
handler. When it sees the title start tag it enables a text handler
that prints any text found and an end handler that will terminate
parsing as soon as the title end tag is seen:
.PP
.Vb 1
\& use HTML::Parser ();
.Ve
.PP
.Vb 8
\& sub start_handler
\& {
\& return if shift ne "title";
\& my $self = shift;
\& $self->handler(text => sub { print shift }, "dtext");
\& $self->handler(end => sub { shift->eof if shift eq "title"; },
\& "tagname,self");
\& }
.Ve
.PP
.Vb 4
\& my $p = HTML::Parser->new(api_version => 3);
\& $p->handler( start => \e&start_handler, "tagname,self");
\& $p->parse_file(shift || die) || die $!;
\& print "\en";
.Ve
.PP
More examples are found in the \*(L"eg/\*(R" directory of the \f(CW\*(C`HTML\-Parser\*(C'\fR
distribution; the program \f(CW\*(C`hrefsub\*(C'\fR shows how you can edit all links
found in a document and \f(CW\*(C`htextsub\*(C'\fR how to edid the text only; the
program \f(CW\*(C`hstrip\*(C'\fR shows how you can strip out certain tags/elements
and/or attributes; and the program \f(CW\*(C`htext\*(C'\fR show how to obtain the
plain text, but not any script/style content.
.SH "BUGS"
.IX Header "BUGS"
The <style> and <script> sections do not end with the first \*(L"</\*(R", but
need the complete corresponding end tag.
.PP
When the \fIstrict_comment\fR option is enabled, we still recognize
comments where there is something other than whitespace between even
and odd \*(L"\-\-\*(R" markers.
.PP
Once \f(CW$p\fR\->boolean_attribute_value has been set, there is no way to
restore the default behaviour.
.PP
There is currently no way to get both quote characters
into the same literal argspec.
.PP
Empty tags, e.g. \*(L"<>\*(R" and \*(L"</>\*(R", are not recognized. \s-1SGML\s0 allows them
to repeat the previous start tag or close the previous start tag
respecitvely.
.PP
\&\s-1NET\s0 tags, e.g. \*(L"code/.../\*(R" are not recognized. This is an \s-1SGML\s0
shorthand for \*(L"<code>...</code>\*(R".
.PP
Unclosed start or end tags, e.g. \*(L"<tt<b>...</b</tt>\*(R" are not
recognized.
.SH "DIAGNOSTICS"
.IX Header "DIAGNOSTICS"
The following messages may be produced by HTML::Parser. The notation
in this listing is the same as used in perldiag:
.IP "Not a reference to a hash" 4
.IX Item "Not a reference to a hash"
(F) The object blessed into or subclassed from HTML::Parser is not a
hash as required by the HTML::Parser methods.
.ie n .IP "Bad signature in parser state object at %p" 4
.el .IP "Bad signature in parser state object at \f(CW%p\fR" 4
.IX Item "Bad signature in parser state object at %p"
(F) The _hparser_xs_state element does not refer to a valid state structure.
Something must have changed the internal value
stored in this hash element, or the memory has been overwritten.
.IP "_hparser_xs_state element is not a reference" 4
.IX Item "_hparser_xs_state element is not a reference"
(F) The _hparser_xs_state element has been destroyed.
.IP "Can't find '_hparser_xs_state' element in HTML::Parser hash" 4
.IX Item "Can't find '_hparser_xs_state' element in HTML::Parser hash"
(F) The _hparser_xs_state element is missing from the parser hash.
It was either deleted, or not created when the object was created.
.ie n .IP "\s-1API\s0 version %s\fR not supported by HTML::Parser \f(CW%s" 4
.el .IP "\s-1API\s0 version \f(CW%s\fR not supported by HTML::Parser \f(CW%s\fR" 4
.IX Item "API version %s not supported by HTML::Parser %s"
(F) The constructor option 'api_version' with an argument greater than
or equal to 4 is reserved for future extentions.
.IP "Bad constructor option '%s'" 4
.IX Item "Bad constructor option '%s'"
(F) An unknown constructor option key was passed to the \fInew()\fR or
\&\fIinit()\fR methods.
.IP "Parse loop not allowed" 4
.IX Item "Parse loop not allowed"
(F) A handler invoked the \fIparse()\fR or \fIparse_file()\fR method.
This is not permitted.
.IP "marked sections not supported" 4
.IX Item "marked sections not supported"
(F) The \f(CW$p\fR\->\fImarked_sections()\fR method was invoked in a HTML::Parser
module that was compiled without support for marked sections.
.IP "Unknown boolean attribute (%d)" 4
.IX Item "Unknown boolean attribute (%d)"
(F) Something is wrong with the internal logic that set up aliases for
boolean attributes.
.IP "Only code or array references allowed as handler" 4
.IX Item "Only code or array references allowed as handler"
(F) The second argument for \f(CW$p\fR\->handler must be either a subroutine
reference, then name of a subroutine or method, or a reference to an
array.
.ie n .IP "No handler for %s events" 4
.el .IP "No handler for \f(CW%s\fR events" 4
.IX Item "No handler for %s events"
(F) The first argument to \f(CW$p\fR\->handler must be a valid event name; i.e. one
of \*(L"start\*(R", \*(L"end\*(R", \*(L"text\*(R", \*(L"process\*(R", \*(L"declaration\*(R" or \*(L"comment\*(R".
.ie n .IP "Unrecognized identifier %s in argspec" 4
.el .IP "Unrecognized identifier \f(CW%s\fR in argspec" 4
.IX Item "Unrecognized identifier %s in argspec"
(F) The identifier is not a known argspec name.
Use one of the names mentioned in the argspec section above.
.IP "Literal string is longer than 255 chars in argspec" 4
.IX Item "Literal string is longer than 255 chars in argspec"
(F) The current implementation limits the length of literals in
an argspec to 255 characters. Make the literal shorter.
.IP "Backslash reserved for literal string in argspec" 4
.IX Item "Backslash reserved for literal string in argspec"
(F) The backslash character \*(L"\e\*(R" is not allowed in argspec literals.
It is reserved to permit quoting inside a literal in a later version.
.IP "Unterminated literal string in argspec" 4
.IX Item "Unterminated literal string in argspec"
(F) The terminating quote character for a literal was not found.
.IP "Bad argspec (%s)" 4
.IX Item "Bad argspec (%s)"
(F) Only identifier names, literals, spaces and commas
are allowed in argspecs.
.IP "Missing comma separator in argspec" 4
.IX Item "Missing comma separator in argspec"
(F) Identifiers in an argspec must be separated with \*(L",\*(R".
.SH "SEE ALSO"
.IX Header "SEE ALSO"
HTML::Entities, HTML::PullParser, HTML::TokeParser, HTML::HeadParser,
HTML::LinkExtor, HTML::Form
.PP
HTML::TreeBuilder (part of the \fIHTML-Tree\fR distribution)
.PP
http://www.w3.org/TR/REC\-html40
.PP
More information about marked sections and processing instructions may
be found at \f(CW\*(C`http://www.sgml.u\-net.com/book/sgml\-8.htm\*(C'\fR.
.SH "COPYRIGHT"
.IX Header "COPYRIGHT"
.Vb 2
\& Copyright 1996-2003 Gisle Aas. All rights reserved.
\& Copyright 1999-2000 Michael A. Chase. All rights reserved.
.Ve
.PP
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.