1HTML::Form(3) User Contributed Perl Documentation HTML::Form(3)
2
3
4
6 HTML::Form - Class that represents an HTML form element
7
9 use HTML::Form;
10 $form = HTML::Form->parse($html, $base_uri);
11 $form->value(query => "Perl");
12
13 use LWP::UserAgent;
14 $ua = LWP::UserAgent->new;
15 $response = $ua->request($form->click);
16
18 Objects of the "HTML::Form" class represents a single HTML "<form> ...
19 </form>" instance. A form consists of a sequence of inputs that
20 usually have names, and which can take on various values. The state of
21 a form can be tweaked and it can then be asked to provide
22 "HTTP::Request" objects that can be passed to the request() method of
23 "LWP::UserAgent".
24
25 The following methods are available:
26
27 @forms = HTML::Form->parse( $html_document, $base_uri )
28 @forms = HTML::Form->parse( $html_document, base => $base_uri, %opt )
29 @forms = HTML::Form->parse( $response, %opt )
30 The parse() class method will parse an HTML document and build up
31 "HTML::Form" objects for each <form> element found. If called in
32 scalar context only returns the first <form>. Returns an empty
33 list if there are no forms to be found.
34
35 The required arguments is the HTML document to parse
36 ($html_document) and the URI used to retrieve the document
37 ($base_uri). The base URI is needed to resolve relative action
38 URIs. The provided HTML document should be a Unicode string (or
39 US-ASCII).
40
41 By default HTML::Form assumes that the original document was UTF-8
42 encoded and thus encode forms that don't specify an explicit
43 accept-charset as UTF-8. The charset assumed can be overridden by
44 providing the "charset" option to parse(). It's a good idea to be
45 explicit about this parameter as well, thus the recommended
46 simplest invocation becomes:
47
48 my @forms = HTML::Form->parse(
49 Encode::decode($encoding, $html_document_bytes),
50 base => $base_uri,
51 charset => $encoding,
52 );
53
54 If the document was retrieved with LWP then the response object
55 provide methods to obtain a proper value for "base" and "charset":
56
57 my $ua = LWP::UserAgent->new;
58 my $response = $ua->get("http://www.example.com/form.html");
59 my @forms = HTML::Form->parse($response->decoded_content,
60 base => $response->base,
61 charset => $response->content_charset,
62 );
63
64 In fact, the parse() method can parse from an "HTTP::Response"
65 object directly, so the example above can be more conveniently
66 written as:
67
68 my $ua = LWP::UserAgent->new;
69 my $response = $ua->get("http://www.example.com/form.html");
70 my @forms = HTML::Form->parse($response);
71
72 Note that any object that implements a decoded_content(), base()
73 and content_charset() method with similar behaviour as
74 "HTTP::Response" will do.
75
76 Additional options might be passed in to control how the parse
77 method behaves. The following are all the options currently
78 recognized:
79
80 "base => $uri"
81 This is the URI used to retrive the original document. This
82 option is not optional ;-)
83
84 "charset => $str"
85 Specify what charset the original document was encoded in.
86 This is used as the default for accept_charset. If not
87 provided this defaults to "UTF-8".
88
89 "verbose => $bool"
90 Warn (print messages to STDERR) about any bad HTML form
91 constructs found. You can trap these with $SIG{__WARN__}. The
92 default is not to issue warnings.
93
94 "strict => $bool"
95 Initialize any form objects with the given strict attribute.
96 If the strict is turned on the methods that change values of
97 the form will croak if you try to set illegal values or modify
98 readonly fields. The default is not to be strict.
99
100 $method = $form->method
101 $form->method( $new_method )
102 This method is gets/sets the method name used for the
103 "HTTP::Request" generated. It is a string like "GET" or "POST".
104
105 $action = $form->action
106 $form->action( $new_action )
107 This method gets/sets the URI which we want to apply the request
108 method to.
109
110 $enctype = $form->enctype
111 $form->enctype( $new_enctype )
112 This method gets/sets the encoding type for the form data. It is a
113 string like "application/x-www-form-urlencoded" or
114 "multipart/form-data".
115
116 $accept = $form->accept_charset
117 $form->accept_charset( $new_accept )
118 This method gets/sets the list of charset encodings that the server
119 processing the form accepts. Current implementation supports only
120 one-element lists. Default value is "UNKNOWN" which we interpret
121 as a request to use document charset as specified by the 'charset'
122 parameter of the parse() method.
123
124 $value = $form->attr( $name )
125 $form->attr( $name, $new_value )
126 This method give access to the original HTML attributes of the
127 <form> tag. The $name should always be passed in lower case.
128
129 Example:
130
131 @f = HTML::Form->parse( $html, $foo );
132 @f = grep $_->attr("id") eq "foo", @f;
133 die "No form named 'foo' found" unless @f;
134 $foo = shift @f;
135
136 $bool = $form->strict
137 $form->strict( $bool )
138 Gets/sets the strict attribute of a form. If the strict is turned
139 on the methods that change values of the form will croak if you try
140 to set illegal values or modify readonly fields. The default is
141 not to be strict.
142
143 @inputs = $form->inputs
144 This method returns the list of inputs in the form. If called in
145 scalar context it returns the number of inputs contained in the
146 form. See "INPUTS" for what methods are available for the input
147 objects returned.
148
149 $input = $form->find_input( $selector )
150 $input = $form->find_input( $selector, $type )
151 $input = $form->find_input( $selector, $type, $index )
152 This method is used to locate specific inputs within the form. All
153 inputs that match the arguments given are returned. In scalar
154 context only the first is returned, or "undef" if none match.
155
156 If $selector is specified, then the input's name, id, class
157 attribute must match. A selector prefixed with '#' must match the
158 id attribute of the input. A selector prefixed with '.' matches
159 the class attribute. A selector prefixed with '^' or with no
160 prefix matches the name attribute.
161
162 If $type is specified, then the input must have the specified type.
163 The following type names are used: "text", "password", "hidden",
164 "textarea", "file", "image", "submit", "radio", "checkbox" and
165 "option".
166
167 The $index is the sequence number of the input matched where 1 is
168 the first. If combined with $name and/or $type then it select the
169 nth input with the given name and/or type.
170
171 $value = $form->value( $selector )
172 $form->value( $selector, $new_value )
173 The value() method can be used to get/set the value of some input.
174 If strict is enabled and no input has the indicated name, then this
175 method will croak.
176
177 If multiple inputs have the same name, only the first one will be
178 affected.
179
180 The call:
181
182 $form->value('foo')
183
184 is basically a short-hand for:
185
186 $form->find_input('foo')->value;
187
188 @names = $form->param
189 @values = $form->param( $name )
190 $form->param( $name, $value, ... )
191 $form->param( $name, \@values )
192 Alternative interface to examining and setting the values of the
193 form.
194
195 If called without arguments then it returns the names of all the
196 inputs in the form. The names will not repeat even if multiple
197 inputs have the same name. In scalar context the number of
198 different names is returned.
199
200 If called with a single argument then it returns the value or
201 values of inputs with the given name. If called in scalar context
202 only the first value is returned. If no input exists with the
203 given name, then "undef" is returned.
204
205 If called with 2 or more arguments then it will set values of the
206 named inputs. This form will croak if no inputs have the given
207 name or if any of the values provided does not fit. Values can
208 also be provided as a reference to an array. This form will allow
209 unsetting all values with the given name as well.
210
211 This interface resembles that of the param() function of the CGI
212 module.
213
214 $form->try_others( \&callback )
215 This method will iterate over all permutations of unvisited
216 enumerated values (<select>, <radio>, <checkbox>) and invoke the
217 callback for each. The callback is passed the $form as argument.
218 The return value from the callback is ignored and the try_others()
219 method itself does not return anything.
220
221 $request = $form->make_request
222 Will return an "HTTP::Request" object that reflects the current
223 setting of the form. You might want to use the click() method
224 instead.
225
226 $request = $form->click
227 $request = $form->click( $selector )
228 $request = $form->click( $x, $y )
229 $request = $form->click( $selector, $x, $y )
230 Will "click" on the first clickable input (which will be of type
231 "submit" or "image"). The result of clicking is an "HTTP::Request"
232 object that can then be passed to "LWP::UserAgent" if you want to
233 obtain the server response.
234
235 If a $selector is specified, we will click on the first clickable
236 input matching the selector, and the method will croak if no
237 matching clickable input is found. If $selector is not specified,
238 then it is ok if the form contains no clickable inputs. In this
239 case the click() method returns the same request as the
240 make_request() method would do. See description of the
241 find_input() method above for how the $selector is specified.
242
243 If there are multiple clickable inputs with the same name, then
244 there is no way to get the click() method of the "HTML::Form" to
245 click on any but the first. If you need this you would have to
246 locate the input with find_input() and invoke the click() method on
247 the given input yourself.
248
249 A click coordinate pair can also be provided, but this only makes a
250 difference if you clicked on an image. The default coordinate is
251 (1,1). The upper-left corner of the image is (0,0), but some badly
252 coded CGI scripts are known to not recognize this. Therefore (1,1)
253 was selected as a safer default.
254
255 @kw = $form->form
256 Returns the current setting as a sequence of key/value pairs. Note
257 that keys might be repeated, which means that some values might be
258 lost if the return values are assigned to a hash.
259
260 In scalar context this method returns the number of key/value pairs
261 generated.
262
263 $form->dump
264 Returns a textual representation of current state of the form.
265 Mainly useful for debugging. If called in void context, then the
266 dump is printed on STDERR.
267
269 An "HTML::Form" objects contains a sequence of inputs. References to
270 the inputs can be obtained with the $form->inputs or $form->find_input
271 methods.
272
273 Note that there is not a one-to-one correspondence between input
274 objects and <input> elements in the HTML document. An input object
275 basically represents a name/value pair, so when multiple HTML elements
276 contribute to the same name/value pair in the submitted form they are
277 combined.
278
279 The input elements that are mapped one-to-one are "text", "textarea",
280 "password", "hidden", "file", "image", "submit" and "checkbox". For
281 the "radio" and "option" inputs the story is not as simple: All <input
282 type="radio"> elements with the same name will contribute to the same
283 input radio object. The number of radio input objects will be the same
284 as the number of distinct names used for the <input type="radio">
285 elements. For a <select> element without the "multiple" attribute
286 there will be one input object of type of "option". For a <select
287 multiple> element there will be one input object for each contained
288 <option> element. Each one of these option objects will have the same
289 name.
290
291 The following methods are available for the input objects:
292
293 $input->type
294 Returns the type of this input. The type is one of the following
295 strings: "text", "password", "hidden", "textarea", "file", "image",
296 "submit", "radio", "checkbox" or "option".
297
298 $name = $input->name
299 $input->name( $new_name )
300 This method can be used to get/set the current name of the input.
301
302 $input->id
303 $input->class
304 These methods can be used to get/set the current id or class
305 attribute for the input.
306
307 $input->selected( $selector )
308 Returns TRUE if the given selector matched the input. See the
309 description of the find_input() method above for a description of
310 the selector syntax.
311
312 $value = $input->value
313 $input->value( $new_value )
314 This method can be used to get/set the current value of an input.
315
316 If strict is enabled and the input only can take an enumerated list
317 of values, then it is an error to try to set it to something else
318 and the method will croak if you try.
319
320 You will also be able to set the value of read-only inputs, but a
321 warning will be generated if running under "perl -w".
322
323 $input->possible_values
324 Returns a list of all values that an input can take. For inputs
325 that do not have discrete values, this returns an empty list.
326
327 $input->other_possible_values
328 Returns a list of all values not tried yet.
329
330 $input->value_names
331 For some inputs the values can have names that are different from
332 the values themselves. The number of names returned by this method
333 will match the number of values reported by
334 $input->possible_values.
335
336 When setting values using the value() method it is also possible to
337 use the value names in place of the value itself.
338
339 $bool = $input->readonly
340 $input->readonly( $bool )
341 This method is used to get/set the value of the readonly attribute.
342 You are allowed to modify the value of readonly inputs, but setting
343 the value will generate some noise when warnings are enabled.
344 Hidden fields always start out readonly.
345
346 $bool = $input->disabled
347 $input->disabled( $bool )
348 This method is used to get/set the value of the disabled attribute.
349 Disabled inputs do not contribute any key/value pairs for the form
350 value.
351
352 $input->form_name_value
353 Returns a (possible empty) list of key/value pairs that should be
354 incorporated in the form value from this input.
355
356 $input->check
357 Some input types represent toggles that can be turned on/off. This
358 includes "checkbox" and "option" inputs. Calling this method turns
359 this input on without having to know the value name. If the input
360 is already on, then nothing happens.
361
362 This has the same effect as:
363
364 $input->value($input->possible_values[1]);
365
366 The input can be turned off with:
367
368 $input->value(undef);
369
370 $input->click($form, $x, $y)
371 Some input types (currently "submit" buttons and "images") can be
372 clicked to submit the form. The click() method returns the
373 corresponding "HTTP::Request" object.
374
375 If the input is of type "file", then it has these additional methods:
376
377 $input->file
378 This is just an alias for the value() method. It sets the filename
379 to read data from.
380
381 For security reasons this field will never be initialized from the
382 parsing of a form. This prevents the server from triggering
383 stealth uploads of arbitrary files from the client machine.
384
385 $filename = $input->filename
386 $input->filename( $new_filename )
387 This get/sets the filename reported to the server during file
388 upload. This attribute defaults to the value reported by the
389 file() method.
390
391 $content = $input->content
392 $input->content( $new_content )
393 This get/sets the file content provided to the server during file
394 upload. This method can be used if you do not want the content to
395 be read from an actual file.
396
397 @headers = $input->headers
398 input->headers($key => $value, .... )
399 This get/set additional header fields describing the file uploaded.
400 This can for instance be used to set the "Content-Type" reported
401 for the file.
402
404 LWP, LWP::UserAgent, HTML::Parser
405
407 Copyright 1998-2008 Gisle Aas.
408
409 This library is free software; you can redistribute it and/or modify it
410 under the same terms as Perl itself.
411
412
413
414perl v5.28.0 2012-03-30 HTML::Form(3)