.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "HTML::Form 3" .TH HTML::Form 3 "2023-02-11" "perl v5.16.3" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" HTML::Form \- Class that represents an HTML form element .SH "VERSION" .IX Header "VERSION" version 6.11 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 3 \& 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); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Objects of the \f(CW\*(C`HTML::Form\*(C'\fR class represents a single \s-1HTML \&\s0\f(CW\*(C`
...
\*(C'\fR 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 \&\fIrequest()\fR method of LWP::UserAgent. .PP The following methods are available: .ie n .IP "@forms = HTML::Form\->parse( $html_document, $base_uri )" 4 .el .IP "\f(CW@forms\fR = HTML::Form\->parse( \f(CW$html_document\fR, \f(CW$base_uri\fR )" 4 .IX Item "@forms = HTML::Form->parse( $html_document, $base_uri )" .PD 0 .ie n .IP "@forms = HTML::Form\->parse( $html_document, base => $base_uri, %opt )" 4 .el .IP "\f(CW@forms\fR = HTML::Form\->parse( \f(CW$html_document\fR, base => \f(CW$base_uri\fR, \f(CW%opt\fR )" 4 .IX Item "@forms = HTML::Form->parse( $html_document, base => $base_uri, %opt )" .ie n .IP "@forms = HTML::Form\->parse( $response, %opt )" 4 .el .IP "\f(CW@forms\fR = HTML::Form\->parse( \f(CW$response\fR, \f(CW%opt\fR )" 4 .IX Item "@forms = HTML::Form->parse( $response, %opt )" .PD The \fIparse()\fR class method will parse an \s-1HTML\s0 document and build up \&\f(CW\*(C`HTML::Form\*(C'\fR objects for each
element found. If called in scalar context only returns the first . Returns an empty list if there are no forms to be found. .Sp The required arguments is the \s-1HTML\s0 document to parse ($html_document) and the \&\s-1URI\s0 used to retrieve the document ($base_uri). The base \s-1URI\s0 is needed to resolve relative action URIs. The provided \s-1HTML\s0 document should be a Unicode string (or US-ASCII). .Sp By default HTML::Form assumes that the original document was \s-1UTF\-8\s0 encoded and thus encode forms that don't specify an explicit \fIaccept-charset\fR as \s-1UTF\-8.\s0 The charset assumed can be overridden by providing the \f(CW\*(C`charset\*(C'\fR option to \&\fIparse()\fR. It's a good idea to be explicit about this parameter as well, thus the recommended simplest invocation becomes: .Sp .Vb 5 \& my @forms = HTML::Form\->parse( \& Encode::decode($encoding, $html_document_bytes), \& base => $base_uri, \& charset => $encoding, \& ); .Ve .Sp If the document was retrieved with \s-1LWP\s0 then the response object provide methods to obtain a proper value for \f(CW\*(C`base\*(C'\fR and \f(CW\*(C`charset\*(C'\fR: .Sp .Vb 6 \& 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, \& ); .Ve .Sp In fact, the \fIparse()\fR method can parse from an HTTP::Response object directly, so the example above can be more conveniently written as: .Sp .Vb 3 \& my $ua = LWP::UserAgent\->new; \& my $response = $ua\->get("http://www.example.com/form.html"); \& my @forms = HTML::Form\->parse($response); .Ve .Sp Note that any object that implements a \fIdecoded_content()\fR, \fIbase()\fR and \&\fIcontent_charset()\fR method with similar behaviour as HTTP::Response will do. .Sp Additional options might be passed in to control how the parse method behaves. The following are all the options currently recognized: .RS 4 .ie n .IP """base => $uri""" 4 .el .IP "\f(CWbase => $uri\fR" 4 .IX Item "base => $uri" This is the \s-1URI\s0 used to retrieve the original document. This option is not optional ;\-) .ie n .IP """charset => $str""" 4 .el .IP "\f(CWcharset => $str\fR" 4 .IX Item "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 \*(L"\s-1UTF\-8\*(R".\s0 .ie n .IP """verbose => $bool""" 4 .el .IP "\f(CWverbose => $bool\fR" 4 .IX Item "verbose => $bool" Warn (print messages to \s-1STDERR\s0) about any bad \s-1HTML\s0 form constructs found. You can trap these with \f(CW$SIG\fR{_\|_WARN_\|_}. The default is not to issue warnings. .ie n .IP """strict => $bool""" 4 .el .IP "\f(CWstrict => $bool\fR" 4 .IX Item "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. .RE .RS 4 .RE .ie n .IP "$form\->push_input( $type, \e%attr, $verbose )" 4 .el .IP "\f(CW$form\fR\->push_input( \f(CW$type\fR, \e%attr, \f(CW$verbose\fR )" 4 .IX Item "$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. .Sp Example: .Sp .Vb 5 \& push_input( \*(Aqhidden\*(Aq, { \& name => \*(AqNewFormElement\*(Aq, \& id => \*(AqNewFormElementId\*(Aq, \& value => \*(Aqsome value\*(Aq, \& }); .Ve .ie n .IP "$method = $form\->method" 4 .el .IP "\f(CW$method\fR = \f(CW$form\fR\->method" 4 .IX Item "$method = $form->method" .PD 0 .ie n .IP "$form\->method( $new_method )" 4 .el .IP "\f(CW$form\fR\->method( \f(CW$new_method\fR )" 4 .IX Item "$form->method( $new_method )" .PD This method is gets/sets the \fImethod\fR name used for the HTTP::Request generated. It is a string like \*(L"\s-1GET\*(R"\s0 or \*(L"\s-1POST\*(R".\s0 .ie n .IP "$action = $form\->action" 4 .el .IP "\f(CW$action\fR = \f(CW$form\fR\->action" 4 .IX Item "$action = $form->action" .PD 0 .ie n .IP "$form\->action( $new_action )" 4 .el .IP "\f(CW$form\fR\->action( \f(CW$new_action\fR )" 4 .IX Item "$form->action( $new_action )" .PD This method gets/sets the \s-1URI\s0 which we want to apply the request \&\fImethod\fR to. .ie n .IP "$enctype = $form\->enctype" 4 .el .IP "\f(CW$enctype\fR = \f(CW$form\fR\->enctype" 4 .IX Item "$enctype = $form->enctype" .PD 0 .ie n .IP "$form\->enctype( $new_enctype )" 4 .el .IP "\f(CW$form\fR\->enctype( \f(CW$new_enctype\fR )" 4 .IX Item "$form->enctype( $new_enctype )" .PD This method gets/sets the encoding type for the form data. It is a string like \*(L"application/x\-www\-form\-urlencoded\*(R" or \*(L"multipart/form\-data\*(R". .ie n .IP "$accept = $form\->accept_charset" 4 .el .IP "\f(CW$accept\fR = \f(CW$form\fR\->accept_charset" 4 .IX Item "$accept = $form->accept_charset" .PD 0 .ie n .IP "$form\->accept_charset( $new_accept )" 4 .el .IP "\f(CW$form\fR\->accept_charset( \f(CW$new_accept\fR )" 4 .IX Item "$form->accept_charset( $new_accept )" .PD 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 \*(L"\s-1UNKNOWN\*(R"\s0 which we interpret as a request to use document charset as specified by the 'charset' parameter of the \fIparse()\fR method. .ie n .IP "$value = $form\->attr( $name )" 4 .el .IP "\f(CW$value\fR = \f(CW$form\fR\->attr( \f(CW$name\fR )" 4 .IX Item "$value = $form->attr( $name )" .PD 0 .ie n .IP "$form\->attr( $name, $new_value )" 4 .el .IP "\f(CW$form\fR\->attr( \f(CW$name\fR, \f(CW$new_value\fR )" 4 .IX Item "$form->attr( $name, $new_value )" .PD This method give access to the original \s-1HTML\s0 attributes of the tag. The \f(CW$name\fR should always be passed in lower case. .Sp Example: .Sp .Vb 4 \& @f = HTML::Form\->parse( $html, $foo ); \& @f = grep $_\->attr("id") eq "foo", @f; \& die "No form named \*(Aqfoo\*(Aq found" unless @f; \& $foo = shift @f; .Ve .ie n .IP "$bool = $form\->strict" 4 .el .IP "\f(CW$bool\fR = \f(CW$form\fR\->strict" 4 .IX Item "$bool = $form->strict" .PD 0 .ie n .IP "$form\->strict( $bool )" 4 .el .IP "\f(CW$form\fR\->strict( \f(CW$bool\fR )" 4 .IX Item "$form->strict( $bool )" .PD 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. .ie n .IP "@inputs = $form\->inputs" 4 .el .IP "\f(CW@inputs\fR = \f(CW$form\fR\->inputs" 4 .IX Item "@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 \*(L"\s-1INPUTS\*(R"\s0 for what methods are available for the input objects returned. .ie n .IP "$input = $form\->find_input( $selector )" 4 .el .IP "\f(CW$input\fR = \f(CW$form\fR\->find_input( \f(CW$selector\fR )" 4 .IX Item "$input = $form->find_input( $selector )" .PD 0 .ie n .IP "$input = $form\->find_input( $selector, $type )" 4 .el .IP "\f(CW$input\fR = \f(CW$form\fR\->find_input( \f(CW$selector\fR, \f(CW$type\fR )" 4 .IX Item "$input = $form->find_input( $selector, $type )" .ie n .IP "$input = $form\->find_input( $selector, $type, $index )" 4 .el .IP "\f(CW$input\fR = \f(CW$form\fR\->find_input( \f(CW$selector\fR, \f(CW$type\fR, \f(CW$index\fR )" 4 .IX Item "$input = $form->find_input( $selector, $type, $index )" .ie n .IP "@inputs = $form\->find_input( $selector )" 4 .el .IP "\f(CW@inputs\fR = \f(CW$form\fR\->find_input( \f(CW$selector\fR )" 4 .IX Item "@inputs = $form->find_input( $selector )" .ie n .IP "@inputs = $form\->find_input( $selector, $type )" 4 .el .IP "\f(CW@inputs\fR = \f(CW$form\fR\->find_input( \f(CW$selector\fR, \f(CW$type\fR )" 4 .IX Item "@inputs = $form->find_input( $selector, $type )" .PD 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 \f(CW\*(C`undef\*(C'\fR if none match. .Sp If \f(CW$selector\fR is not \f(CW\*(C`undef\*(C'\fR, then the input's \fIname\fR, \fIid\fR or \fIclass\fR attribute must match. A selector prefixed with '#' must match the \fIid\fR attribute of the input. A selector prefixed with '.' matches the \fIclass\fR attribute. A selector prefixed with '^' or with no prefix matches the \fIname\fR attribute. .Sp .Vb 4 \& my @by_id = $form\->find_input( \*(Aq#some\-id\*(Aq ); \& my @by_class = $form\->find_input( \*(Aq.some\-class\*(Aq ); \& my @by_name = $form\->find_input( \*(Aq^some\-name\*(Aq ); \& my @also_by_name = $form\->find_input( \*(Aqsome\-name\*(Aq ); .Ve .Sp If you want to find an input that has no \fIname\fR at all, pass in a reference to \f(CW\*(C`undef\*(C'\fR. .Sp .Vb 1 \& my @nameless_inputs = $form\->find_input( \eundef ); .Ve .Sp If \f(CW$type\fR is not \f(CW\*(C`undef\*(C'\fR, then the input must have the specified type. The following type names are used: \*(L"text\*(R", \*(L"password\*(R", \*(L"hidden\*(R", \&\*(L"textarea\*(R", \*(L"file\*(R", \*(L"image\*(R", \*(L"submit\*(R", \*(L"radio\*(R", \*(L"checkbox\*(R" and \*(L"option\*(R". .Sp The \f(CW$index\fR is the sequence number of the input matched where 1 is the first. If combined with \f(CW$selector\fR and/or \f(CW$type\fR, then it selects the \&\fIn\fRth input with the given \fIname\fR and/or type. .ie n .IP "$value = $form\->value( $selector )" 4 .el .IP "\f(CW$value\fR = \f(CW$form\fR\->value( \f(CW$selector\fR )" 4 .IX Item "$value = $form->value( $selector )" .PD 0 .ie n .IP "$form\->value( $selector, $new_value )" 4 .el .IP "\f(CW$form\fR\->value( \f(CW$selector\fR, \f(CW$new_value\fR )" 4 .IX Item "$form->value( $selector, $new_value )" .PD The \fIvalue()\fR 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. .Sp If multiple inputs have the same name, only the first one will be affected. .Sp The call: .Sp .Vb 1 \& $form\->value(\*(Aqfoo\*(Aq) .Ve .Sp is basically a short-hand for: .Sp .Vb 1 \& $form\->find_input(\*(Aqfoo\*(Aq)\->value; .Ve .ie n .IP "@names = $form\->param" 4 .el .IP "\f(CW@names\fR = \f(CW$form\fR\->param" 4 .IX Item "@names = $form->param" .PD 0 .ie n .IP "@values = $form\->param( $name )" 4 .el .IP "\f(CW@values\fR = \f(CW$form\fR\->param( \f(CW$name\fR )" 4 .IX Item "@values = $form->param( $name )" .ie n .IP "$form\->param( $name, $value, ... )" 4 .el .IP "\f(CW$form\fR\->param( \f(CW$name\fR, \f(CW$value\fR, ... )" 4 .IX Item "$form->param( $name, $value, ... )" .ie n .IP "$form\->param( $name, \e@values )" 4 .el .IP "\f(CW$form\fR\->param( \f(CW$name\fR, \e@values )" 4 .IX Item "$form->param( $name, @values )" .PD Alternative interface to examining and setting the values of the form. .Sp 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. .Sp 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 \&\f(CW\*(C`undef\*(C'\fR is returned. .Sp 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. .Sp This interface resembles that of the \fIparam()\fR function of the \s-1CGI\s0 module. .ie n .IP "$form\->try_others( \e&callback )" 4 .el .IP "\f(CW$form\fR\->try_others( \e&callback )" 4 .IX Item "$form->try_others( &callback )" This method will iterate over all permutations of unvisited enumerated values ( \fIelements\fR in the \s-1HTML\s0 document. An input object basically represents a name/value pair, so when multiple \&\s-1HTML\s0 elements contribute to the same name/value pair in the submitted form they are combined. .PP The input elements that are mapped one-to-one are \*(L"text\*(R", \*(L"textarea\*(R", \&\*(L"password\*(R", \*(L"hidden\*(R", \*(L"file\*(R", \*(L"image\*(R", \*(L"submit\*(R" and \*(L"checkbox\*(R". For the \*(L"radio\*(R" and \*(L"option\*(R" inputs the story is not as simple: All 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 elements. For a element there will be one input object for each contained