.\" Automatically generated by Pod::Man 4.10 (Pod::Simple 3.35)
.\"
.\" Standard preamble:
.\" ========================================================================
.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.  \*(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-
.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" ''
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
.    if \nF \{\
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{\
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "Parser 3pm"
.TH Parser 3pm "2018-11-01" "perl v5.28.0" "User Contributed Perl Documentation"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
HTML::Parser \- HTML parser class
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\& use HTML::Parser ();
\&
\& # Create parser object
\& $p = HTML::Parser\->new( api_version => 3,
\&                         start_h => [\e&start, "tagname, attr"],
\&                         end_h   => [\e&end,   "tagname"],
\&                         marked_sections => 1,
\&                       );
\&
\& # Parse document text chunk by chunk
\& $p\->parse($chunk1);
\& $p\->parse($chunk2);
\& #...
\& $p\->eof;                 # signal end of document
\&
\& # Parse directly from file
\& $p\->parse_file("foo.html");
\& # or
\& open(my $fh, "<:utf8", "foo.html") || die;
\& $p\->parse_file($fh);
.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 is 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.  This is an \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 "\f(CW$p\fR = 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 argument pairs may be provided to assign event
handlers or initialize parser options.  The handlers and parser
options can also be set or modified later by the 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 \fBnew()\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
The 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 => [ \*(Aqmy_start\*(Aq, "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 "\f(CW$p\fR\->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.  Handlers invoked should
not attempt to 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\->\fBparse()\fR
will return a \s-1FALSE\s0 value.  Otherwise the return value is a reference to the
parser object ($p).
.ie n .IP "$p\->parse( $code_ref )" 4
.el .IP "\f(CW$p\fR\->parse( \f(CW$code_ref\fR )" 4
.IX Item "$p->parse( $code_ref )"
If a code reference is passed as the argument to be parsed, then the
chunks to be parsed are 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 signaled.
.Sp
Parsing will also abort if one of the event handlers calls \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 "\f(CW$p\fR\->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 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\->\fBparse_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 \fBparse_file()\fR is called on
a file handle that is not in binary mode.
.Sp
If a filename is passed in, then \fBparse_file()\fR will open the file in
binary mode.
.ie n .IP "$p\->eof" 4
.el .IP "\f(CW$p\fR\->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\->\fBparse_file()\fR.
.Sp
After \f(CW$p\fR\->eof has been called, the \fBparse()\fR and \fBparse_file()\fR methods
can be invoked to feed new documents with the parser object.
.Sp
The return value from \fBeof()\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:
.ie n .IP "$p\->attr_encoded" 4
.el .IP "\f(CW$p\fR\->attr_encoded" 4
.IX Item "$p->attr_encoded"
.PD 0
.ie n .IP "$p\->attr_encoded( $bool )" 4
.el .IP "\f(CW$p\fR\->attr_encoded( \f(CW$bool\fR )" 4
.IX Item "$p->attr_encoded( $bool )"
.PD
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.
.ie n .IP "$p\->backquote" 4
.el .IP "\f(CW$p\fR\->backquote" 4
.IX Item "$p->backquote"
.PD 0
.ie n .IP "$p\->backquote( $bool )" 4
.el .IP "\f(CW$p\fR\->backquote( \f(CW$bool\fR )" 4
.IX Item "$p->backquote( $bool )"
.PD
By default, only ' and " are recognized as quote characters around
attribute values.  \s-1MSIE\s0 also recognizes backquotes for some reason.
Enabling this attribute provides compatibility with this behaviour.
.ie n .IP "$p\->boolean_attribute_value( $val )" 4
.el .IP "\f(CW$p\fR\->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.
.ie n .IP "$p\->case_sensitive" 4
.el .IP "\f(CW$p\fR\->case_sensitive" 4
.IX Item "$p->case_sensitive"
.PD 0
.ie n .IP "$p\->case_sensitive( $bool )" 4
.el .IP "\f(CW$p\fR\->case_sensitive( \f(CW$bool\fR )" 4
.IX Item "$p->case_sensitive( $bool )"
.PD
By default, tagnames and attribute names are down-cased.  Enabling this
attribute leaves them as found in the \s-1HTML\s0 source document.
.ie n .IP "$p\->closing_plaintext" 4
.el .IP "\f(CW$p\fR\->closing_plaintext" 4
.IX Item "$p->closing_plaintext"
.PD 0
.ie n .IP "$p\->closing_plaintext( $bool )" 4
.el .IP "\f(CW$p\fR\->closing_plaintext( \f(CW$bool\fR )" 4
.IX Item "$p->closing_plaintext( $bool )"
.PD
By default, \*(L"plaintext\*(R" element can never be closed. Everything up to
the end of the document is parsed in \s-1CDATA\s0 mode.  This historical
behaviour is what at least \s-1MSIE\s0 does.  Enabling this attribute makes
closing \*(L"</plaintext>\*(R" tag effective and the parsing process will resume
after seeing this tag.  This emulates early gecko-based browsers.
.ie n .IP "$p\->empty_element_tags" 4
.el .IP "\f(CW$p\fR\->empty_element_tags" 4
.IX Item "$p->empty_element_tags"
.PD 0
.ie n .IP "$p\->empty_element_tags( $bool )" 4
.el .IP "\f(CW$p\fR\->empty_element_tags( \f(CW$bool\fR )" 4
.IX Item "$p->empty_element_tags( $bool )"
.PD
By default, empty element tags are not recognized as such and the \*(L"/\*(R"
before \*(L">\*(R" is just treated like a normal name character (unless
\&\f(CW\*(C`strict_names\*(C'\fR is enabled).  Enabling this attribute make
\&\f(CW\*(C`HTML::Parser\*(C'\fR recognize these tags.
.Sp
Empty element tags look like start tags, but end with the character
sequence \*(L"/>\*(R" instead of \*(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 token array will have one
element containing the tag name.
.ie n .IP "$p\->marked_sections" 4
.el .IP "\f(CW$p\fR\->marked_sections" 4
.IX Item "$p->marked_sections"
.PD 0
.ie n .IP "$p\->marked_sections( $bool )" 4
.el .IP "\f(CW$p\fR\->marked_sections( \f(CW$bool\fR )" 4
.IX Item "$p->marked_sections( $bool )"
.PD
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.
.ie n .IP "$p\->strict_comment" 4
.el .IP "\f(CW$p\fR\->strict_comment" 4
.IX Item "$p->strict_comment"
.PD 0
.ie n .IP "$p\->strict_comment( $bool )" 4
.el .IP "\f(CW$p\fR\->strict_comment( \f(CW$bool\fR )" 4
.IX Item "$p->strict_comment( $bool )"
.PD
By default, comments are terminated by the first occurrence of \*(L"\-\->\*(R".
This is the behaviour of most popular browsers (like Mozilla, Opera 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.
.Sp
Enabling of 'strict_comment' also disables recognizing these forms as
comments:
.Sp
.Vb 2
\&  </ comment>
\&  <! comment>
.Ve
.ie n .IP "$p\->strict_end" 4
.el .IP "\f(CW$p\fR\->strict_end" 4
.IX Item "$p->strict_end"
.PD 0
.ie n .IP "$p\->strict_end( $bool )" 4
.el .IP "\f(CW$p\fR\->strict_end( \f(CW$bool\fR )" 4
.IX Item "$p->strict_end( $bool )"
.PD
By default, attributes and other junk are allowed to be present on end tags in a
manner that emulates \s-1MSIE\s0's behaviour.
.Sp
The official behaviour is enabled with this attribute.  If enabled,
only whitespace is allowed between the tagname and the final \*(L">\*(R".
.ie n .IP "$p\->strict_names" 4
.el .IP "\f(CW$p\fR\->strict_names" 4
.IX Item "$p->strict_names"
.PD 0
.ie n .IP "$p\->strict_names( $bool )" 4
.el .IP "\f(CW$p\fR\->strict_names( \f(CW$bool\fR )" 4
.IX Item "$p->strict_names( $bool )"
.PD
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 attribute 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
Mozilla 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\->unbroken_text" 4
.el .IP "\f(CW$p\fR\->unbroken_text" 4
.IX Item "$p->unbroken_text"
.PD 0
.ie n .IP "$p\->unbroken_text( $bool )" 4
.el .IP "\f(CW$p\fR\->unbroken_text( \f(CW$bool\fR )" 4
.IX Item "$p->unbroken_text( $bool )"
.PD
By default, blocks of text are given to the text handler as soon as
possible (but the parser takes care always to break text at a
boundary between whitespace and non-whitespace so single words and
entities can always 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.
.ie n .IP "$p\->utf8_mode" 4
.el .IP "\f(CW$p\fR\->utf8_mode" 4
.IX Item "$p->utf8_mode"
.PD 0
.ie n .IP "$p\->utf8_mode( $bool )" 4
.el .IP "\f(CW$p\fR\->utf8_mode( \f(CW$bool\fR )" 4
.IX Item "$p->utf8_mode( $bool )"
.PD
Enable this option when parsing raw undecoded \s-1UTF\-8.\s0  This tells the
parser that the entities expanded for strings reported by \f(CW\*(C`attr\*(C'\fR,
\&\f(CW@attr\fR and \f(CW\*(C`dtext\*(C'\fR should be expanded as decoded \s-1UTF\-8\s0 so they end
up compatible with the surrounding text.
.Sp
If \f(CW\*(C`utf8_mode\*(C'\fR is enabled then it is an error to pass strings
containing characters with code above 255 to the \fBparse()\fR method, and
the \fBparse()\fR method will croak if you try.
.Sp
Example: The Unicode character \*(L"\ex{2665}\*(R" is \*(L"\exE2\ex99\exA5\*(R" when \s-1UTF\-8\s0
encoded.  The character can also be represented by the entity
\&\*(L"&hearts;\*(R" or \*(L"&#x2665\*(R".  If we feed the parser:
.Sp
.Vb 1
\&  $p\->parse("\exE2\ex99\exA5&hearts;");
.Ve
.Sp
then \f(CW\*(C`dtext\*(C'\fR will be reported as \*(L"\exE2\ex99\exA5\ex{2665}\*(R" without
\&\f(CW\*(C`utf8_mode\*(C'\fR enabled, but as \*(L"\exE2\ex99\exA5\exE2\ex99\exA5\*(R" when enabled.
The later string is what you want.
.Sp
This option is only available with perl\-5.8 or better.
.ie n .IP "$p\->xml_mode" 4
.el .IP "\f(CW$p\fR\->xml_mode" 4
.IX Item "$p->xml_mode"
.PD 0
.ie n .IP "$p\->xml_mode( $bool )" 4
.el .IP "\f(CW$p\fR\->xml_mode( \f(CW$bool\fR )" 4
.IX Item "$p->xml_mode( $bool )"
.PD
Enabling this attribute changes the parser to allow some \s-1XML\s0
constructs.  This enables the behaviour controlled by individually by
the \f(CW\*(C`case_sensitive\*(C'\fR, \f(CW\*(C`empty_element_tags\*(C'\fR, \f(CW\*(C`strict_names\*(C'\fR and
\&\f(CW\*(C`xml_pic\*(C'\fR attributes and also suppresses special treatment of
elements that are parsed as \s-1CDATA\s0 for \s-1HTML.\s0
.ie n .IP "$p\->xml_pic" 4
.el .IP "\f(CW$p\fR\->xml_pic" 4
.IX Item "$p->xml_pic"
.PD 0
.ie n .IP "$p\->xml_pic( $bool )" 4
.el .IP "\f(CW$p\fR\->xml_pic( \f(CW$bool\fR )" 4
.IX Item "$p->xml_pic( $bool )"
.PD
By default, \fIprocessing instructions\fR are terminated by \*(L">\*(R". When
this attribute is enabled, processing instructions are terminated by
\&\*(L"?>\*(R" instead.
.PP
As markup and text is recognized, handlers are invoked.  The following
method is used to set up handlers for different events:
.ie n .IP "$p\->handler( event => \e&subroutine, $argspec )" 4
.el .IP "\f(CW$p\fR\->handler( event => \e&subroutine, \f(CW$argspec\fR )" 4
.IX Item "$p->handler( event => &subroutine, $argspec )"
.PD 0
.ie n .IP "$p\->handler( event => $method_name, $argspec )" 4
.el .IP "\f(CW$p\fR\->handler( event => \f(CW$method_name\fR, \f(CW$argspec\fR )" 4
.IX Item "$p->handler( event => $method_name, $argspec )"
.ie n .IP "$p\->handler( event => \e@accum, $argspec )" 4
.el .IP "\f(CW$p\fR\->handler( event => \e@accum, \f(CW$argspec\fR )" 4
.IX Item "$p->handler( event => @accum, $argspec )"
.ie n .IP "$p\->handler( event => """" );" 4
.el .IP "\f(CW$p\fR\->handler( event => ``'' );" 4
.IX Item "$p->handler( event => """" );"
.ie n .IP "$p\->handler( event => undef );" 4
.el .IP "\f(CW$p\fR\->handler( event => undef );" 4
.IX Item "$p->handler( event => undef );"
.ie n .IP "$p\->handler( event );" 4
.el .IP "\f(CW$p\fR\->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
The \f(CW\*(C`\e&subroutine\*(C'\fR is a reference to a subroutine which is called to handle
the event.
.Sp
The \f(CW$method_name\fR is the name of a method of \f(CW$p\fR which is called to handle
the event.
.Sp
The \f(CW@accum\fR is an 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
The \f(CW$argspec\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.
.Sp
The return value from \f(CW$p\fR\->handler 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\->\fBparse()\fR or \f(CW$p\fR\->\fBparse_file()\fR method.  An exception will
be raised if it tries.
.Sp
Examples:
.Sp
.Vb 1
\&    $p\->handler(start =>  "start", \*(Aqself, attr, attrseq, text\*(Aq );
.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, \*(Aqattr, attrseq, text\*(Aq );
.Ve
.Sp
This causes subroutine \fBstart()\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, \*(Aq"S", attr, attrseq, text\*(Aq );
.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 suppresses
invocations 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 associated 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:
.ie n .IP "$p\->ignore_elements( @tags )" 4
.el .IP "\f(CW$p\fR\->ignore_elements( \f(CW@tags\fR )" 4
.IX Item "$p->ignore_elements( @tags )"
Both the \f(CW\*(C`start\*(C'\fR event 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 occurrences 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.
.ie n .IP "$p\->ignore_tags( @tags )" 4
.el .IP "\f(CW$p\fR\->ignore_tags( \f(CW@tags\fR )" 4
.IX Item "$p->ignore_tags( @tags )"
Any \f(CW\*(C`start\*(C'\fR and \f(CW\*(C`end\*(C'\fR events involving any of the tags given are
suppressed.  To reset the filter (i.e. don't suppress any \f(CW\*(C`start\*(C'\fR and
\&\f(CW\*(C`end\*(C'\fR events), call \f(CW\*(C`ignore_tags\*(C'\fR without an argument.
.ie n .IP "$p\->report_tags( @tags )" 4
.el .IP "\f(CW$p\fR\->report_tags( \f(CW@tags\fR )" 4
.IX Item "$p->report_tags( @tags )"
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.  To reset the filter (i.e. report all \f(CW\*(C`start\*(C'\fR and
\&\f(CW\*(C`end\*(C'\fR events), call \f(CW\*(C`report_tags\*(C'\fR without an argument.
.PP
Internally, the system has two filter lists, one for \f(CW\*(C`report_tags\*(C'\fR
and one for \f(CW\*(C`ignore_tags\*(C'\fR, and both filters are applied.  This
effectively gives \f(CW\*(C`ignore_tags\*(C'\fR precedence over \f(CW\*(C`report_tags\*(C'\fR.
.PP
Examples:
.PP
.Vb 2
\&   $p\->ignore_tags(qw(style));
\&   $p\->report_tags(qw(script style));
.Ve
.PP
results in only \f(CW\*(C`script\*(C'\fR events being reported.
.SS "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 """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 is removed.
.Sp
The Unicode character set is assumed for entity decoding.
.ie n .IP "@attr" 4
.el .IP "\f(CW@attr\fR" 4
.IX Item "@attr"
Basically the 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 passes no values for events besides \f(CW\*(C`start\*(C'\fR.
.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 """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 """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, \f(CW\*(C`iframe\*(C'\fR, \f(CW\*(C`title\*(C'\fR, \f(CW\*(C`textarea\*(C'\fR and \f(CW\*(C`plaintext\*(C'\fR).
.Sp
The Unicode character set is assumed for entity decoding.  With Perl
version 5.6 or earlier only the Latin\-1 range is supported, and
entities for characters outside the range 0..255 are left unchanged.
.Sp
This passes undef except for \f(CW\*(C`text\*(C'\fR events.
.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 or \f(CW\*(C`end_document\*(C'\fR.
.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 between literal start and end tags (\f(CW\*(C`script\*(C'\fR,
\&\f(CW\*(C`style\*(C'\fR, \f(CW\*(C`xmp\*(C'\fR, \f(CW\*(C`iframe\*(C'\fR, \f(CW\*(C`title\*(C'\fR, \f(CW\*(C`textarea\*(C'\fR and \f(CW\*(C`plaintext\*(C'\fR).
.Sp
if 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 """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 """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 """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 has offset 0.
.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 """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 circular references which prevent the HTML::Parser object
from being garbage collected.  Using the \f(CW\*(C`self\*(C'\fR argspec avoids this
problem.
.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 have
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 includes marked section markup,
since there are no events that can catch it.
.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 """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 """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.  The 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 """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 """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 artificial \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 """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 values 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 """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 """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.
.ie n .IP "\*(Aq...\*(Aq" 4
.el .IP "\f(CW\*(Aq...\*(Aq\fR" 4
.IX Item "..."
A literal string of 0 to 255 characters enclosed
in single (') or double (") quotes is passed as entered.
.PP
The whole argspec string can be wrapped up in \f(CW\*(Aq@{...}\*(Aq\fR to signal
that the resulting event array should be flattened.  This only makes a
difference if an array reference is used as the handler target.
Consider this example:
.PP
.Vb 2
\&   $p\->handler(text => [], \*(Aqtext\*(Aq);
\&   $p\->handler(text => [], \*(Aq@{text}\*(Aq]);
.Ve
.PP
With two text events; \f(CW"foo"\fR, \f(CW"bar"\fR; then the first example will end
up with [[\*(L"foo\*(R"], [\*(L"bar\*(R"]] and the second with [\*(L"foo\*(R", \*(L"bar\*(R"] in
the handler target array.
.SS "Events"
.IX Subsection "Events"
Handlers for the following events can be registered:
.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 """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/html4/strict.dtd">
.Ve
.Sp
DTDs inside <!DOCTYPE ...> will confuse HTML::Parser.
.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.
.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 """end_document""" 4
.el .IP "\f(CWend_document\fR" 4
.IX Item "end_document"
This event is triggered when \f(CW$p\fR\->eof is called and after any remaining
text is flushed.  There is no document text associated with this event.
.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 are system and
application dependent.
.Sp
Examples:
.Sp
.Vb 2
\&  <? HTML processing instructions >
\&  <? XML processing instructions ?>
.Ve
.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 """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 """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.
.SS "Unicode"
.IX Subsection "Unicode"
\&\f(CW\*(C`HTML::Parser\*(C'\fR can parse Unicode strings when running under
perl\-5.8 or better.  If Unicode is passed to \f(CW$p\fR\->\fBparse()\fR then chunks
of Unicode will be reported to the handlers.  The offset and length
argspecs will also report their position in terms of characters.
.PP
It is safe to parse raw undecoded \s-1UTF\-8\s0 if you either avoid decoding
entities and make sure to not use \fIargspecs\fR that do, or enable the
\&\f(CW\*(C`utf8_mode\*(C'\fR for the parser.  Parsing of undecoded \s-1UTF\-8\s0 might be
useful when parsing from a file where you need the reported offsets
and lengths to match the byte offsets in the file.
.PP
If a filename is passed to \f(CW$p\fR\->\fBparse_file()\fR then the file will be read
in binary mode.  This will be fine if the file contains only \s-1ASCII\s0 or
Latin\-1 characters.  If the file contains \s-1UTF\-8\s0 encoded text then care
must be taken when decoding entities as described in the previous
paragraph, but better is to open the file with the \s-1UTF\-8\s0 layer so that
it is decoded properly:
.PP
.Vb 2
\&   open(my $fh, "<:utf8", "index.html") || die "...: $!";
\&   $p\->parse_file($fh);
.Ve
.PP
If the file contains text encoded in a charset besides \s-1ASCII,\s0 Latin\-1
or \s-1UTF\-8\s0 then decoding will always be needed.
.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 10
\&   $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
Setting up 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 \fBinit()\fR
method, which takes the same arguments as \fBnew()\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 }, \*(Aqtext\*(Aq],
\&                    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 },
\&                                       \*(Aqskipped_text\*(Aq],
\&                    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 ();
\&
\&  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");
\&  }
\&
\&  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
On a Debian box, more examples can be found in the
/usr/share/doc/libhtml\-parser\-perl/examples directory.
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 edit 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.
.PP
You can browse the \fIeg/\fR directory online from the \fI[Browse]\fR link on
the http://search.cpan.org/~gaas/HTML\-Parser/ page.
.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.  The standard behaviour is
not really practical.
.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
respectively.
.PP
\&\s-1NET\s0 tags, e.g. \*(L"code/.../\*(R" are not recognized.  This is \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 not supported by HTML::Parser %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 extensions.
.IP "Bad constructor option '%s'" 4
.IX Item "Bad constructor option '%s'"
(F) An unknown constructor option key was passed to the \fBnew()\fR or
\&\fBinit()\fR methods.
.IP "Parse loop not allowed" 4
.IX Item "Parse loop not allowed"
(F) A handler invoked the \fBparse()\fR or \fBparse_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\->\fBmarked_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".
.IP "Parsing of undecoded \s-1UTF\-8\s0 will give garbage when decoding entities" 4
.IX Item "Parsing of undecoded UTF-8 will give garbage when decoding entities"
(W) The first chunk parsed appears to contain undecoded \s-1UTF\-8\s0 and one
or more argspecs that decode entities are used for the callback
handlers.
.Sp
The result of decoding will be a mix of encoded and decoded characters
for any entities that expand to characters with code above 127.  This
is not a good thing.
.Sp
The recommended solution is to apply \fBEncode::decode_utf8()\fR on the data before
feeding it to the \f(CW$p\fR\->\fBparse()\fR.  For \f(CW$p\fR\->\fBparse_file()\fR pass a file that has been
opened in \*(L":utf8\*(R" mode.
.Sp
The alternative solution is to enable the \f(CW\*(C`utf8_mode\*(C'\fR and not decode before
passing strings to \f(CW$p\fR\->\fBparse()\fR.  The parser can process raw undecoded \s-1UTF\-8\s0
sanely if the \f(CW\*(C`utf8_mode\*(C'\fR is enabled, or if the \*(L"attr\*(R", \*(L"@attr\*(R" or \*(L"dtext\*(R"
argspecs are avoided.
.IP "Parsing string decoded with wrong endianness" 4
.IX Item "Parsing string decoded with wrong endianness"
(W) The first character in the document is U+FFFE.  This is not a
legal Unicode character but a byte swapped \s-1BOM.\s0  The result of parsing
will likely be garbage.
.IP "Parsing of undecoded \s-1UTF\-32\s0" 4
.IX Item "Parsing of undecoded UTF-32"
(W) The parser found the Unicode \s-1UTF\-32 BOM\s0 signature at the start
of the document.  The result of parsing will likely be garbage.
.IP "Parsing of undecoded \s-1UTF\-16\s0" 4
.IX Item "Parsing of undecoded UTF-16"
(W) The parser found the Unicode \s-1UTF\-16 BOM\s0 signature at the start of
the document.  The result of parsing will likely be garbage.
.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/html4/>
.PP
More information about marked sections and processing instructions may
be found at <http://www.is\-thought.co.uk/book/sgml\-8.htm>.
.SH "COPYRIGHT"
.IX Header "COPYRIGHT"
.Vb 2
\& Copyright 1996\-2016 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.
