HTML::Form - Class that represents an HTML form element
use HTML::Form; $form = HTML::Form->parse($html, $base_uri); $form->value(query => "Perl");
use LWP::UserAgent; $ua = LWP::UserAgent->new; $response = $ua->request($form->click);
The following methods are available:
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:
my $ua = LWP::UserAgent->new;
my $response = $ua->get("http://www.example.com/form.html");
my @forms = HTML::Form->parse($response);
Note that any object that implements a decoded_content() and base() method with similar behaviour as "HTTP::Response" will do.
Finally options might be passed in to control how the parse method behaves. The following options are currently recognized:
Example:
@f = HTML::Form->parse( $html, $foo );
@f = grep $_->attr("id") eq "foo", @f;
die "No form named 'foo' found" unless @f;
$foo = shift @f;
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.
If multiple inputs have the same name, only the first one will be affected.
The call:
$form->value('foo')
is a short-hand for:
$form->find_input('foo')->value;
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"
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.
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.
In scalar context this method returns the number of key/value pairs generated.
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:
If the input only can take an enumerated list of values, then it is an error to try to set it to something else and the method will croak if you try.
You will also be able to set the value of read-only inputs, but a warning will be generated if running under "perl -w".
When setting values using the value() method it is also possible to use the value names in place of the value itself.
This has the same effect as:
$input->value($input->possible_values[1]);
The input can be turned off with:
$input->value(undef);
If the input is of type "file", then it has these additional methods:
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.