1HTML::PullParser(3) User Contributed Perl Documentation HTML::PullParser(3)
2
3
4
6 HTML::PullParser - Alternative HTML::Parser interface
7
9 use HTML::PullParser;
10
11 $p = HTML::PullParser->new(file => "index.html",
12 start => 'event, tagname, @attr',
13 end => 'event, tagname',
14 ignore_elements => [qw(script style)],
15 ) || die "Can't open: $!";
16 while (my $token = $p->get_token) {
17 #...do something with $token
18 }
19
21 The HTML::PullParser is an alternative interface to the HTML::Parser
22 class. It basically turns the HTML::Parser inside out. You associate
23 a file (or any IO::Handle object or string) with the parser at
24 construction time and then repeatedly call $parser->get_token to obtain
25 the tags and text found in the parsed document.
26
27 The following methods are provided:
28
29 $p = HTML::PullParser->new( file => $file, %options )
30 $p = HTML::PullParser->new( doc => \$doc, %options )
31 A "HTML::PullParser" can be made to parse from either a file or a
32 literal document based on whether the "file" or "doc" option is
33 passed to the parser's constructor.
34
35 The "file" passed in can either be a file name or a file handle
36 object. If a file name is passed, and it can't be opened for
37 reading, then the constructor will return an undefined value and $!
38 will tell you why it failed. Otherwise the argument is taken to be
39 some object that the "HTML::PullParser" can read() from when it
40 needs more data. The stream will be read() until EOF, but not
41 closed.
42
43 A "doc" can be passed plain or as a reference to a scalar. If a
44 reference is passed then the value of this scalar should not be
45 changed before all tokens have been extracted.
46
47 Next the information to be returned for the different token types
48 must be set up. This is done by simply associating an argspec (as
49 defined in HTML::Parser) with the events you have an interest in.
50 For instance, if you want "start" tokens to be reported as the
51 string 'S' followed by the tagname and the attributes you might
52 pass an "start"-option like this:
53
54 $p = HTML::PullParser->new(
55 doc => $document_to_parse,
56 start => '"S", tagname, @attr',
57 end => '"E", tagname',
58 );
59
60 At last other "HTML::Parser" options, like "ignore_tags", and
61 "unbroken_text", can be passed in. Note that you should not use
62 the event_h options to set up parser handlers. That would confuse
63 the inner logic of "HTML::PullParser".
64
65 $token = $p->get_token
66 This method will return the next token found in the HTML document,
67 or "undef" at the end of the document. The token is returned as an
68 array reference. The content of this array match the argspec set
69 up during "HTML::PullParser" construction.
70
71 $p->unget_token( @tokens )
72 If you find out you have read too many tokens you can push them
73 back, so that they are returned again the next time $p->get_token
74 is called.
75
77 The 'eg/hform' script shows how we might parse the form section of
78 HTML::Documents using HTML::PullParser.
79
81 HTML::Parser, HTML::TokeParser
82
84 Copyright 1998-2001 Gisle Aas.
85
86 This library is free software; you can redistribute it and/or modify it
87 under the same terms as Perl itself.
88
89
90
91perl v5.34.1 2022-03-31 HTML::PullParser(3)