Description :
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 following methods are available:
* AATTforms = HTML::Form->parse( $html_document, $base_uri )
* AATTforms = HTML::Form->parse( $html_document, base => $base_uri, %opt )
* AATTforms = HTML::Form->parse( $response, %opt )
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 required arguments is the HTML document to parse ($html_document) and the URI used to retrieve the document ($base_uri). The base URI is needed to resolve relative action URIs. The provided HTML document should be a Unicode string (or US-ASCII).
By default HTML::Form assumes that the original document was UTF-8 encoded and thus encode forms that don\'t specify an explicit _accept-charset_ as UTF-8. The charset assumed can be overridden by providing the \'charset\' option to parse(). It\'s a good idea to be explicit about this parameter as well, thus the recommended simplest invocation becomes:
my AATTforms = HTML::Form->parse( Encode::decode($encoding, $html_document_bytes), base => $base_uri, charset => $encoding, );
If the document was retrieved with LWP then the response object provide methods to obtain a proper value for \'base\' and \'charset\':
my $ua = LWP::UserAgent->new; my $response = $ua->get(\"http://www.example.com/form.html\"); my AATTforms = HTML::Form->parse($response->decoded_content, base => $response->base, charset => $response->content_charset, );
In fact, 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 AATTforms = HTML::Form->parse($response);
Note that any object that implements a decoded_content(), base() and content_charset() method with similar behaviour as HTTP::Response will do.
Additional options might be passed in to control how the parse method behaves. The following are all the options currently recognized:
* \'base => $uri\'
This is the URI used to retrieve the original document. This option is not optional ;-)
* \'charset => $str\'
Specify what charset the original document was encoded in. This is used as the default for accept_charset. If not provided this defaults to \"UTF-8\".
* \'verbose => $bool\'
Warn (print messages to STDERR) about any bad HTML form constructs found. You can trap these with $SIG{__WARN__}. The default is not to issue warnings.
* \'strict => $bool\'
Initialize any form objects with the given strict attribute. If the strict is turned on the methods that change values of the form will croak if you try to set illegal values or modify readonly fields. The default is not to be strict.
* $form->push_input( $type, \\%attr, $verbose )
This method adds additional inputs to the form. The first argument is the type of input (e.g. hidden, option, etc.). The second argument is a reference to a hash of the input attributes. The third argument is optional, and will issue warnings about unknown input types.
Example:
push_input( \'hidden\', { name => \'NewFormElement\', id => \'NewFormElementId\', value => \'some value\', });
* $method = $form->method
* $form->method( $new_method )
This method is gets/sets the _method_ name used 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\".
* $accept = $form->accept_charset
* $form->accept_charset( $new_accept )
This method gets/sets the list of charset encodings that the server processing the form accepts. Current implementation supports only one-element lists. Default value is \"UNKNOWN\" which we interpret as a request to use document charset as specified by the \'charset\' parameter of the parse() method.
* $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:
AATTf = HTML::Form->parse( $html, $foo ); AATTf = grep $_->attr(\"id\") eq \"foo\", AATTf; die \"No form named \'foo\' found\" unless AATTf; $foo = shift AATTf;
* $bool = $form->strict
* $form->strict( $bool )
Gets/sets the strict attribute of a form. If the strict is turned on the methods that change values of the form will croak if you try to set illegal values or modify readonly fields. The default is not to be strict.
* AATTinputs = $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. See INPUTS for what methods are available for the input objects returned.
* $input = $form->find_input( $selector )
* $input = $form->find_input( $selector, $type )
* $input = $form->find_input( $selector, $type, $index )
* AATTinputs = $form->find_input( $selector )
* AATTinputs = $form->find_input( $selector, $type )
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 \'undef\' if none match.
If \'$selector\' is not \'undef\', then the input\'s _name_, _id_ or _class_ attribute must match. A selector prefixed with \'#\' must match the _id_ attribute of the input. A selector prefixed with \'.\' matches the _class_ attribute. A selector prefixed with \'^\' or with no prefix matches the _name_ attribute.
my AATTby_id = $form->find_input( \'#some-id\' ); my AATTby_class = $form->find_input( \'.some-class\' ); my AATTby_name = $form->find_input( \'^some-name\' ); my AATTalso_by_name = $form->find_input( \'some-name\' );
If you want to find an input that has no _name_ at all, pass in a reference to \'undef\'.
my AATTnameless_inputs = $form->find_input( \\undef );
If \'$type\' is not \'undef\', 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 \'$selector\' and/or \'$type\', then it selects the _n_th input with the given _name_ and/or type.
* $value = $form->value( $selector )
* $form->value( $selector, $new_value )
The value() method can be used to get/set the value of some input. If strict is enabled and no input has the indicated name, then this method will croak.
If multiple inputs have the same name, only the first one will be affected.
The call:
$form->value(\'foo\')
is basically a short-hand for:
$form->find_input(\'foo\')->value;
* AATTnames = $form->param
* AATTvalues = $form->param( $name )
* $form->param( $name, $value, ... )
* $form->param( $name, \\AATTvalues )
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.
* $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( $selector )
* $request = $form->click( $x, $y )
* $request = $form->click( $selector, $x, $y )
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::UserAgent if you want to obtain the server response.
If a $selector is specified, we will click on the first clickable input matching the selector, and the method will croak if no matching clickable input is found. If $selector 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. See description of the find_input() method above for how the $selector is specified.
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.
* AATTkw = $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.
|