HTML::Form(3)
π HTML::Form(3) β the class that's literally an HTML form element π
Okay bestie, this is the HTML::Form(3) man page from User Contributed Perl Documentation, and it's giving main character energy in the best way. We're talking about a Perl class whose whole job is to represent one HTML form element. Iconic. π
β¨ NAME β¨
HTML::Form β Class that represents an HTML form element.
π VERSION π
version 6.12
π 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 π
Okay so, objects of the HTML::Form class represent a single HTML <form> ... </form> instance. A form is a sequence of inputs that usually have names, and those inputs can take on various values. You can tweak the form's state, and then ask it for HTTP::Request objects that can be passed to the request() method of LWP::UserAgent. Obsessed, honestly.
The following methods are available:
π§ parse() β the form-finding class method
@forms = HTML::Form->parse( $html_document, $base_uri )
@forms = HTML::Form->parse( $html_document, base => $base_uri, %opt )
@forms = HTML::Form->parse( $response, %opt )
The parse() class method parses an HTML document and builds up HTML::Form objects for each <form> element found. If called in scalar context it only returns the first <form>. Returns an empty list if there are no forms to be found. Not a single form. Tragic, but fr fr.
The required arguments are 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 encodes 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, so the recommended simplest invocation becomes:
my @forms = 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 provides 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 @forms = 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 @forms = 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. Slay.
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.
π«Ά push_input() β add extra inputs to the form
$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. Lowkey handy.
Example:
push_input( 'hidden', {
name => 'NewFormElement',
id => 'NewFormElementId',
value => 'some value',
});
π method() β gets/sets the HTTP request method
$method = $form->method
$form->method( $new_method )
This method gets/sets the method name used for the HTTP::Request generated. It is a string like "GET" or "POST".
πΈ action() β gets/sets the URI
$action = $form->action
$form->action( $new_action )
This method gets/sets the URI which we want to apply the request method to.
π¦ enctype() β gets/sets the form data encoding type
$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_charset() β gets/sets the server's accepted charsets
$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.
π§ attr() β peek at the original <form> attributes
$value = $form->attr( $name )
$form->attr( $name, $new_value )
This method gives 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") eq "foo", @f;
die "No form named 'foo' found" unless @f;
$foo = shift @f;
π₯ strict() β gets/sets strict mode
$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.
π inputs() β list all the inputs
@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. See "INPUTS" for what methods are available for the input objects returned.
π find_input() β locate inputs like a detective
$input = $form->find_input( $selector )
$input = $form->find_input( $selector, $type )
$input = $form->find_input( $selector, $type, $index )
@inputs = $form->find_input( $selector )
@inputs = $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. Iykyk.
my @by_id = $form->find_input( '#some-id' );
my @by_class = $form->find_input( '.some-class' );
my @by_name = $form->find_input( '^some-name' );
my @also_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 @nameless_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 nth input with the given name and/or type.
π value() β get/set one input's value
$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;
π param() β the CGI-ish interface
@names = $form->param
@values = $form->param( $name )
$form->param( $name, $value, ... )
$form->param( $name, \@values )
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.
π try_others( \&callback ) β all the permutations
$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.
π¨ make_request() β build the HTTP::Request
$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.
π click() β simulate clicking a clickable input
$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.
β οΈ So if a$selectoris specified, we click on the first clickable input that matches the selector β and the method croaks π if no matching clickable input is found. If$selectoris not specified, then it's totally fine if the form contains no clickable inputs at all. In that case, theclick()method returns the same request thatmake_request()would do. Peek at thefind_input()description above for the whole tea on how$selectoris specified. β¨
Okay but here's the π part: if there are multiple clickable inputs with the same name, then there is literally no way to get the click() method of the "HTML::Form" to click on any but the first. Need more than that? You'd have to locate the input with find_input() and invoke the click() method on that input yourself. π
You can also pass a click coordinate pair π±οΈ, but ngl 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 that π. Therefore (1,1) was selected as a safer default.
ποΈ @kw = $form->form
Returns the current setting as a sequence of key/value pairs. Okay but heads up bestie: keys might be repeated, which means some values might get lost if you assign the return values to a hash. π
In scalar context, this method returns the number of key/value pairs generated. Slay. β¨
π $form->dump
Returns a textual representation of the current state of the form. Mainly useful for debugging. If it's called in void context, then the dump is printed on STDERR. π
πΈ NAME πΈ
HTML::Form - Class that represents an HTML form element
π INPUTS π
An "HTML::Form" object contains a sequence of inputs. You can grab references to the inputs with the $form->inputs or $form->find_input methods. π«Ά
β οΈ Important vibe check: 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 get 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: π
π $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.
π $input->id
π $input->class
These methods can be used to get/set the current id or class attribute for the input.
β
$input->selected( $selector )
Returns TRUE if the given selector matched the input. See the description of the find_input() method above for a full description of the selector syntax.
π $value = $input->value
βοΈ $input->value( $new_value )
This method can be used to get/set the current value of an input.
π¬ If strict is enabled and 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 you're running under perl -w.
πͺ $autocomplete = $input->autocomplete
βοΈ $input->autocomplete( $new_autocomplete )
This method can be used to get/set the current value (if any) of "autcomplete" for the input.
π $input->possible_values
Returns a list of all values that an input can take. For inputs that do not have discrete values, this returns an empty list.
π $input->other_possible_values
Returns a list of all values not tried yet.
π·οΈ $input->value_names
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. π
π $bool = $input->readonly
βοΈ $input->readonly( $bool )
This method is used to get/set the value of the readonly attribute. You are allowed to modify the value of read-only inputs, but setting the value will generate some noise when warnings are enabled. Hidden fields always start out read-only. π
π« $bool = $input->disabled
βοΈ $input->disabled( $bool )
This method is used to get/set the value of the disabled attribute. Disabled inputs do not contribute any key/value pairs for the form value.
π¦ $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->check
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: π
π $input->file
This is just an alias for the value() method. It sets the filename to read data from.
π₯ Security tea: for security reasons this field will never be initialized from the parsing of a form. This prevents the server from triggering stealth uploads of arbitrary files from the client machine. Periodt. π
π·οΈ $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 read 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 π
LWP, LWP::UserAgent, HTML::Parser
π AUTHOR π
Gisle Aas <gisle@activestate.com>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 1998 by Gisle Aas.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Okay that's the whole thing bestie, go build something iconic with it π¦π β this page came from perl v5.42.0, dated 2024-10-19, and the whole HTML::Form(3) manual is officially served. π
π¦ where this page comes from
- opensuse-leap 16.0: perl-HTML-Form 6.120.0-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms