1HTML::Form(3)         User Contributed Perl Documentation        HTML::Form(3)
2
3
4

NAME

6       HTML::Form - Class that represents an HTML form element
7

SYNOPSIS

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

DESCRIPTION

18       Objects of the "HTML::Form" class represents a single HTML "<form> ...
19       </form>" instance.  A form consists of a sequence of inputs that usu‐
20       ally have names, and which can take on various values.  The state of a
21       form can be tweaked and it can then be asked to provide "HTTP::Request"
22       objects that can be passed to the request() method of "LWP::UserAgent".
23
24       The following methods are available:
25
26       @forms = HTML::Form->parse( $response )
27       @forms = HTML::Form->parse( $html_document, $base )
28       @forms = HTML::Form->parse( $html_document, %opt )
29           The parse() class method will parse an HTML document and build up
30           "HTML::Form" objects for each <form> element found.  If called in
31           scalar context only returns the first <form>.  Returns an empty
32           list if there are no forms to be found.
33
34           The $base is the URI used to retrieve the $html_document.  It is
35           needed to resolve relative action URIs.  If the document was
36           retrieved with LWP then this this parameter is obtained from the
37           $response->base() method, as shown by the following example:
38
39               my $ua = LWP::UserAgent->new;
40               my $response = $ua->get("http://www.example.com/form.html");
41               my @forms = HTML::Form->parse($response->decoded_content,
42                                             $response->base);
43
44           The parse() method can parse from an "HTTP::Response" object
45           directly, so the example above can be more conveniently written as:
46
47               my $ua = LWP::UserAgent->new;
48               my $response = $ua->get("http://www.example.com/form.html");
49               my @forms = HTML::Form->parse($response);
50
51           Note that any object that implements a decoded_content() and base()
52           method with similar behaviour as "HTTP::Response" will do.
53
54           Finally options might be passed in to control how the parse method
55           behaves.  The following options are currently recognized:
56
57           "base"
58               Another way to provide the base URI.
59
60           "verbose"
61               Print messages to STDERR about any bad HTML form constructs
62               found.
63
64       $method = $form->method
65       $form->method( $new_method )
66           This method is gets/sets the method name used for the
67           "HTTP::Request" generated.  It is a string like "GET" or "POST".
68
69       $action = $form->action
70       $form->action( $new_action )
71           This method gets/sets the URI which we want to apply the request
72           method to.
73
74       $enctype = $form->enctype
75       $form->enctype( $new_enctype )
76           This method gets/sets the encoding type for the form data.  It is a
77           string like "application/x-www-form-urlencoded" or "multi‐
78           part/form-data".
79
80       $value = $form->attr( $name )
81       $form->attr( $name, $new_value )
82           This method give access to the original HTML attributes of the
83           <form> tag.  The $name should always be passed in lower case.
84
85           Example:
86
87              @f = HTML::Form->parse( $html, $foo );
88              @f = grep $_->attr("id") eq "foo", @f;
89              die "No form named 'foo' found" unless @f;
90              $foo = shift @f;
91
92       @inputs = $form->inputs
93           This method returns the list of inputs in the form.  If called in
94           scalar context it returns the number of inputs contained in the
95           form.  See "INPUTS" for what methods are available for the input
96           objects returned.
97
98       $input = $form->find_input( $name )
99       $input = $form->find_input( $name, $type )
100       $input = $form->find_input( $name, $type, $index )
101           This method is used to locate specific inputs within the form.  All
102           inputs that match the arguments given are returned.  In scalar con‐
103           text only the first is returned, or "undef" if none match.
104
105           If $name is specified, then the input must have the indicated name.
106
107           If $type is specified, then the input must have the specified type.
108           The following type names are used: "text", "password", "hidden",
109           "textarea", "file", "image", "submit", "radio", "checkbox" and
110           "option".
111
112           The $index is the sequence number of the input matched where 1 is
113           the first.  If combined with $name and/or $type then it select the
114           nth input with the given name and/or type.
115
116       $value = $form->value( $name )
117       $form->value( $name, $new_value )
118           The value() method can be used to get/set the value of some input.
119           If no input has the indicated name, then this method will croak.
120
121           If multiple inputs have the same name, only the first one will be
122           affected.
123
124           The call:
125
126               $form->value('foo')
127
128           is a short-hand for:
129
130               $form->find_input('foo')->value;
131
132       @names = $form->param
133       @values = $form->param( $name )
134       $form->param( $name, $value, ... )
135       $form->param( $name, \@values )
136           Alternative interface to examining and setting the values of the
137           form.
138
139           If called without arguments then it returns the names of all the
140           inputs in the form.  The names will not repeat even if multiple
141           inputs have the same name.  In scalar context the number of differ‐
142           ent names is returned.
143
144           If called with a single argument then it returns the value or val‐
145           ues of inputs with the given name.  If called in scalar context
146           only the first value is returned.  If no input exists with the
147           given name, then "undef" is returned.
148
149           If called with 2 or more arguments then it will set values of the
150           named inputs.  This form will croak if no inputs have the given
151           name or if any of the values provided does not fit.  Values can
152           also be provided as a reference to an array.  This form will allow
153           unsetting all values with the given name as well.
154
155           This interface resembles that of the param() function of the CGI
156           module.
157
158       $form->try_others( \&callback )
159           This method will iterate over all permutations of unvisited enumer‐
160           ated values (<select>, <radio>, <checkbox>) and invoke the callback
161           for each.  The callback is passed the $form as argument.  The
162           return value from the callback is ignored and the try_others()
163           method itself does not return anything.
164
165       $request = $form->make_request
166           Will return an "HTTP::Request" object that reflects the current
167           setting of the form.  You might want to use the click() method
168           instead.
169
170       $request = $form->click
171       $request = $form->click( $name )
172       $request = $form->click( $x, $y )
173       $request = $form->click( $name, $x, $y )
174           Will "click" on the first clickable input (which will be of type
175           "submit" or "image").  The result of clicking is an "HTTP::Request"
176           object that can then be passed to "LWP::UserAgent" if you want to
177           obtain the server response.
178
179           If a $name is specified, we will click on the first clickable input
180           with the given name, and the method will croak if no clickable
181           input with the given name is found.  If $name is not specified,
182           then it is ok if the form contains no clickable inputs.  In this
183           case the click() method returns the same request as the
184           make_request() method would do.
185
186           If there are multiple clickable inputs with the same name, then
187           there is no way to get the click() method of the "HTML::Form" to
188           click on any but the first.  If you need this you would have to
189           locate the input with find_input() and invoke the click() method on
190           the given input yourself.
191
192           A click coordinate pair can also be provided, but this only makes a
193           difference if you clicked on an image.  The default coordinate is
194           (1,1).  The upper-left corner of the image is (0,0), but some badly
195           coded CGI scripts are known to not recognize this.  Therefore (1,1)
196           was selected as a safer default.
197
198       @kw = $form->form
199           Returns the current setting as a sequence of key/value pairs.  Note
200           that keys might be repeated, which means that some values might be
201           lost if the return values are assigned to a hash.
202
203           In scalar context this method returns the number of key/value pairs
204           generated.
205
206       $form->dump
207           Returns a textual representation of current state of the form.
208           Mainly useful for debugging.  If called in void context, then the
209           dump is printed on STDERR.
210

