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
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 explict accept-
43           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           explict about this parameter as well, thus the recommended simplest
46           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__}.
92
93           "strict => $bool"
94               Initialize any form objects with the given strict attribute.
95
96       $method = $form->method
97       $form->method( $new_method )
98           This method is gets/sets the method name used for the
99           "HTTP::Request" generated.  It is a string like "GET" or "POST".
100
101       $action = $form->action
102       $form->action( $new_action )
103           This method gets/sets the URI which we want to apply the request
104           method to.
105
106       $enctype = $form->enctype
107       $form->enctype( $new_enctype )
108           This method gets/sets the encoding type for the form data.  It is a
109           string like "application/x-www-form-urlencoded" or
110           "multipart/form-data".
111
112       $accept = $form->accept_charset
113       $form->accept_charset( $new_accept )
114           This method gets/sets the list of charset encodings that the server
115           processing the form accepts. Current implementation supports only
116           one-element lists.  Default value is "UNKNOWN" which we interpret
117           as a request to use document charset as specified by the 'charset'
118           parameter of the parse() method. To encode character strings you
119           should have modern perl with Encode module. On older perls the
120           setting of this attribute has no effect.
121
122       $value = $form->attr( $name )
123       $form->attr( $name, $new_value )
124           This method give access to the original HTML attributes of the
125           <form> tag.  The $name should always be passed in lower case.
126
127           Example:
128
129              @f = HTML::Form->parse( $html, $foo );
130              @f = grep $_->attr("id") eq "foo", @f;
131              die "No form named 'foo' found" unless @f;
132              $foo = shift @f;
133
134       $bool = $form->strict
135       $form->strict( $bool )
136           Gets/sets the strict attribute of a form.  If the strict is turned
137           on the methods that change values of the form will croak if you try
138           to set illegal values or modify readonly fields.  The default is
139           not to be strict.
140
141       @inputs = $form->inputs
142           This method returns the list of inputs in the form.  If called in
143           scalar context it returns the number of inputs contained in the
144           form.  See "INPUTS" for what methods are available for the input
145           objects returned.
146
147       $input = $form->find_input( $selector )
148       $input = $form->find_input( $selector, $type )
149       $input = $form->find_input( $selector, $type, $index )
150           This method is used to locate specific inputs within the form.  All
151           inputs that match the arguments given are returned.  In scalar
152           context only the first is returned, or "undef" if none match.
153
154           If $selector is specified, then the input's name, id, class
155           attribute must match.  A selector prefixed with '#' must match the
156           id attribute of the input.  A selector prefixed with '.' matches
157           the class attribute.  A selector prefixed with '^' or with no
158           prefix matches the name attribute.
159
160           If $type is specified, then the input must have the specified type.
161           The following type names are used: "text", "password", "hidden",
162           "textarea", "file", "image", "submit", "radio", "checkbox" and
163           "option".
164
165           The $index is the sequence number of the input matched where 1 is
166           the first.  If combined with $name and/or $type then it select the
167           nth input with the given name and/or type.
168
169       $value = $form->value( $selector )
170       $form->value( $selector, $new_value )
171           The value() method can be used to get/set the value of some input.
172           If strict is enabled and no input has the indicated name, then this
173           method will croak.
174
175           If multiple inputs have the same name, only the first one will be
176           affected.
177
178           The call:
179
180               $form->value('foo')
181
182           is basically a short-hand for:
183
184               $form->find_input('foo')->value;
185
186       @names = $form->param
187       @values = $form->param( $name )
188       $form->param( $name, $value, ... )
189       $form->param( $name, \@values )
190           Alternative interface to examining and setting the values of the
191           form.
192
193           If called without arguments then it returns the names of all the
194           inputs in the form.  The names will not repeat even if multiple
195           inputs have the same name.  In scalar context the number of
196           different names is returned.
197
198           If called with a single argument then it returns the value or
199           values of inputs with the given name.  If called in scalar context
200           only the first value is returned.  If no input exists with the
201           given name, then "undef" is returned.
202
203           If called with 2 or more arguments then it will set values of the
204           named inputs.  This form will croak if no inputs have the given
205           name or if any of the values provided does not fit.  Values can
206           also be provided as a reference to an array.  This form will allow
207           unsetting all values with the given name as well.
208
209           This interface resembles that of the param() function of the CGI
210           module.
211
212       $form->try_others( \&callback )
213           This method will iterate over all permutations of unvisited
214           enumerated values (<select>, <radio>, <checkbox>) and invoke the
215           callback for each.  The callback is passed the $form as argument.
216           The return value from the callback is ignored and the try_others()
217           method itself does not return anything.
218
219       $request = $form->make_request
220           Will return an "HTTP::Request" object that reflects the current
221           setting of the form.  You might want to use the click() method
222           instead.
223
224       $request = $form->click
225       $request = $form->click( $selector )
226       $request = $form->click( $x, $y )
227       $request = $form->click( $selector, $x, $y )
228           Will "click" on the first clickable input (which will be of type
229           "submit" or "image").  The result of clicking is an "HTTP::Request"
230           object that can then be passed to "LWP::UserAgent" if you want to
231           obtain the server response.
232
233           If a $selector is specified, we will click on the first clickable
234           input matching the selector, and the method will croak if no
235           matching clickable input is found.  If $selector is not specified,
236           then it is ok if the form contains no clickable inputs.  In this
237           case the click() method returns the same request as the
238           make_request() method would do.  See description of the
239           find_input() method above for how the $selector is specified.
240
241           If there are multiple clickable inputs with the same name, then
242           there is no way to get the click() method of the "HTML::Form" to
243           click on any but the first.  If you need this you would have to
244           locate the input with find_input() and invoke the click() method on
245           the given input yourself.
246
247           A click coordinate pair can also be provided, but this only makes a
248           difference if you clicked on an image.  The default coordinate is
249           (1,1).  The upper-left corner of the image is (0,0), but some badly
250           coded CGI scripts are known to not recognize this.  Therefore (1,1)
251           was selected as a safer default.
252
253       @kw = $form->form
254           Returns the current setting as a sequence of key/value pairs.  Note
255           that keys might be repeated, which means that some values might be
256           lost if the return values are assigned to a hash.
257
258           In scalar context this method returns the number of key/value pairs
259           generated.
260
261       $form->dump
262           Returns a textual representation of current state of the form.
263           Mainly useful for debugging.  If called in void context, then the
264           dump is printed on STDERR.
265

