Objects of the HTML::Form class represents a single HTML
<form> ... </form> instance. A form consists of a
sequence of inputs that usually have names, and which can take on
various values. The state of a form can be tweaked and it can then be
asked to provide HTTP::Request objects that can be passed to the
request() method of LWP::UserAgent.
The parse() class method will parse an HTML document and build up
HTML::Form objects for each <form> element found. If called in scalar
context only returns the first <form>. Returns an empty list if there
are no forms to be found.
The $base is the URI used to retrieve the $html_document. It is
needed to resolve relative action URIs. If the document was retrieved
with LWP then this this parameter is obtained from the
$response->base() method, as shown by the following example:
my $ua = LWP::UserAgent->new;
my $response = $ua->get("http://www.example.com/form.html");
my @forms = HTML::Form->parse($response->decoded_content,
$response->base);
The parse() method can parse from an HTTP::Response object
directly, so the example above can be more conveniently written as:
This method returns the list of inputs in the form. If called in
scalar context it returns the number of inputs contained in the form.
See INPUTS for what methods are available for the input objects
returned.
This method is used to locate specific inputs within the form. All
inputs that match the arguments given are returned. In scalar context
only the first is returned, or undefif none match.
If $name is specified, then the input must have the indicated name.
If $type is specified, then the input must have the specified type.
The following type names are used: ``text'', ``password'', ``hidden'',
``textarea'', ``file'', ``image'', ``submit'', ``radio'', ``checkbox'' and ``option''.
The $index is the sequence number of the input matched where 1 is the
first. If combined with $name and/or $type then it select the nth
input with the given name and/or type.
Alternative interface to examining and setting the values of the form.
If called without arguments then it returns the names of all the
inputs in the form. The names will not repeat even if multiple inputs
have the same name. In scalar context the number of different names
is returned.
If called with a single argument then it returns the value or values
of inputs with the given name. If called in scalar context only the
first value is returned. If no input exists with the given name, then
undef is returned.
If called with 2 or more arguments then it will set values of the
named inputs. This form will croak if no inputs have the given name
or if any of the values provided does not fit. Values can also be
provided as a reference to an array. This form will allow unsetting
all values with the given name as well.
This interface resembles that of the param() function of the CGI module.
This method will iterate over all permutations of unvisited enumerated
values (<select>, <radio>, <checkbox>) and invoke the callback for
each. The callback is passed the $form as argument. The return value
from the callback is ignored and the try_others() method itself does
not return anything.
Will ``click'' on the first clickable input (which will be of type
submit or image). The result of clicking is an HTTP::Request
object that can then be passed to LWP::UserAgentif you want to
obtain the server response.
If a $name is specified, we will click on the first clickable input
with the given name, and the method will croak if no clickable input
with the given name is found. If $name is not specified, then it
is ok if the form contains no clickable inputs. In this case the
click() method returns the same request as the make_request() method
would do.
If there are multiple clickable inputs with the same name, then there
is no way to get the click() method of the HTML::Form to click on
any but the first. If you need this you would have to locate the
input with find_input() and invoke the click() method on the given
input yourself.
A click coordinate pair can also be provided, but this only makes a
difference if you clicked on an image. The default coordinate is
(1,1). The upper-left corner of the image is (0,0), but some badly
coded CGI scripts are known to not recognize this. Therefore (1,1) was
selected as a safer default.
Returns the current setting as a sequence of key/value pairs. Note
that keys might be repeated, which means that some values might be
lost if the return values are assigned to a hash.
In scalar context this method returns the number of key/value pairs
generated.
Returns a textual representation of current state of the form. Mainly
useful for debugging. If called in void context, then the dump is
printed on STDERR.
An HTML::Form objects contains a sequence of inputs. References to
the inputs can be obtained with the $form->inputs or $form->find_input
methods.
Note that there is not a one-to-one correspondence between input
objects and <input> elements in the HTML document. An
input object basically represents a name/value pair, so when multiple
HTML elements contribute to the same name/value pair in the submitted
form they are combined.
The input elements that are mapped one-to-one are ``text'', ``textarea'',
``password'', ``hidden'', ``file'', ``image'', ``submit'' and ``checkbox''. For
the ``radio'' and ``option'' inputs the story is not as simple: All
<input type=``radio''> elements with the same name will
contribute to the same input radio object. The number of radio input
objects will be the same as the number of distinct names used for the
<input type=``radio''> elements. For a <select> element
without the multiple attribute there will be one input object of
type of ``option''. For a <select multiple> element there will
be one input object for each contained <option> element. Each
one of these option objects will have the same name.
The following methods are available for the input objects:
Returns the type of this input. The type is one of the following
strings: ``text'', ``password'', ``hidden'', ``textarea'', ``file'', ``image'', ``submit'',
``radio'', ``checkbox'' or ``option''.
For some inputs the values can have names that are different from the
values themselves. The number of names returned by this method will
match the number of values reported by $input->possible_values.
When setting values using the value() method it is also possible to
use the value names in place of the value itself.
This method is used to get/set the value of the readonly attribute.
You are allowed to modify the value of readonly inputs, but setting
the value will generate some noise when warnings are enabled. Hidden fields always start out readonly.
Some input types represent toggles that can be turned on/off. This
includes ``checkbox'' and ``option'' inputs. Calling this method turns
this input on without having to know the value name. If the input is
already on, then nothing happens.
This has the same effect as:
$input->value($input->possible_values[1]);
The input can be turned off with:
$input->value(undef);
$input->click($form, $x, $y)
Some input types (currently ``submit'' buttons and ``images'') can be
clicked to submit the form. The click() method returns the
corresponding HTTP::Request object.
If the input is of type file, then it has these additional methods:
This get/sets the file content provided to the server during file
upload. This method can be used if you do not want the content to be
read from an actual file.