INPUTS

212       An "HTML::Form" objects contains a sequence of inputs.  References to
213       the inputs can be obtained with the $form->inputs or $form->find_input
214       methods.
215
216       Note that there is not a one-to-one correspondence between input
217       objects and <input> elements in the HTML document.  An input object
218       basically represents a name/value pair, so when multiple HTML elements
219       contribute to the same name/value pair in the submitted form they are
220       combined.
221
222       The input elements that are mapped one-to-one are "text", "textarea",
223       "password", "hidden", "file", "image", "submit" and "checkbox".  For
224       the "radio" and "option" inputs the story is not as simple: All <input
225       type="radio"> elements with the same name will contribute to the same
226       input radio object.  The number of radio input objects will be the same
227       as the number of distinct names used for the <input type="radio"> ele‐
228       ments.  For a <select> element without the "multiple" attribute there
229       will be one input object of type of "option".  For a <select multiple>
230       element there will be one input object for each contained <option> ele‐
231       ment.  Each one of these option objects will have the same name.
232
233       The following methods are available for the input objects:
234
235       $input->type
236           Returns the type of this input.  The type is one of the following
237           strings: "text", "password", "hidden", "textarea", "file", "image",
238           "submit", "radio", "checkbox" or "option".
239
240       $name = $input->name
241       $input->name( $new_name )
242           This method can be used to get/set the current name of the input.
243
244       $value = $input->value
245       $input->value( $new_value )
246           This method can be used to get/set the current value of an input.
247
248           If the input only can take an enumerated list of values, then it is
249           an error to try to set it to something else and the method will
250           croak if you try.
251
252           You will also be able to set the value of read-only inputs, but a
253           warning will be generated if running under "perl -w".
254
255       $input->possible_values
256           Returns a list of all values that an input can take.  For inputs
257           that do not have discrete values, this returns an empty list.
258
259       $input->other_possible_values
260           Returns a list of all values not tried yet.
261
262       $input->value_names
263           For some inputs the values can have names that are different from
264           the values themselves.  The number of names returned by this method
265           will match the number of values reported by $input->possible_val‐
266           ues.
267
268           When setting values using the value() method it is also possible to
269           use the value names in place of the value itself.
270
271       $bool = $input->readonly
272       $input->readonly( $bool )
273           This method is used to get/set the value of the readonly attribute.
274           You are allowed to modify the value of readonly inputs, but setting
275           the value will generate some noise when warnings are enabled.  Hid‐
276           den fields always start out readonly.
277
278       $bool = $input->disabled
279       $input->disabled( $bool )
280           This method is used to get/set the value of the disabled attribute.
281           Disabled inputs do not contribute any key/value pairs for the form
282           value.
283
284       $input->form_name_value
285           Returns a (possible empty) list of key/value pairs that should be
286           incorporated in the form value from this input.
287
288       $input->check
289           Some input types represent toggles that can be turned on/off.  This
290           includes "checkbox" and "option" inputs.  Calling this method turns
291           this input on without having to know the value name.  If the input
292           is already on, then nothing happens.
293
294           This has the same effect as:
295
296               $input->value($input->possible_values[1]);
297
298           The input can be turned off with:
299
300               $input->value(undef);
301
302       $input->click($form, $x, $y)
303           Some input types (currently "submit" buttons and "images") can be
304           clicked to submit the form.  The click() method returns the corre‐
305           sponding "HTTP::Request" object.
306
307       If the input is of type "file", then it has these additional methods:
308
309       $input->file
310           This is just an alias for the value() method.  It sets the filename
311           to read data from.
312
313       $filename = $input->filename
314       $input->filename( $new_filename )
315           This get/sets the filename reported to the server during file
316           upload.  This attribute defaults to the value reported by the
317           file() method.
318
319       $content = $input->content
320       $input->content( $new_content )
321           This get/sets the file content provided to the server during file
322           upload.  This method can be used if you do not want the content to
323           be read from an actual file.
324
325       @headers = $input->headers
326       input->headers($key => $value, .... )
327           This get/set additional header fields describing the file uploaded.
328           This can for instance be used to set the "Content-Type" reported
329           for the file.
330

SEE ALSO

332       LWP, LWP::UserAgent, HTML::Parser
333
335       Copyright 1998-2005 Gisle Aas.
336
337       This library is free software; you can redistribute it and/or modify it
338       under the same terms as Perl itself.
339
340
341
342perl v5.8.8                       2004-04-06                     HTML::Form(3)
Impressum