Add built local::lib
[catagits/Gitalist.git] / local-lib5 / man / man3 / HTML::Parser.3pm
1 .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.3
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sh \" Subsection heading
6 .br
7 .if t .Sp
8 .ne 5
9 .PP
10 \fB\\$1\fR
11 .PP
12 ..
13 .de Sp \" Vertical space (when we can't use .PP)
14 .if t .sp .5v
15 .if n .sp
16 ..
17 .de Vb \" Begin verbatim text
18 .ft CW
19 .nf
20 .ne \\$1
21 ..
22 .de Ve \" End verbatim text
23 .ft R
24 .fi
25 ..
26 .\" Set up some character translations and predefined strings.  \*(-- will
27 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28 .\" double quote, and \*(R" will give a right double quote.  | will give a
29 .\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
30 .\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
31 .\" expand to `' in nroff, nothing in troff, for use with C<>.
32 .tr \(*W-|\(bv\*(Tr
33 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34 .ie n \{\
35 .    ds -- \(*W-
36 .    ds PI pi
37 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
39 .    ds L" ""
40 .    ds R" ""
41 .    ds C` ""
42 .    ds C' ""
43 'br\}
44 .el\{\
45 .    ds -- \|\(em\|
46 .    ds PI \(*p
47 .    ds L" ``
48 .    ds R" ''
49 'br\}
50 .\"
51 .\" If the F register is turned on, we'll generate index entries on stderr for
52 .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53 .\" entries marked with X<> in POD.  Of course, you'll have to process the
54 .\" output yourself in some meaningful fashion.
55 .if \nF \{\
56 .    de IX
57 .    tm Index:\\$1\t\\n%\t"\\$2"
58 ..
59 .    nr % 0
60 .    rr F
61 .\}
62 .\"
63 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
64 .\" way too many mistakes in technical documents.
65 .hy 0
66 .if n .na
67 .\"
68 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
70 .    \" fudge factors for nroff and troff
71 .if n \{\
72 .    ds #H 0
73 .    ds #V .8m
74 .    ds #F .3m
75 .    ds #[ \f1
76 .    ds #] \fP
77 .\}
78 .if t \{\
79 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 .    ds #V .6m
81 .    ds #F 0
82 .    ds #[ \&
83 .    ds #] \&
84 .\}
85 .    \" simple accents for nroff and troff
86 .if n \{\
87 .    ds ' \&
88 .    ds ` \&
89 .    ds ^ \&
90 .    ds , \&
91 .    ds ~ ~
92 .    ds /
93 .\}
94 .if t \{\
95 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101 .\}
102 .    \" troff and (daisy-wheel) nroff accents
103 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110 .ds ae a\h'-(\w'a'u*4/10)'e
111 .ds Ae A\h'-(\w'A'u*4/10)'E
112 .    \" corrections for vroff
113 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115 .    \" for low resolution devices (crt and lpr)
116 .if \n(.H>23 .if \n(.V>19 \
117 \{\
118 .    ds : e
119 .    ds 8 ss
120 .    ds o a
121 .    ds d- d\h'-1'\(ga
122 .    ds D- D\h'-1'\(hy
123 .    ds th \o'bp'
124 .    ds Th \o'LP'
125 .    ds ae ae
126 .    ds Ae AE
127 .\}
128 .rm #[ #] #H #V #F C
129 .\" ========================================================================
130 .\"
131 .IX Title "Parser 3"
132 .TH Parser 3 "2009-10-25" "perl v5.8.7" "User Contributed Perl Documentation"
133 .SH "NAME"
134 HTML::Parser \- HTML parser class
135 .SH "SYNOPSIS"
136 .IX Header "SYNOPSIS"
137 .Vb 1
138 \& use HTML::Parser ();
139 .Ve
140 .PP
141 .Vb 6
142 \& # Create parser object
143 \& $p = HTML::Parser\->new( api_version => 3,
144 \&                         start_h => [\e&start, "tagname, attr"],
145 \&                         end_h   => [\e&end,   "tagname"],
146 \&                         marked_sections => 1,
147 \&                       );
148 .Ve
149 .PP
150 .Vb 5
151 \& # Parse document text chunk by chunk
152 \& $p\->parse($chunk1);
153 \& $p\->parse($chunk2);
154 \& #...
155 \& $p\->eof;                 # signal end of document
156 .Ve
157 .PP
158 .Vb 5
159 \& # Parse directly from file
160 \& $p\->parse_file("foo.html");
161 \& # or
162 \& open(my $fh, "<:utf8", "foo.html") || die;
163 \& $p\->parse_file($fh);
164 .Ve
165 .SH "DESCRIPTION"
166 .IX Header "DESCRIPTION"
167 Objects of the \f(CW\*(C`HTML::Parser\*(C'\fR class will recognize markup and
168 separate it from plain text (alias data content) in \s-1HTML\s0
169 documents.  As different kinds of markup and text are recognized, the
170 corresponding event handlers are invoked.
171 .PP
172 \&\f(CW\*(C`HTML::Parser\*(C'\fR is not a generic \s-1SGML\s0 parser.  We have tried to
173 make it able to deal with the \s-1HTML\s0 that is actually \*(L"out there\*(R", and
174 it normally parses as closely as possible to the way the popular web
175 browsers do it instead of strictly following one of the many \s-1HTML\s0
176 specifications from W3C.  Where there is disagreement, there is often
177 an option that you can enable to get the official behaviour.
178 .PP
179 The document to be parsed may be supplied in arbitrary chunks.  This
180 makes on-the-fly parsing as documents are received from the network
181 possible.
182 .PP
183 If event driven parsing does not feel right for your application, you
184 might want to use \f(CW\*(C`HTML::PullParser\*(C'\fR.  This is an \f(CW\*(C`HTML::Parser\*(C'\fR
185 subclass that allows a more conventional program structure.
186 .SH "METHODS"
187 .IX Header "METHODS"
188 The following method is used to construct a new \f(CW\*(C`HTML::Parser\*(C'\fR object:
189 .ie n .IP "$p = HTML::Parser\->new( %options_and_handlers )" 4
190 .el .IP "$p = HTML::Parser\->new( \f(CW%options_and_handlers\fR )" 4
191 .IX Item "$p = HTML::Parser->new( %options_and_handlers )"
192 This class method creates a new \f(CW\*(C`HTML::Parser\*(C'\fR object and
193 returns it.  Key/value argument pairs may be provided to assign event
194 handlers or initialize parser options.  The handlers and parser
195 options can also be set or modified later by the method calls described below.
196 .Sp
197 If a top level key is in the form \*(L"<event>_h\*(R" (e.g., \*(L"text_h\*(R") then it
198 assigns a handler to that event, otherwise it initializes a parser
199 option. The event handler specification value must be an array
200 reference.  Multiple handlers may also be assigned with the 'handlers
201 => [%handlers]' option.  See examples below.
202 .Sp
203 If \fInew()\fR is called without any arguments, it will create a parser that
204 uses callback methods compatible with version 2 of \f(CW\*(C`HTML::Parser\*(C'\fR.
205 See the section on \*(L"version 2 compatibility\*(R" below for details.
206 .Sp
207 The special constructor option 'api_version => 2' can be used to
208 initialize version 2 callbacks while still setting other options and
209 handlers.  The 'api_version => 3' option can be used if you don't want
210 to set any options and don't want to fall back to v2 compatible
211 mode.
212 .Sp
213 Examples:
214 .Sp
215 .Vb 2
216 \& $p = HTML::Parser\->new(api_version => 3,
217 \&                        text_h => [ sub {...}, "dtext" ]);
218 .Ve
219 .Sp
220 This creates a new parser object with a text event handler subroutine
221 that receives the original text with general entities decoded.
222 .Sp
223 .Vb 2
224 \& $p = HTML::Parser\->new(api_version => 3,
225 \&                        start_h => [ 'my_start', "self,tokens" ]);
226 .Ve
227 .Sp
228 This creates a new parser object with a start event handler method
229 that receives the \f(CW$p\fR and the tokens array.
230 .Sp
231 .Vb 4
232 \& $p = HTML::Parser\->new(api_version => 3,
233 \&                        handlers => { text => [\e@array, "event,text"],
234 \&                                      comment => [\e@array, "event,text"],
235 \&                                    });
236 .Ve
237 .Sp
238 This creates a new parser object that stores the event type and the
239 original text in \f(CW@array\fR for text and comment events.
240 .PP
241 The following methods feed the \s-1HTML\s0 document
242 to the \f(CW\*(C`HTML::Parser\*(C'\fR object:
243 .ie n .IP "$p\->parse( $string )" 4
244 .el .IP "$p\->parse( \f(CW$string\fR )" 4
245 .IX Item "$p->parse( $string )"
246 Parse \f(CW$string\fR as the next chunk of the \s-1HTML\s0 document.  Handlers invoked should
247 not attempt to modify the \f(CW$string\fR in-place until \f(CW$p\fR\->parse returns.
248 .Sp
249 If an invoked event handler aborts parsing by calling \f(CW$p\fR\->eof, then \f(CW$p\fR\->\fIparse()\fR
250 will return a \s-1FALSE\s0 value.  Otherwise the return value is a reference to the
251 parser object ($p).
252 .ie n .IP "$p\->parse( $code_ref )" 4
253 .el .IP "$p\->parse( \f(CW$code_ref\fR )" 4
254 .IX Item "$p->parse( $code_ref )"
255 If a code reference is passed as the argument to be parsed, then the
256 chunks to be parsed are obtained by invoking this function repeatedly.
257 Parsing continues until the function returns an empty (or undefined)
258 result.  When this happens \f(CW$p\fR\->eof is automatically signaled.
259 .Sp
260 Parsing will also abort if one of the event handlers calls \f(CW$p\fR\->eof.
261 .Sp
262 The effect of this is the same as:
263 .Sp
264 .Vb 8
265 \& while (1) {
266 \&    my $chunk = &$code_ref();
267 \&    if (!defined($chunk) || !length($chunk)) {
268 \&        $p\->eof;
269 \&        return $p;
270 \&    }
271 \&    $p\->parse($chunk) || return undef;
272 \& }
273 .Ve
274 .Sp
275 But it is more efficient as this loop runs internally in \s-1XS\s0 code.
276 .ie n .IP "$p\->parse_file( $file )" 4
277 .el .IP "$p\->parse_file( \f(CW$file\fR )" 4
278 .IX Item "$p->parse_file( $file )"
279 Parse text directly from a file.  The \f(CW$file\fR argument can be a
280 filename, an open file handle, or a reference to an open file
281 handle.
282 .Sp
283 If \f(CW$file\fR contains a filename and the file can't be opened, then the
284 method returns an undefined value and $! tells why it failed.
285 Otherwise the return value is a reference to the parser object.
286 .Sp
287 If a file handle is passed as the \f(CW$file\fR argument, then the file will
288 normally be read until \s-1EOF\s0, but not closed.
289 .Sp
290 If an invoked event handler aborts parsing by calling \f(CW$p\fR\->eof,
291 then \f(CW$p\fR\->\fIparse_file()\fR may not have read the entire file.
292 .Sp
293 On systems with multi-byte line terminators, the values passed for the
294 offset and length argspecs may be too low if \fIparse_file()\fR is called on
295 a file handle that is not in binary mode.
296 .Sp
297 If a filename is passed in, then \fIparse_file()\fR will open the file in
298 binary mode.
299 .IP "$p\->eof" 4
300 .IX Item "$p->eof"
301 Signals the end of the \s-1HTML\s0 document.  Calling the \f(CW$p\fR\->eof method
302 outside a handler callback will flush any remaining buffered text
303 (which triggers the \f(CW\*(C`text\*(C'\fR event if there is any remaining text).
304 .Sp
305 Calling \f(CW$p\fR\->eof inside a handler will terminate parsing at that point
306 and cause \f(CW$p\fR\->parse to return a \s-1FALSE\s0 value.  This also terminates
307 parsing by \f(CW$p\fR\->\fIparse_file()\fR.
308 .Sp
309 After \f(CW$p\fR\->eof has been called, the \fIparse()\fR and \fIparse_file()\fR methods
310 can be invoked to feed new documents with the parser object.
311 .Sp
312 The return value from \fIeof()\fR is a reference to the parser object.
313 .PP
314 Most parser options are controlled by boolean attributes.
315 Each boolean attribute is enabled by calling the corresponding method
316 with a \s-1TRUE\s0 argument and disabled with a \s-1FALSE\s0 argument.  The
317 attribute value is left unchanged if no argument is given.  The return
318 value from each method is the old attribute value.
319 .PP
320 Methods that can be used to get and/or set parser options are:
321 .IP "$p\->attr_encoded" 4
322 .IX Item "$p->attr_encoded"
323 .PD 0
324 .ie n .IP "$p\->attr_encoded( $bool )" 4
325 .el .IP "$p\->attr_encoded( \f(CW$bool\fR )" 4
326 .IX Item "$p->attr_encoded( $bool )"
327 .PD
328 By default, the \f(CW\*(C`attr\*(C'\fR and \f(CW@attr\fR argspecs will have general
329 entities for attribute values decoded.  Enabling this attribute leaves
330 entities alone.
331 .IP "$p\->backquote" 4
332 .IX Item "$p->backquote"
333 .PD 0
334 .ie n .IP "$p\->backquote( $bool )" 4
335 .el .IP "$p\->backquote( \f(CW$bool\fR )" 4
336 .IX Item "$p->backquote( $bool )"
337 .PD
338 By default, only ' and " are recognized as quote characters around
339 attribute values.  \s-1MSIE\s0 also recognizes backquotes for some reason.
340 Enabling this attribute provides compatibility with this behaviour.
341 .ie n .IP "$p\->boolean_attribute_value( $val )" 4
342 .el .IP "$p\->boolean_attribute_value( \f(CW$val\fR )" 4
343 .IX Item "$p->boolean_attribute_value( $val )"
344 This method sets the value reported for boolean attributes inside \s-1HTML\s0
345 start tags.  By default, the name of the attribute is also used as its
346 value.  This affects the values reported for \f(CW\*(C`tokens\*(C'\fR and \f(CW\*(C`attr\*(C'\fR
347 argspecs.
348 .IP "$p\->case_sensitive" 4
349 .IX Item "$p->case_sensitive"
350 .PD 0
351 .ie n .IP "$p\->case_sensitive( $bool )" 4
352 .el .IP "$p\->case_sensitive( \f(CW$bool\fR )" 4
353 .IX Item "$p->case_sensitive( $bool )"
354 .PD
355 By default, tagnames and attribute names are down\-cased.  Enabling this
356 attribute leaves them as found in the \s-1HTML\s0 source document.
357 .IP "$p\->closing_plaintext" 4
358 .IX Item "$p->closing_plaintext"
359 .PD 0
360 .ie n .IP "$p\->closing_plaintext( $bool )" 4
361 .el .IP "$p\->closing_plaintext( \f(CW$bool\fR )" 4
362 .IX Item "$p->closing_plaintext( $bool )"
363 .PD
364 By default, \*(L"plaintext\*(R" element can never be closed. Everything up to
365 the end of the document is parsed in \s-1CDATA\s0 mode.  This historical
366 behaviour is what at least \s-1MSIE\s0 does.  Enabling this attribute makes
367 closing \*(L"</plaintext>\*(R" tag effective and the parsing process will resume
368 after seeing this tag.  This emulates early gecko-based browsers.
369 .IP "$p\->empty_element_tags" 4
370 .IX Item "$p->empty_element_tags"
371 .PD 0
372 .ie n .IP "$p\->empty_element_tags( $bool )" 4
373 .el .IP "$p\->empty_element_tags( \f(CW$bool\fR )" 4
374 .IX Item "$p->empty_element_tags( $bool )"
375 .PD
376 By default, empty element tags are not recognized as such and the \*(L"/\*(R"
377 before \*(L">\*(R" is just treated like a normal name character (unless
378 \&\f(CW\*(C`strict_names\*(C'\fR is enabled).  Enabling this attribute make
379 \&\f(CW\*(C`HTML::Parser\*(C'\fR recognize these tags.
380 .Sp
381 Empty element tags look like start tags, but end with the character
382 sequence \*(L"/>\*(R" instead of \*(L">\*(R".  When recognized by \f(CW\*(C`HTML::Parser\*(C'\fR they
383 cause an artificial end event in addition to the start event.  The
384 \&\f(CW\*(C`text\*(C'\fR for the artificial end event will be empty and the \f(CW\*(C`tokenpos\*(C'\fR
385 array will be undefined even though the the token array will have one
386 element containing the tag name.
387 .IP "$p\->marked_sections" 4
388 .IX Item "$p->marked_sections"
389 .PD 0
390 .ie n .IP "$p\->marked_sections( $bool )" 4
391 .el .IP "$p\->marked_sections( \f(CW$bool\fR )" 4
392 .IX Item "$p->marked_sections( $bool )"
393 .PD
394 By default, section markings like <![CDATA[...]]> are treated like
395 ordinary text.  When this attribute is enabled section markings are
396 honoured.
397 .Sp
398 There are currently no events associated with the marked section
399 markup, but the text can be returned as \f(CW\*(C`skipped_text\*(C'\fR.
400 .IP "$p\->strict_comment" 4
401 .IX Item "$p->strict_comment"
402 .PD 0
403 .ie n .IP "$p\->strict_comment( $bool )" 4
404 .el .IP "$p\->strict_comment( \f(CW$bool\fR )" 4
405 .IX Item "$p->strict_comment( $bool )"
406 .PD
407 By default, comments are terminated by the first occurrence of \*(L"\-\->\*(R".
408 This is the behaviour of most popular browsers (like Mozilla, Opera and
409 \&\s-1MSIE\s0), but it is not correct according to the official \s-1HTML\s0
410 standard.  Officially, you need an even number of \*(L"\-\-\*(R" tokens before
411 the closing \*(L">\*(R" is recognized and there may not be anything but
412 whitespace between an even and an odd \*(L"\-\-\*(R".
413 .Sp
414 The official behaviour is enabled by enabling this attribute.
415 .Sp
416 Enabling of 'strict_comment' also disables recognizing these forms as
417 comments:
418 .Sp
419 .Vb 2
420 \&  </ comment>
421 \&  <! comment>
422 .Ve
423 .IP "$p\->strict_end" 4
424 .IX Item "$p->strict_end"
425 .PD 0
426 .ie n .IP "$p\->strict_end( $bool )" 4
427 .el .IP "$p\->strict_end( \f(CW$bool\fR )" 4
428 .IX Item "$p->strict_end( $bool )"
429 .PD
430 By default, attributes and other junk are allowed to be present on end tags in a
431 manner that emulates \s-1MSIE\s0's behaviour.
432 .Sp
433 The official behaviour is enabled with this attribute.  If enabled,
434 only whitespace is allowed between the tagname and the final \*(L">\*(R".
435 .IP "$p\->strict_names" 4
436 .IX Item "$p->strict_names"
437 .PD 0
438 .ie n .IP "$p\->strict_names( $bool )" 4
439 .el .IP "$p\->strict_names( \f(CW$bool\fR )" 4
440 .IX Item "$p->strict_names( $bool )"
441 .PD
442 By default, almost anything is allowed in tag and attribute names.
443 This is the behaviour of most popular browsers and allows us to parse
444 some broken tags with invalid attribute values like:
445 .Sp
446 .Vb 1
447 \&   <IMG SRC=newprevlstGr.gif ALT=[PREV LIST] BORDER=0>
448 .Ve
449 .Sp
450 By default, \*(L"\s-1LIST\s0]\*(R" is parsed as a boolean attribute, not as
451 part of the \s-1ALT\s0 value as was clearly intended.  This is also what
452 Mozilla sees.
453 .Sp
454 The official behaviour is enabled by enabling this attribute.  If
455 enabled, it will cause the tag above to be reported as text
456 since \*(L"\s-1LIST\s0]\*(R" is not a legal attribute name.
457 .IP "$p\->unbroken_text" 4
458 .IX Item "$p->unbroken_text"
459 .PD 0
460 .ie n .IP "$p\->unbroken_text( $bool )" 4
461 .el .IP "$p\->unbroken_text( \f(CW$bool\fR )" 4
462 .IX Item "$p->unbroken_text( $bool )"
463 .PD
464 By default, blocks of text are given to the text handler as soon as
465 possible (but the parser takes care always to break text at a
466 boundary between whitespace and non-whitespace so single words and
467 entities can always be decoded safely).  This might create breaks that
468 make it hard to do transformations on the text. When this attribute is
469 enabled, blocks of text are always reported in one piece.  This will
470 delay the text event until the following (non\-text) event has been
471 recognized by the parser.
472 .Sp
473 Note that the \f(CW\*(C`offset\*(C'\fR argspec will give you the offset of the first
474 segment of text and \f(CW\*(C`length\*(C'\fR is the combined length of the segments.
475 Since there might be ignored tags in between, these numbers can't be
476 used to directly index in the original document file.
477 .IP "$p\->utf8_mode" 4
478 .IX Item "$p->utf8_mode"
479 .PD 0
480 .ie n .IP "$p\->utf8_mode( $bool )" 4
481 .el .IP "$p\->utf8_mode( \f(CW$bool\fR )" 4
482 .IX Item "$p->utf8_mode( $bool )"
483 .PD
484 Enable this option when parsing raw undecoded \s-1UTF\-8\s0.  This tells the
485 parser that the entities expanded for strings reported by \f(CW\*(C`attr\*(C'\fR,
486 \&\f(CW@attr\fR and \f(CW\*(C`dtext\*(C'\fR should be expanded as decoded \s-1UTF\-8\s0 so they end
487 up compatible with the surrounding text.
488 .Sp
489 If \f(CW\*(C`utf8_mode\*(C'\fR is enabled then it is an error to pass strings
490 containing characters with code above 255 to the \fIparse()\fR method, and
491 the \fIparse()\fR method will croak if you try.
492 .Sp
493 Example: The Unicode character \*(L"\ex{2665}\*(R" is \*(L"\exE2\ex99\exA5\*(R" when \s-1UTF\-8\s0
494 encoded.  The character can also be represented by the entity
495 \&\*(L"&hearts;\*(R" or \*(L"&#x2665\*(R".  If we feed the parser:
496 .Sp
497 .Vb 1
498 \&  $p\->parse("\exE2\ex99\exA5&hearts;");
499 .Ve
500 .Sp
501 then \f(CW\*(C`dtext\*(C'\fR will be reported as \*(L"\exE2\ex99\exA5\ex{2665}\*(R" without
502 \&\f(CW\*(C`utf8_mode\*(C'\fR enabled, but as \*(L"\exE2\ex99\exA5\exE2\ex99\exA5\*(R" when enabled.
503 The later string is what you want.
504 .Sp
505 This option is only available with perl\-5.8 or better.
506 .IP "$p\->xml_mode" 4
507 .IX Item "$p->xml_mode"
508 .PD 0
509 .ie n .IP "$p\->xml_mode( $bool )" 4
510 .el .IP "$p\->xml_mode( \f(CW$bool\fR )" 4
511 .IX Item "$p->xml_mode( $bool )"
512 .PD
513 Enabling this attribute changes the parser to allow some \s-1XML\s0
514 constructs.  This enables the behaviour controlled by individually by
515 the \f(CW\*(C`case_sensitive\*(C'\fR, \f(CW\*(C`empty_element_tags\*(C'\fR, \f(CW\*(C`strict_names\*(C'\fR and
516 \&\f(CW\*(C`xml_pic\*(C'\fR attributes and also suppresses special treatment of
517 elements that are parsed as \s-1CDATA\s0 for \s-1HTML\s0.
518 .IP "$p\->xml_pic" 4
519 .IX Item "$p->xml_pic"
520 .PD 0
521 .ie n .IP "$p\->xml_pic( $bool )" 4
522 .el .IP "$p\->xml_pic( \f(CW$bool\fR )" 4
523 .IX Item "$p->xml_pic( $bool )"
524 .PD
525 By default, \fIprocessing instructions\fR are terminated by \*(L">\*(R". When
526 this attribute is enabled, processing instructions are terminated by
527 \&\*(L"?>\*(R" instead.
528 .PP
529 As markup and text is recognized, handlers are invoked.  The following
530 method is used to set up handlers for different events:
531 .ie n .IP "$p\->handler( event => \e&subroutine, $argspec )" 4
532 .el .IP "$p\->handler( event => \e&subroutine, \f(CW$argspec\fR )" 4
533 .IX Item "$p->handler( event => &subroutine, $argspec )"
534 .PD 0
535 .ie n .IP "$p\->handler( event => $method_name\fR, \f(CW$argspec )" 4
536 .el .IP "$p\->handler( event => \f(CW$method_name\fR, \f(CW$argspec\fR )" 4
537 .IX Item "$p->handler( event => $method_name, $argspec )"
538 .ie n .IP "$p\->handler( event => \e@accum, $argspec )" 4
539 .el .IP "$p\->handler( event => \e@accum, \f(CW$argspec\fR )" 4
540 .IX Item "$p->handler( event => @accum, $argspec )"
541 .ie n .IP "$p\->handler( event => """" );" 4
542 .el .IP "$p\->handler( event => ``'' );" 4
543 .IX Item "$p->handler( event => """" );"
544 .IP "$p\->handler( event => undef );" 4
545 .IX Item "$p->handler( event => undef );"
546 .IP "$p\->handler( event );" 4
547 .IX Item "$p->handler( event );"
548 .PD
549 This method assigns a subroutine, method, or array to handle an event.
550 .Sp
551 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,
552 \&\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.
553 .Sp
554 The \f(CW\*(C`\e&subroutine\*(C'\fR is a reference to a subroutine which is called to handle
555 the event.
556 .Sp
557 The \f(CW$method_name\fR is the name of a method of \f(CW$p\fR which is called to handle
558 the event.
559 .Sp
560 The \f(CW@accum\fR is an array that will hold the event information as
561 sub\-arrays.
562 .Sp
563 If the second argument is "", the event is ignored.
564 If it is undef, the default handler is invoked for the event.
565 .Sp
566 The \f(CW$argspec\fR is a string that describes the information to be reported
567 for the event.  Any requested information that does not apply to a
568 specific event is passed as \f(CW\*(C`undef\*(C'\fR.  If argspec is omitted, then it
569 is left unchanged.
570 .Sp
571 The return value from \f(CW$p\fR\->handler is the old callback routine or a
572 reference to the accumulator array.
573 .Sp
574 Any return values from handler callback routines/methods are always
575 ignored.  A handler callback can request parsing to be aborted by
576 invoking the \f(CW$p\fR\->eof method.  A handler callback is not allowed to
577 invoke the \f(CW$p\fR\->\fIparse()\fR or \f(CW$p\fR\->\fIparse_file()\fR method.  An exception will
578 be raised if it tries.
579 .Sp
580 Examples:
581 .Sp
582 .Vb 1
583 \&    $p\->handler(start =>  "start", 'self, attr, attrseq, text' );
584 .Ve
585 .Sp
586 This causes the \*(L"start\*(R" method of object \f(CW$p\fR to be called for 'start' events.
587 The callback signature is \f(CW$p\fR\->start(\e%attr, \e@attr_seq, \f(CW$text\fR).
588 .Sp
589 .Vb 1
590 \&    $p\->handler(start =>  \e&start, 'attr, attrseq, text' );
591 .Ve
592 .Sp
593 This causes subroutine \fIstart()\fR to be called for 'start' events.
594 The callback signature is start(\e%attr, \e@attr_seq, \f(CW$text\fR).
595 .Sp
596 .Vb 1
597 \&    $p\->handler(start =>  \e@accum, '"S", attr, attrseq, text' );
598 .Ve
599 .Sp
600 This causes 'start' event information to be saved in \f(CW@accum\fR.
601 The array elements will be ['S', \e%attr, \e@attr_seq, \f(CW$text\fR].
602 .Sp
603 .Vb 1
604 \&   $p\->handler(start => "");
605 .Ve
606 .Sp
607 This causes 'start' events to be ignored.  It also suppresses
608 invocations of any default handler for start events.  It is in most
609 cases equivalent to \f(CW$p\fR\->handler(start => sub {}), but is more
610 efficient.  It is different from the empty-sub-handler in that
611 \&\f(CW\*(C`skipped_text\*(C'\fR is not reset by it.
612 .Sp
613 .Vb 1
614 \&   $p\->handler(start => undef);
615 .Ve
616 .Sp
617 This causes no handler to be associated with start events.
618 If there is a default handler it will be invoked.
619 .PP
620 Filters based on tags can be set up to limit the number of events
621 reported.  The main bottleneck during parsing is often the huge number
622 of callbacks made from the parser.  Applying filters can improve
623 performance significantly.
624 .PP
625 The following methods control filters:
626 .ie n .IP "$p\->ignore_elements( @tags )" 4
627 .el .IP "$p\->ignore_elements( \f(CW@tags\fR )" 4
628 .IX Item "$p->ignore_elements( @tags )"
629 Both the \f(CW\*(C`start\*(C'\fR event and the \f(CW\*(C`end\*(C'\fR event as well as any events that
630 would be reported in between are suppressed.  The ignored elements can
631 contain nested occurrences of itself.  Example:
632 .Sp
633 .Vb 1
634 \&   $p\->ignore_elements(qw(script style));
635 .Ve
636 .Sp
637 The \f(CW\*(C`script\*(C'\fR and \f(CW\*(C`style\*(C'\fR tags will always nest properly since their
638 content is parsed in \s-1CDATA\s0 mode.  For most other tags
639 \&\f(CW\*(C`ignore_elements\*(C'\fR must be used with caution since \s-1HTML\s0 is often not
640 \&\fIwell formed\fR.
641 .ie n .IP "$p\->ignore_tags( @tags )" 4
642 .el .IP "$p\->ignore_tags( \f(CW@tags\fR )" 4
643 .IX Item "$p->ignore_tags( @tags )"
644 Any \f(CW\*(C`start\*(C'\fR and \f(CW\*(C`end\*(C'\fR events involving any of the tags given are
645 suppressed.  To reset the filter (i.e. don't suppress any \f(CW\*(C`start\*(C'\fR and
646 \&\f(CW\*(C`end\*(C'\fR events), call \f(CW\*(C`ignore_tags\*(C'\fR without an argument.
647 .ie n .IP "$p\->report_tags( @tags )" 4
648 .el .IP "$p\->report_tags( \f(CW@tags\fR )" 4
649 .IX Item "$p->report_tags( @tags )"
650 Any \f(CW\*(C`start\*(C'\fR and \f(CW\*(C`end\*(C'\fR events involving any of the tags \fInot\fR given
651 are suppressed.  To reset the filter (i.e. report all \f(CW\*(C`start\*(C'\fR and
652 \&\f(CW\*(C`end\*(C'\fR events), call \f(CW\*(C`report_tags\*(C'\fR without an argument.
653 .PP
654 Internally, the system has two filter lists, one for \f(CW\*(C`report_tags\*(C'\fR
655 and one for \f(CW\*(C`ignore_tags\*(C'\fR, and both filters are applied.  This
656 effectively gives \f(CW\*(C`ignore_tags\*(C'\fR precedence over \f(CW\*(C`report_tags\*(C'\fR.
657 .PP
658 Examples:
659 .PP
660 .Vb 2
661 \&   $p\->ignore_tags(qw(style));
662 \&   $p\->report_tags(qw(script style));
663 .Ve
664 .PP
665 results in only \f(CW\*(C`script\*(C'\fR events being reported.
666 .Sh "Argspec"
667 .IX Subsection "Argspec"
668 Argspec is a string containing a comma-separated list that describes
669 the information reported by the event.  The following argspec
670 identifier names can be used:
671 .ie n .IP """attr""" 4
672 .el .IP "\f(CWattr\fR" 4
673 .IX Item "attr"
674 Attr causes a reference to a hash of attribute name/value pairs to be
675 passed.
676 .Sp
677 Boolean attributes' values are either the value set by
678 \&\f(CW$p\fR\->boolean_attribute_value, or the attribute name if no value has been
679 set by \f(CW$p\fR\->boolean_attribute_value.
680 .Sp
681 This passes undef except for \f(CW\*(C`start\*(C'\fR events.
682 .Sp
683 Unless \f(CW\*(C`xml_mode\*(C'\fR or \f(CW\*(C`case_sensitive\*(C'\fR is enabled, the attribute
684 names are forced to lower case.
685 .Sp
686 General entities are decoded in the attribute values and
687 one layer of matching quotes enclosing the attribute values is removed.
688 .Sp
689 The Unicode character set is assumed for entity decoding.  With Perl
690 version 5.6 or earlier only the Latin\-1 range is supported, and
691 entities for characters outside the range 0..255 are left unchanged.
692 .ie n .IP "@attr" 4
693 .el .IP "\f(CW@attr\fR" 4
694 .IX Item "@attr"
695 Basically the same as \f(CW\*(C`attr\*(C'\fR, but keys and values are passed as
696 individual arguments and the original sequence of the attributes is
697 kept.  The parameters passed will be the same as the \f(CW@attr\fR calculated
698 here:
699 .Sp
700 .Vb 1
701 \&   @attr = map { $_ => $attr\->{$_} } @$attrseq;
702 .Ve
703 .Sp
704 assuming \f(CW$attr\fR and \f(CW$attrseq\fR here are the hash and array passed as the
705 result of \f(CW\*(C`attr\*(C'\fR and \f(CW\*(C`attrseq\*(C'\fR argspecs.
706 .Sp
707 This passes no values for events besides \f(CW\*(C`start\*(C'\fR.
708 .ie n .IP """attrseq""" 4
709 .el .IP "\f(CWattrseq\fR" 4
710 .IX Item "attrseq"
711 Attrseq causes a reference to an array of attribute names to be
712 passed.  This can be useful if you want to walk the \f(CW\*(C`attr\*(C'\fR hash in
713 the original sequence.
714 .Sp
715 This passes undef except for \f(CW\*(C`start\*(C'\fR events.
716 .Sp
717 Unless \f(CW\*(C`xml_mode\*(C'\fR or \f(CW\*(C`case_sensitive\*(C'\fR is enabled, the attribute
718 names are forced to lower case.
719 .ie n .IP """column""" 4
720 .el .IP "\f(CWcolumn\fR" 4
721 .IX Item "column"
722 Column causes the column number of the start of the event to be passed.
723 The first column on a line is 0.
724 .ie n .IP """dtext""" 4
725 .el .IP "\f(CWdtext\fR" 4
726 .IX Item "dtext"
727 Dtext causes the decoded text to be passed.  General entities are
728 automatically decoded unless the event was inside a \s-1CDATA\s0 section or
729 was between literal start and end tags (\f(CW\*(C`script\*(C'\fR, \f(CW\*(C`style\*(C'\fR,
730 \&\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).
731 .Sp
732 The Unicode character set is assumed for entity decoding.  With Perl
733 version 5.6 or earlier only the Latin\-1 range is supported, and
734 entities for characters outside the range 0..255 are left unchanged.
735 .Sp
736 This passes undef except for \f(CW\*(C`text\*(C'\fR events.
737 .ie n .IP """event""" 4
738 .el .IP "\f(CWevent\fR" 4
739 .IX Item "event"
740 Event causes the event name to be passed.
741 .Sp
742 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,
743 \&\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.
744 .ie n .IP """is_cdata""" 4
745 .el .IP "\f(CWis_cdata\fR" 4
746 .IX Item "is_cdata"
747 Is_cdata causes a \s-1TRUE\s0 value to be passed if the event is inside a \s-1CDATA\s0
748 section or between literal start and end tags (\f(CW\*(C`script\*(C'\fR,
749 \&\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).
750 .Sp
751 if the flag is \s-1FALSE\s0 for a text event, then you should normally
752 either use \f(CW\*(C`dtext\*(C'\fR or decode the entities yourself before the text is
753 processed further.
754 .ie n .IP """length""" 4
755 .el .IP "\f(CWlength\fR" 4
756 .IX Item "length"
757 Length causes the number of bytes of the source text of the event to
758 be passed.
759 .ie n .IP """line""" 4
760 .el .IP "\f(CWline\fR" 4
761 .IX Item "line"
762 Line causes the line number of the start of the event to be passed.
763 The first line in the document is 1.  Line counting doesn't start
764 until at least one handler requests this value to be reported.
765 .ie n .IP """offset""" 4
766 .el .IP "\f(CWoffset\fR" 4
767 .IX Item "offset"
768 Offset causes the byte position in the \s-1HTML\s0 document of the start of
769 the event to be passed.  The first byte in the document has offset 0.
770 .ie n .IP """offset_end""" 4
771 .el .IP "\f(CWoffset_end\fR" 4
772 .IX Item "offset_end"
773 Offset_end causes the byte position in the \s-1HTML\s0 document of the end of
774 the event to be passed.  This is the same as \f(CW\*(C`offset\*(C'\fR + \f(CW\*(C`length\*(C'\fR.
775 .ie n .IP """self""" 4
776 .el .IP "\f(CWself\fR" 4
777 .IX Item "self"
778 Self causes the current object to be passed to the handler.  If the
779 handler is a method, this must be the first element in the argspec.
780 .Sp
781 An alternative to passing self as an argspec is to register closures
782 that capture \f(CW$self\fR by themselves as handlers.  Unfortunately this
783 creates circular references which prevent the HTML::Parser object
784 from being garbage collected.  Using the \f(CW\*(C`self\*(C'\fR argspec avoids this
785 problem.
786 .ie n .IP """skipped_text""" 4
787 .el .IP "\f(CWskipped_text\fR" 4
788 .IX Item "skipped_text"
789 Skipped_text returns the concatenated text of all the events that have
790 been skipped since the last time an event was reported.  Events might
791 be skipped because no handler is registered for them or because some
792 filter applies.  Skipped text also includes marked section markup,
793 since there are no events that can catch it.
794 .Sp
795 If an \f(CW""\fR\-handler is registered for an event, then the text for this
796 event is not included in \f(CW\*(C`skipped_text\*(C'\fR.  Skipped text both before
797 and after the \f(CW""\fR\-event is included in the next reported
798 \&\f(CW\*(C`skipped_text\*(C'\fR.
799 .ie n .IP """tag""" 4
800 .el .IP "\f(CWtag\fR" 4
801 .IX Item "tag"
802 Same as \f(CW\*(C`tagname\*(C'\fR, but prefixed with \*(L"/\*(R" if it belongs to an \f(CW\*(C`end\*(C'\fR
803 event and \*(L"!\*(R" for a declaration.  The \f(CW\*(C`tag\*(C'\fR does not have any prefix
804 for \f(CW\*(C`start\*(C'\fR events, and is in this case identical to \f(CW\*(C`tagname\*(C'\fR.
805 .ie n .IP """tagname""" 4
806 .el .IP "\f(CWtagname\fR" 4
807 .IX Item "tagname"
808 This is the element name (or \fIgeneric identifier\fR in \s-1SGML\s0 jargon) for
809 start and end tags.  Since \s-1HTML\s0 is case insensitive, this name is
810 forced to lower case to ease string matching.
811 .Sp
812 Since \s-1XML\s0 is case sensitive, the tagname case is not changed when
813 \&\f(CW\*(C`xml_mode\*(C'\fR is enabled.  The same happens if the \f(CW\*(C`case_sensitive\*(C'\fR attribute
814 is set.
815 .Sp
816 The declaration type of declaration elements is also passed as a tagname,
817 even if that is a bit strange.
818 In fact, in the current implementation tagname is
819 identical to \f(CW\*(C`token0\*(C'\fR except that the name may be forced to lower case.
820 .ie n .IP """token0""" 4
821 .el .IP "\f(CWtoken0\fR" 4
822 .IX Item "token0"
823 Token0 causes the original text of the first token string to be
824 passed.  This should always be the same as \f(CW$tokens\fR\->[0].
825 .Sp
826 For \f(CW\*(C`declaration\*(C'\fR events, this is the declaration type.
827 .Sp
828 For \f(CW\*(C`start\*(C'\fR and \f(CW\*(C`end\*(C'\fR events, this is the tag name.
829 .Sp
830 For \f(CW\*(C`process\*(C'\fR and non-strict \f(CW\*(C`comment\*(C'\fR events, this is everything
831 inside the tag.
832 .Sp
833 This passes undef if there are no tokens in the event.
834 .ie n .IP """tokenpos""" 4
835 .el .IP "\f(CWtokenpos\fR" 4
836 .IX Item "tokenpos"
837 Tokenpos causes a reference to an array of token positions to be
838 passed.  For each string that appears in \f(CW\*(C`tokens\*(C'\fR, this array
839 contains two numbers.  The first number is the offset of the start of
840 the token in the original \f(CW\*(C`text\*(C'\fR and the second number is the length
841 of the token.
842 .Sp
843 Boolean attributes in a \f(CW\*(C`start\*(C'\fR event will have (0,0) for the
844 attribute value offset and length.
845 .Sp
846 This passes undef if there are no tokens in the event (e.g., \f(CW\*(C`text\*(C'\fR)
847 and for artificial \f(CW\*(C`end\*(C'\fR events triggered by empty element tags.
848 .Sp
849 If you are using these offsets and lengths to modify \f(CW\*(C`text\*(C'\fR, you
850 should either work from right to left, or be very careful to calculate
851 the changes to the offsets.
852 .ie n .IP """tokens""" 4
853 .el .IP "\f(CWtokens\fR" 4
854 .IX Item "tokens"
855 Tokens causes a reference to an array of token strings to be passed.
856 The strings are exactly as they were found in the original text,
857 no decoding or case changes are applied.
858 .Sp
859 For \f(CW\*(C`declaration\*(C'\fR events, the array contains each word, comment, and
860 delimited string starting with the declaration type.
861 .Sp
862 For \f(CW\*(C`comment\*(C'\fR events, this contains each sub\-comment.  If
863 \&\f(CW$p\fR\->strict_comments is disabled, there will be only one sub\-comment.
864 .Sp
865 For \f(CW\*(C`start\*(C'\fR events, this contains the original tag name followed by
866 the attribute name/value pairs.  The values of boolean attributes will
867 be either the value set by \f(CW$p\fR\->boolean_attribute_value, or the
868 attribute name if no value has been set by
869 \&\f(CW$p\fR\->boolean_attribute_value.
870 .Sp
871 For \f(CW\*(C`end\*(C'\fR events, this contains the original tag name (always one token).
872 .Sp
873 For \f(CW\*(C`process\*(C'\fR events, this contains the process instructions (always one
874 token).
875 .Sp
876 This passes \f(CW\*(C`undef\*(C'\fR for \f(CW\*(C`text\*(C'\fR events.
877 .ie n .IP """text""" 4
878 .el .IP "\f(CWtext\fR" 4
879 .IX Item "text"
880 Text causes the source text (including markup element delimiters) to be
881 passed.
882 .ie n .IP """undef""" 4
883 .el .IP "\f(CWundef\fR" 4
884 .IX Item "undef"
885 Pass an undefined value.  Useful as padding where the same handler
886 routine is registered for multiple events.
887 .ie n .IP "'...'" 4
888 .el .IP "\f(CW'...'\fR" 4
889 .IX Item "'...'"
890 A literal string of 0 to 255 characters enclosed
891 in single (') or double (") quotes is passed as entered.
892 .PP
893 The whole argspec string can be wrapped up in \f(CW'@{...}'\fR to signal
894 that the resulting event array should be flattened.  This only makes a
895 difference if an array reference is used as the handler target.
896 Consider this example:
897 .PP
898 .Vb 2
899 \&   $p\->handler(text => [], 'text');
900 \&   $p\->handler(text => [], '@{text}']);
901 .Ve
902 .PP
903 With two text events; \f(CW"foo"\fR, \f(CW"bar"\fR; then the first example will end
904 up with [[\*(L"foo\*(R"], [\*(L"bar\*(R"]] and the second with [\*(L"foo\*(R", \*(L"bar\*(R"] in
905 the handler target array.
906 .Sh "Events"
907 .IX Subsection "Events"
908 Handlers for the following events can be registered:
909 .ie n .IP """comment""" 4
910 .el .IP "\f(CWcomment\fR" 4
911 .IX Item "comment"
912 This event is triggered when a markup comment is recognized.
913 .Sp
914 Example:
915 .Sp
916 .Vb 1
917 \&  <!\-\- This is a comment \-\- \-\- So is this \-\->
918 .Ve
919 .ie n .IP """declaration""" 4
920 .el .IP "\f(CWdeclaration\fR" 4
921 .IX Item "declaration"
922 This event is triggered when a \fImarkup declaration\fR is recognized.
923 .Sp
924 For typical \s-1HTML\s0 documents, the only declaration you are
925 likely to find is <!DOCTYPE ...>.
926 .Sp
927 Example:
928 .Sp
929 .Vb 2
930 \&  <!DOCTYPE HTML PUBLIC "\-//W3C//DTD HTML 4.01//EN"
931 \&  "http://www.w3.org/TR/html40/strict.dtd">
932 .Ve
933 .Sp
934 DTDs inside <!DOCTYPE ...> will confuse HTML::Parser.
935 .ie n .IP """default""" 4
936 .el .IP "\f(CWdefault\fR" 4
937 .IX Item "default"
938 This event is triggered for events that do not have a specific
939 handler.  You can set up a handler for this event to catch stuff you
940 did not want to catch explicitly.
941 .ie n .IP """end""" 4
942 .el .IP "\f(CWend\fR" 4
943 .IX Item "end"
944 This event is triggered when an end tag is recognized.
945 .Sp
946 Example:
947 .Sp
948 .Vb 1
949 \&  </A>
950 .Ve
951 .ie n .IP """end_document""" 4
952 .el .IP "\f(CWend_document\fR" 4
953 .IX Item "end_document"
954 This event is triggered when \f(CW$p\fR\->eof is called and after any remaining
955 text is flushed.  There is no document text associated with this event.
956 .ie n .IP """process""" 4
957 .el .IP "\f(CWprocess\fR" 4
958 .IX Item "process"
959 This event is triggered when a processing instructions markup is
960 recognized.
961 .Sp
962 The format and content of processing instructions are system and
963 application dependent.
964 .Sp
965 Examples:
966 .Sp
967 .Vb 2
968 \&  <? HTML processing instructions >
969 \&  <? XML processing instructions ?>
970 .Ve
971 .ie n .IP """start""" 4
972 .el .IP "\f(CWstart\fR" 4
973 .IX Item "start"
974 This event is triggered when a start tag is recognized.
975 .Sp
976 Example:
977 .Sp
978 .Vb 1
979 \&  <A HREF="http://www.perl.com/">
980 .Ve
981 .ie n .IP """start_document""" 4
982 .el .IP "\f(CWstart_document\fR" 4
983 .IX Item "start_document"
984 This event is triggered before any other events for a new document.  A
985 handler for it can be used to initialize stuff.  There is no document
986 text associated with this event.
987 .ie n .IP """text""" 4
988 .el .IP "\f(CWtext\fR" 4
989 .IX Item "text"
990 This event is triggered when plain text (characters) is recognized.
991 The text may contain multiple lines.  A sequence of text may be broken
992 between several text events unless \f(CW$p\fR\->unbroken_text is enabled.
993 .Sp
994 The parser will make sure that it does not break a word or a sequence
995 of whitespace between two text events.
996 .Sh "Unicode"
997 .IX Subsection "Unicode"
998 The \f(CW\*(C`HTML::Parser\*(C'\fR can parse Unicode strings when running under
999 perl\-5.8 or better.  If Unicode is passed to \f(CW$p\fR\->\fIparse()\fR then chunks
1000 of Unicode will be reported to the handlers.  The offset and length
1001 argspecs will also report their position in terms of characters.
1002 .PP
1003 It is safe to parse raw undecoded \s-1UTF\-8\s0 if you either avoid decoding
1004 entities and make sure to not use \fIargspecs\fR that do, or enable the
1005 \&\f(CW\*(C`utf8_mode\*(C'\fR for the parser.  Parsing of undecoded \s-1UTF\-8\s0 might be
1006 useful when parsing from a file where you need the reported offsets
1007 and lengths to match the byte offsets in the file.
1008 .PP
1009 If a filename is passed to \f(CW$p\fR\->\fIparse_file()\fR then the file will be read
1010 in binary mode.  This will be fine if the file contains only \s-1ASCII\s0 or
1011 Latin\-1 characters.  If the file contains \s-1UTF\-8\s0 encoded text then care
1012 must be taken when decoding entities as described in the previous
1013 paragraph, but better is to open the file with the \s-1UTF\-8\s0 layer so that
1014 it is decoded properly:
1015 .PP
1016 .Vb 2
1017 \&   open(my $fh, "<:utf8", "index.html") || die "...: $!";
1018 \&   $p\->parse_file($fh);
1019 .Ve
1020 .PP
1021 If the file contains text encoded in a charset besides \s-1ASCII\s0, Latin\-1
1022 or \s-1UTF\-8\s0 then decoding will always be needed.
1023 .SH "VERSION 2 COMPATIBILITY"
1024 .IX Header "VERSION 2 COMPATIBILITY"
1025 When an \f(CW\*(C`HTML::Parser\*(C'\fR object is constructed with no arguments, a set
1026 of handlers is automatically provided that is compatible with the old
1027 HTML::Parser version 2 callback methods.
1028 .PP
1029 This is equivalent to the following method calls:
1030 .PP
1031 .Vb 14
1032 \&   $p\->handler(start   => "start",   "self, tagname, attr, attrseq, text");
1033 \&   $p\->handler(end     => "end",     "self, tagname, text");
1034 \&   $p\->handler(text    => "text",    "self, text, is_cdata");
1035 \&   $p\->handler(process => "process", "self, token0, text");
1036 \&   $p\->handler(comment =>
1037 \&             sub {
1038 \&                 my($self, $tokens) = @_;
1039 \&                 for (@$tokens) {$self\->comment($_);}},
1040 \&             "self, tokens");
1041 \&   $p\->handler(declaration =>
1042 \&             sub {
1043 \&                 my $self = shift;
1044 \&                 $self\->declaration(substr($_[0], 2, \-1));},
1045 \&             "self, text");
1046 .Ve
1047 .PP
1048 Setting up these handlers can also be requested with the \*(L"api_version =>
1049 2\*(R" constructor option.
1050 .SH "SUBCLASSING"
1051 .IX Header "SUBCLASSING"
1052 The \f(CW\*(C`HTML::Parser\*(C'\fR class is subclassable.  Parser objects are plain
1053 hashes and \f(CW\*(C`HTML::Parser\*(C'\fR reserves only hash keys that start with
1054 \&\*(L"_hparser\*(R".  The parser state can be set up by invoking the \fIinit()\fR
1055 method, which takes the same arguments as \fInew()\fR.
1056 .SH "EXAMPLES"
1057 .IX Header "EXAMPLES"
1058 The first simple example shows how you might strip out comments from
1059 an \s-1HTML\s0 document.  We achieve this by setting up a comment handler that
1060 does nothing and a default handler that will print out anything else:
1061 .PP
1062 .Vb 4
1063 \&  use HTML::Parser;
1064 \&  HTML::Parser\->new(default_h => [sub { print shift }, 'text'],
1065 \&                    comment_h => [""],
1066 \&                   )\->parse_file(shift || die) || die $!;
1067 .Ve
1068 .PP
1069 An alternative implementation is:
1070 .PP
1071 .Vb 5
1072 \&  use HTML::Parser;
1073 \&  HTML::Parser\->new(end_document_h => [sub { print shift },
1074 \&                                       'skipped_text'],
1075 \&                    comment_h      => [""],
1076 \&                   )\->parse_file(shift || die) || die $!;
1077 .Ve
1078 .PP
1079 This will in most cases be much more efficient since only a single
1080 callback will be made.
1081 .PP
1082 The next example prints out the text that is inside the <title>
1083 element of an \s-1HTML\s0 document.  Here we start by setting up a start
1084 handler.  When it sees the title start tag it enables a text handler
1085 that prints any text found and an end handler that will terminate
1086 parsing as soon as the title end tag is seen:
1087 .PP
1088 .Vb 1
1089 \&  use HTML::Parser ();
1090 .Ve
1091 .PP
1092 .Vb 8
1093 \&  sub start_handler
1094 \&  {
1095 \&    return if shift ne "title";
1096 \&    my $self = shift;
1097 \&    $self\->handler(text => sub { print shift }, "dtext");
1098 \&    $self\->handler(end  => sub { shift\->eof if shift eq "title"; },
1099 \&                           "tagname,self");
1100 \&  }
1101 .Ve
1102 .PP
1103 .Vb 4
1104 \&  my $p = HTML::Parser\->new(api_version => 3);
1105 \&  $p\->handler( start => \e&start_handler, "tagname,self");
1106 \&  $p\->parse_file(shift || die) || die $!;
1107 \&  print "\en";
1108 .Ve
1109 .PP
1110 More examples are found in the \fIeg/\fR directory of the \f(CW\*(C`HTML\-Parser\*(C'\fR
1111 distribution: the program \f(CW\*(C`hrefsub\*(C'\fR shows how you can edit all links
1112 found in a document; the program \f(CW\*(C`htextsub\*(C'\fR shows how to edit the text only; the
1113 program \f(CW\*(C`hstrip\*(C'\fR shows how you can strip out certain tags/elements
1114 and/or attributes; and the program \f(CW\*(C`htext\*(C'\fR show how to obtain the
1115 plain text, but not any script/style content.
1116 .PP
1117 You can browse the \fIeg/\fR directory online from the \fI[Browse]\fR link on
1118 the http://search.cpan.org/~gaas/HTML\-Parser/ page.
1119 .SH "BUGS"
1120 .IX Header "BUGS"
1121 The <style> and <script> sections do not end with the first \*(L"</\*(R", but
1122 need the complete corresponding end tag.  The standard behaviour is
1123 not really practical.
1124 .PP
1125 When the \fIstrict_comment\fR option is enabled, we still recognize
1126 comments where there is something other than whitespace between even
1127 and odd \*(L"\-\-\*(R" markers.
1128 .PP
1129 Once \f(CW$p\fR\->boolean_attribute_value has been set, there is no way to
1130 restore the default behaviour.
1131 .PP
1132 There is currently no way to get both quote characters
1133 into the same literal argspec.
1134 .PP
1135 Empty tags, e.g. \*(L"<>\*(R" and \*(L"</>\*(R", are not recognized.  \s-1SGML\s0 allows them
1136 to repeat the previous start tag or close the previous start tag
1137 respectively.
1138 .PP
1139 \&\s-1NET\s0 tags, e.g. \*(L"code/.../\*(R" are not recognized.  This is \s-1SGML\s0
1140 shorthand for \*(L"<code>...</code>\*(R".
1141 .PP
1142 Unclosed start or end tags, e.g. \*(L"<tt<b>...</b</tt>\*(R" are not
1143 recognized.
1144 .SH "DIAGNOSTICS"
1145 .IX Header "DIAGNOSTICS"
1146 The following messages may be produced by HTML::Parser.  The notation
1147 in this listing is the same as used in perldiag:
1148 .IP "Not a reference to a hash" 4
1149 .IX Item "Not a reference to a hash"
1150 (F) The object blessed into or subclassed from HTML::Parser is not a
1151 hash as required by the HTML::Parser methods.
1152 .ie n .IP "Bad signature in parser state object at %p" 4
1153 .el .IP "Bad signature in parser state object at \f(CW%p\fR" 4
1154 .IX Item "Bad signature in parser state object at %p"
1155 (F) The _hparser_xs_state element does not refer to a valid state structure.
1156 Something must have changed the internal value
1157 stored in this hash element, or the memory has been overwritten.
1158 .IP "_hparser_xs_state element is not a reference" 4
1159 .IX Item "_hparser_xs_state element is not a reference"
1160 (F) The _hparser_xs_state element has been destroyed.
1161 .IP "Can't find '_hparser_xs_state' element in HTML::Parser hash" 4
1162 .IX Item "Can't find '_hparser_xs_state' element in HTML::Parser hash"
1163 (F) The _hparser_xs_state element is missing from the parser hash.
1164 It was either deleted, or not created when the object was created.
1165 .ie n .IP "\s-1API\s0 version %s\fR not supported by HTML::Parser \f(CW%s" 4
1166 .el .IP "\s-1API\s0 version \f(CW%s\fR not supported by HTML::Parser \f(CW%s\fR" 4
1167 .IX Item "API version %s not supported by HTML::Parser %s"
1168 (F) The constructor option 'api_version' with an argument greater than
1169 or equal to 4 is reserved for future extensions.
1170 .IP "Bad constructor option '%s'" 4
1171 .IX Item "Bad constructor option '%s'"
1172 (F) An unknown constructor option key was passed to the \fInew()\fR or
1173 \&\fIinit()\fR methods.
1174 .IP "Parse loop not allowed" 4
1175 .IX Item "Parse loop not allowed"
1176 (F) A handler invoked the \fIparse()\fR or \fIparse_file()\fR method.
1177 This is not permitted.
1178 .IP "marked sections not supported" 4
1179 .IX Item "marked sections not supported"
1180 (F) The \f(CW$p\fR\->\fImarked_sections()\fR method was invoked in a HTML::Parser
1181 module that was compiled without support for marked sections.
1182 .IP "Unknown boolean attribute (%d)" 4
1183 .IX Item "Unknown boolean attribute (%d)"
1184 (F) Something is wrong with the internal logic that set up aliases for
1185 boolean attributes.
1186 .IP "Only code or array references allowed as handler" 4
1187 .IX Item "Only code or array references allowed as handler"
1188 (F) The second argument for \f(CW$p\fR\->handler must be either a subroutine
1189 reference, then name of a subroutine or method, or a reference to an
1190 array.
1191 .ie n .IP "No handler for %s events" 4
1192 .el .IP "No handler for \f(CW%s\fR events" 4
1193 .IX Item "No handler for %s events"
1194 (F) The first argument to \f(CW$p\fR\->handler must be a valid event name; i.e. one
1195 of \*(L"start\*(R", \*(L"end\*(R", \*(L"text\*(R", \*(L"process\*(R", \*(L"declaration\*(R" or \*(L"comment\*(R".
1196 .ie n .IP "Unrecognized identifier %s in argspec" 4
1197 .el .IP "Unrecognized identifier \f(CW%s\fR in argspec" 4
1198 .IX Item "Unrecognized identifier %s in argspec"
1199 (F) The identifier is not a known argspec name.
1200 Use one of the names mentioned in the argspec section above.
1201 .IP "Literal string is longer than 255 chars in argspec" 4
1202 .IX Item "Literal string is longer than 255 chars in argspec"
1203 (F) The current implementation limits the length of literals in
1204 an argspec to 255 characters.  Make the literal shorter.
1205 .IP "Backslash reserved for literal string in argspec" 4
1206 .IX Item "Backslash reserved for literal string in argspec"
1207 (F) The backslash character \*(L"\e\*(R" is not allowed in argspec literals.
1208 It is reserved to permit quoting inside a literal in a later version.
1209 .IP "Unterminated literal string in argspec" 4
1210 .IX Item "Unterminated literal string in argspec"
1211 (F) The terminating quote character for a literal was not found.
1212 .IP "Bad argspec (%s)" 4
1213 .IX Item "Bad argspec (%s)"
1214 (F) Only identifier names, literals, spaces and commas
1215 are allowed in argspecs.
1216 .IP "Missing comma separator in argspec" 4
1217 .IX Item "Missing comma separator in argspec"
1218 (F) Identifiers in an argspec must be separated with \*(L",\*(R".
1219 .IP "Parsing of undecoded \s-1UTF\-8\s0 will give garbage when decoding entities" 4
1220 .IX Item "Parsing of undecoded UTF-8 will give garbage when decoding entities"
1221 (W) The first chunk parsed appears to contain undecoded \s-1UTF\-8\s0 and one
1222 or more argspecs that decode entities are used for the callback
1223 handlers.
1224 .Sp
1225 The result of decoding will be a mix of encoded and decoded characters
1226 for any entities that expand to characters with code above 127.  This
1227 is not a good thing.
1228 .Sp
1229 The solution is to use the \fIEncode::encode_utf8()\fR on the data before
1230 feeding it to the \f(CW$p\fR\->\fIparse()\fR.  For \f(CW$p\fR\->\fIparse_file()\fR pass a file that
1231 has been opened in \*(L":utf8\*(R" mode.
1232 .Sp
1233 The parser can process raw undecoded \s-1UTF\-8\s0 sanely if the \f(CW\*(C`utf8_mode\*(C'\fR
1234 is enabled or if the \*(L"attr\*(R", \*(L"@attr\*(R" or \*(L"dtext\*(R" argspecs is avoided.
1235 .IP "Parsing string decoded with wrong endianness" 4
1236 .IX Item "Parsing string decoded with wrong endianness"
1237 (W) The first character in the document is U+FFFE.  This is not a
1238 legal Unicode character but a byte swapped \s-1BOM\s0.  The result of parsing
1239 will likely be garbage.
1240 .IP "Parsing of undecoded \s-1UTF\-32\s0" 4
1241 .IX Item "Parsing of undecoded UTF-32"
1242 (W) The parser found the Unicode \s-1UTF\-32\s0 \s-1BOM\s0 signature at the start
1243 of the document.  The result of parsing will likely be garbage.
1244 .IP "Parsing of undecoded \s-1UTF\-16\s0" 4
1245 .IX Item "Parsing of undecoded UTF-16"
1246 (W) The parser found the Unicode \s-1UTF\-16\s0 \s-1BOM\s0 signature at the start of
1247 the document.  The result of parsing will likely be garbage.
1248 .SH "SEE ALSO"
1249 .IX Header "SEE ALSO"
1250 HTML::Entities, HTML::PullParser, HTML::TokeParser, HTML::HeadParser,
1251 HTML::LinkExtor, HTML::Form
1252 .PP
1253 HTML::TreeBuilder (part of the \fIHTML-Tree\fR distribution)
1254 .PP
1255 http://www.w3.org/TR/html4
1256 .PP
1257 More information about marked sections and processing instructions may
1258 be found at \f(CW\*(C`http://www.sgml.u\-net.com/book/sgml\-8.htm\*(C'\fR.
1259 .SH "COPYRIGHT"
1260 .IX Header "COPYRIGHT"
1261 .Vb 2
1262 \& Copyright 1996\-2008 Gisle Aas. All rights reserved.
1263 \& Copyright 1999\-2000 Michael A. Chase.  All rights reserved.
1264 .Ve
1265 .PP
1266 This library is free software; you can redistribute it and/or
1267 modify it under the same terms as Perl itself.