| HTML::Form - Class that represents an HTML form element |
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);
Objects of the HTML::Form class represents a single HTML
<form> ... </form> instance. A form consist 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 LWP.
The following constructor methods are available:
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_uri is (usually) the URI used to retrieve the $html_document.
It is needed to resolve relative action URIs. For LWP this parameter
is obtained from the $response->base() method.
The $method defaults to ``GET'' if not provided. The $enctype defaults to ``application/x-www-form-urlencoded'' if not provided.
You will normally use HTML::Form->parse() to create new HTML::Form
objects.
The following instance methods are available on HTML::Form objects:
HTTP::Request generated. It is a string like ``GET'' or ``POST''.
Example:
@f = HTML::Form->parse( $html, $foo );
@f = grep $_->attr("id") == "foo", @f;
die "No form named 'foo' found" unless @f;
$foo = shift @f;
undef is returned.
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.
value() method can be used to get/set the value of some input. If
no input have the indicated name, then this method will croak.
If multiple inputs has the same name, only the first one will be affected.
The call:
$form->value('foo')
is a short-hand for:
$form->find_value('foo')->value;
try_others() method itself does
not return anything.
HTTP::Request object that reflects the current setting
of the form. You might want to use the click() method instead.
submit or image). The result of clicking is an HTTP::Request
object that can then be passed to LWP::UserAgent if 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 is 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 so (1,1) was selectes as a safer default.
In scalar context this method returns the number of key/value pairs generated.
An HTML::Form contains a sequence of inputs. References to the
inputs can be obtained with the $form->inputs or $form->find_input
methods. Once you have such a reference, then one of the following
methods can be used on it:
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. A croak will also be triggered if you try to set the value of a read-only input.
click() method returns the
corrsponding HTTP::Request object.
If the input is of type file, then it has these additional methods:
value() method. It sets the filename to
read data from.
file() method.
Content-Type reported for
the file.
the LWP manpage, the HTML::Parser manpage
Copyright 1998-2002 Gisle Aas.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| HTML::Form - Class that represents an HTML form element |