INPUTS

267       An "HTML::Form" objects contains a sequence of inputs.  References to
268       the inputs can be obtained with the $form->inputs or $form->find_input
269       methods.
270
271       Note that there is not a one-to-one correspondence between input
272       objects and <input> elements in the HTML document.  An input object
273       basically represents a name/value pair, so when multiple HTML elements
274       contribute to the same name/value pair in the submitted form they are
275       combined.
276
277       The input elements that are mapped one-to-one are "text", "textarea",
278       "password", "hidden", "file", "image", "submit" and "checkbox".  For
279       the "radio" and "option" inputs the story is not as simple: All <input
280       type="radio"> elements with the same name will contribute to the same
281       input radio object.  The number of radio input objects will be the same
282       as the number of distinct names used for the <input type="radio">
283       elements.  For a <select> element without the "multiple" attribute
284       there will be one input object of type of "option".  For a <select
285       multiple> element there will be one input object for each contained
286       <option> element.  Each one of these option objects will have the same
287       name.
288
289       The following methods are available for the input objects:
290
291       $input->type
292           Returns the type of this input.  The type is one of the following
293           strings: "text", "password", "hidden", "textarea", "file", "image",
294           "submit", "radio", "checkbox" or "option".
295
296       $name = $input->name
297       $input->name( $new_name )
298           This method can be used to get/set the current name of the input.
299
300       $input->id
301       $input->class
302           These methods can be used to get/set the current id or class
303           attribute for the input.
304
305       $input->selected( $selector )
306           Returns TRUE if the given selector matched the input.  See the
307           description of the find_input() method above for a description of
308           the selector syntax.
309
310       $value = $input->value
311       $input->value( $new_value )
312           This method can be used to get/set the current value of an input.
313
314           If strict is enabled and the input only can take an enumerated list
315           of values, then it is an error to try to set it to something else
316           and the method will croak if you try.
317
318           You will also be able to set the value of read-only inputs, but a
319           warning will be generated if running under "perl -w".
320
321       $input->possible_values
322           Returns a list of all values that an input can take.  For inputs
323           that do not have discrete values, this returns an empty list.
324
325       $input->other_possible_values
326           Returns a list of all values not tried yet.
327
328       $input->value_names
329           For some inputs the values can have names that are different from
330           the values themselves.  The number of names returned by this method
331           will match the number of values reported by
332           $input->possible_values.
333
334           When setting values using the value() method it is also possible to
335           use the value names in place of the value itself.
336
337       $bool = $input->readonly
338       $input->readonly( $bool )
339           This method is used to get/set the value of the readonly attribute.
340           You are allowed to modify the value of readonly inputs, but setting
341           the value will generate some noise when warnings are enabled.
342           Hidden fields always start out readonly.
343
344       $bool = $input->disabled
345       $input->disabled( $bool )
346           This method is used to get/set the value of the disabled attribute.
347           Disabled inputs do not contribute any key/value pairs for the form
348           value.
349
350       $input->form_name_value
351           Returns a (possible empty) list of key/value pairs that should be
352           incorporated in the form value from this input.
353
354       $input->check
355           Some input types represent toggles that can be turned on/off.  This
356           includes "checkbox" and "option" inputs.  Calling this method turns
357           this input on without having to know the value name.  If the input
358           is already on, then nothing happens.
359
360           This has the same effect as:
361
362               $input->value($input->possible_values[1]);
363
364           The input can be turned off with:
365
366               $input->value(undef);
367
368       $input->click($form, $x, $y)
369           Some input types (currently "submit" buttons and "images") can be
370           clicked to submit the form.  The click() method returns the
371           corresponding "HTTP::Request" object.
372
373       If the input is of type "file", then it has these additional methods:
374
375       $input->file
376           This is just an alias for the value() method.  It sets the filename
377           to read data from.
378
379           For security reasons this field will never be initialized from the
380           parsing of a form.  This prevents the server from triggering
381           stealth uploads of arbitrary files from the client machine.
382
383       $filename = $input->filename
384       $input->filename( $new_filename )
385           This get/sets the filename reported to the server during file
386           upload.  This attribute defaults to the value reported by the
387           file() method.
388
389       $content = $input->content
390       $input->content( $new_content )
391           This get/sets the file content provided to the server during file
392           upload.  This method can be used if you do not want the content to
393           be read from an actual file.
394
395       @headers = $input->headers
396       input->headers($key => $value, .... )
397           This get/set additional header fields describing the file uploaded.
398           This can for instance be used to set the "Content-Type" reported
399           for the file.
400

SEE ALSO

402       LWP, LWP::UserAgent, HTML::Parser
403
405       Copyright 1998-2008 Gisle Aas.
406
407       This library is free software; you can redistribute it and/or modify it
408       under the same terms as Perl itself.
409
410
411
412perl v5.12.4                      2009-07-07                     HTML::Form(3)
Impressum