1HTML::HeadParser(3) User Contributed Perl Documentation HTML::HeadParser(3)
2
3
4
6 HTML::HeadParser - Parse <HEAD> section of a HTML document
7
9 require HTML::HeadParser;
10 $p = HTML::HeadParser->new;
11 $p->parse($text) and print "not finished";
12
13 $p->header('Title') # to access <title>....</title>
14 $p->header('Content-Base') # to access <base href="http://...">
15 $p->header('Foo') # to access <meta http-equiv="Foo" content="...">
16 $p->header('X-Meta-Author') # to access <meta name="author" content="...">
17 $p->header('X-Meta-Charset') # to access <meta charset="...">
18
20 The "HTML::HeadParser" is a specialized (and lightweight)
21 "HTML::Parser" that will only parse the <HEAD>...</HEAD> section of an
22 HTML document. The parse() method will return a FALSE value as soon as
23 some <BODY> element or body text are found, and should not be called
24 again after this.
25
26 Note that the "HTML::HeadParser" might get confused if raw undecoded
27 UTF-8 is passed to the parse() method. Make sure the strings are
28 properly decoded before passing them on.
29
30 The "HTML::HeadParser" keeps a reference to a header object, and the
31 parser will update this header object as the various elements of the
32 <HEAD> section of the HTML document are recognized. The following
33 header fields are affected:
34
35 Content-Base:
36 The Content-Base header is initialized from the <base href="...">
37 element.
38
39 Title:
40 The Title header is initialized from the <title>...</title>
41 element.
42
43 Isindex:
44 The Isindex header will be added if there is a <isindex> element in
45 the <head>. The header value is initialized from the prompt
46 attribute if it is present. If no prompt attribute is given it
47 will have '?' as the value.
48
49 X-Meta-Foo:
50 All <meta> elements containing a "name" attribute will result in
51 headers using the prefix "X-Meta-" appended with the value of the
52 "name" attribute as the name of the header, and the value of the
53 "content" attribute as the pushed header value.
54
55 <meta> elements containing a "http-equiv" attribute will result in
56 headers as in above, but without the "X-Meta-" prefix in the header
57 name.
58
59 <meta> elements containing a "charset" attribute will result in an
60 "X-Meta-Charset" header, using the value of the "charset" attribute
61 as the pushed header value.
62
63 The ':' character can't be represented in header field names, so if
64 the meta element contains this char it's substituted with '-'
65 before forming the field name.
66
68 The following methods (in addition to those provided by the superclass)
69 are available:
70
71 $hp = HTML::HeadParser->new
72 $hp = HTML::HeadParser->new( $header )
73 The object constructor. The optional $header argument should be a
74 reference to an object that implement the header() and
75 push_header() methods as defined by the "HTTP::Headers" class.
76 Normally it will be of some class that is a or delegates to the
77 "HTTP::Headers" class.
78
79 If no $header is given "HTML::HeadParser" will create an
80 "HTTP::Headers" object by itself (initially empty).
81
82 $hp->header;
83 Returns a reference to the header object.
84
85 $hp->header( $key )
86 Returns a header value. It is just a shorter way to write
87 "$hp->header->header($key)".
88
90 $h = HTTP::Headers->new;
91 $p = HTML::HeadParser->new($h);
92 $p->parse(<<EOT);
93 <title>Stupid example</title>
94 <base href="http://www.linpro.no/lwp/">
95 Normal text starts here.
96 EOT
97 undef $p;
98 print $h->title; # should print "Stupid example"
99
101 HTML::Parser, HTTP::Headers
102
103 The "HTTP::Headers" class is distributed as part of the libwww-perl
104 package. If you don't have that distribution installed you need to
105 provide the $header argument to the "HTML::HeadParser" constructor with
106 your own object that implements the documented protocol.
107
109 Copyright 1996-2001 Gisle Aas. All rights reserved.
110
111 This library is free software; you can redistribute it and/or modify it
112 under the same terms as Perl itself.
113
114
115
116perl v5.34.0 2021-07-22 HTML::HeadParser(3)