NAME
HTML::Form - Class that represents an HTML form element
SYNOPSIS
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);
DESCRIPTION
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:
- @forms = HTML::Form->parse( $html_document, $base_uri )
-
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_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.
- $form = HTML::Form->new( $method, $action_uri, $enctype )
-
This constructs a new empty HTML::Form object. The arguments are the initial value for which method the form should use to invoke a request, which URI to apply the method to, and what encoding type to use for the form data.
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:
- $method = $form->method
- $form->method( $new_method )
-
This method is gets/sets the method used to for the
HTTP::Request
generated. It is a string like "GET" or "POST". - $action = $form->action
- $form->action( $new_action )
-
This method gets/sets the URI which we want to apply the request method to.
- $enctype = $form->enctype
- $form->enctype( $new_enctype )
-
This method gets/sets the encoding type for the form data. It is a string like "application/x-www-form-urlencoded" or "multipart/form-data".
- $value = $form->attr( $name )
- $form->attr( $name, $new_value )
-
This method give access to the original HTML attributes of the <form> tag. The $name should always be passed in lower case.
Example:
@f = HTML::Form->parse( $html, $foo ); @f = grep $_->attr("id") == "foo", @f; die "No form named 'foo' found" unless @f; $foo = shift @f;
- @inputs = $form->inputs
-
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.
- $input = $form->find_input($name, $type, $index)
-
This method is used to locate some specific input within the form. At least one of the arguments must be defined. If no matching input is found,
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 = $form->value( $name )
- $form->value( $name, $new_value )
-
The 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;
- $form->try_others( \&callback )
-
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.
- $request = $form->make_request
-
Will return an
HTTP::Request
object that reflects the current setting of the form. You might want to use the click() method instead. - $request = $form->click
- $request = $form->click( $name )
- $request = $form->click( $x, $y )
- $request = $form->click( $name, $x, $y )
-
Will "click" on the first clickable input (which will be of type
submit
orimage
). The result of clicking is anHTTP::Request
object that can then be passed toLWP::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.
- @kw = $form->form
-
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.
- $form->dump
-
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.
INPUTS
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:
- $input->type
-
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".
- $name = $input->name
- $input->name( $new_name )
-
This method can be used to get/set the current name of the input.
- $value = $input->value
- $input->value( $new_value )
-
This method can be used to get/set the current value of an input.
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.
- $input->possible_values
-
Returns a list of all values that and input can take. For inputs that does not have discrete values this returns an empty list.
- $input->other_possible_values
-
Returns a list of all values not tried yet.
- $input->form_name_value
-
Returns a (possible empty) list of key/value pairs that should be incorporated in the form value from this input.
- $input->click($form, $x, $y)
-
Some input types (currently "sumbit" buttons and "images") can be clicked to submit the form. The click() method returns the corrsponding
HTTP::Request
object.
If the input is of type file
, then it has these additional methods:
- $input->file
-
This is just an alias for the value() method. It sets the filename to read data from.
- $filename = $input->filename
- $input->filename( $new_filename )
-
This get/sets the filename reported to the server during file upload. This attribute defaults to the value reported by the file() method.
- $content = $input->content
- $input->content( $new_content )
-
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 uploaded to be provided from an actual file.
- @headers = $input->headers
- input->headers($key => $value, .... )
-
This get/set additional header fields describing the file uploaded. This can for instance be used to set the
Content-Type
reported for the file.
SEE ALSO
COPYRIGHT
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.