10 my %form_tags = map {$_ => 1} qw(input textarea button select option);
14 password => "TextInput",
15 hidden => "TextInput",
16 textarea => "TextInput",
18 "reset" => "IgnoreInput",
21 checkbox => "ListInput",
22 option => "ListInput",
24 button => "SubmitInput",
25 submit => "SubmitInput",
26 image => "ImageInput",
29 keygen => "KeygenInput",
34 HTML::Form - Class that represents an HTML form element
39 $form = HTML::Form->parse($html, $base_uri);
40 $form->value(query => "Perl");
43 $ua = LWP::UserAgent->new;
44 $response = $ua->request($form->click);
48 Objects of the C<HTML::Form> class represents a single HTML
49 C<E<lt>formE<gt> ... E<lt>/formE<gt>> instance. A form consists of a
50 sequence of inputs that usually have names, and which can take on
51 various values. The state of a form can be tweaked and it can then be
52 asked to provide C<HTTP::Request> objects that can be passed to the
53 request() method of C<LWP::UserAgent>.
55 The following methods are available:
59 =item @forms = HTML::Form->parse( $response )
61 =item @forms = HTML::Form->parse( $html_document, $base )
63 =item @forms = HTML::Form->parse( $html_document, %opt )
65 The parse() class method will parse an HTML document and build up
66 C<HTML::Form> objects for each <form> element found. If called in scalar
67 context only returns the first <form>. Returns an empty list if there
68 are no forms to be found.
70 The $base is the URI used to retrieve the $html_document. It is
71 needed to resolve relative action URIs. If the document was retrieved
72 with LWP then this this parameter is obtained from the
73 $response->base() method, as shown by the following example:
75 my $ua = LWP::UserAgent->new;
76 my $response = $ua->get("http://www.example.com/form.html");
77 my @forms = HTML::Form->parse($response->decoded_content,
80 The parse() method can parse from an C<HTTP::Response> object
81 directly, so the example above can be more conveniently written as:
83 my $ua = LWP::UserAgent->new;
84 my $response = $ua->get("http://www.example.com/form.html");
85 my @forms = HTML::Form->parse($response);
87 Note that any object that implements a decoded_content() and base() method
88 with similar behaviour as C<HTTP::Response> will do.
90 Finally options might be passed in to control how the parse method
91 behaves. The following options are currently recognized:
97 Another way to provide the base URI.
101 Print messages to STDERR about any bad HTML form constructs found.
111 unshift(@_, "base") if @_ == 1;
114 require HTML::TokeParser;
115 my $p = HTML::TokeParser->new(ref($html) ? $html->decoded_content(ref => 1) : \$html);
116 die "Failed to create HTML::TokeParser object" unless $p;
119 $p->report_tags(qw(form input textarea select optgroup option keygen label button));
122 my $base_uri = delete $opt{base};
123 my $verbose = delete $opt{verbose};
126 Carp::carp("Unrecognized option $_ in HTML::Form->parse") for sort keys %opt;
129 unless (defined $base_uri) {
131 $base_uri = $html->base;
134 Carp::croak("HTML::Form::parse: No \$base_uri provided");
139 my $f; # current form
141 my %openselect; # index to the open instance of a select
143 while (my $t = $p->get_tag) {
144 my($tag,$attr) = @$t;
145 if ($tag eq "form") {
146 my $action = delete $attr->{'action'};
147 $action = "" unless defined $action;
148 $action = URI->new_abs($action, $base_uri);
149 $f = $class->new($attr->{'method'},
155 my(%labels, $current_label);
156 while (my $t = $p->get_tag) {
157 my($tag, $attr) = @$t;
158 last if $tag eq "/form";
160 # if we are inside a label tag, then keep
161 # appending any text to the current label
162 if(defined $current_label) {
163 $current_label = join " ",
164 grep { defined and length }
169 if ($tag eq "input") {
170 $attr->{value_name} =
171 exists $attr->{id} && exists $labels{$attr->{id}} ? $labels{$attr->{id}} :
172 defined $current_label ? $current_label :
176 if ($tag eq "label") {
177 $current_label = $p->get_phrase;
178 $labels{ $attr->{for} } = $current_label
179 if exists $attr->{for};
181 elsif ($tag eq "/label") {
182 $current_label = undef;
184 elsif ($tag eq "input") {
185 my $type = delete $attr->{type} || "text";
186 $f->push_input($type, $attr);
188 elsif ($tag eq "button") {
189 my $type = delete $attr->{type} || "submit";
190 $f->push_input($type, $attr);
192 elsif ($tag eq "textarea") {
193 $attr->{textarea_value} = $attr->{value}
194 if exists $attr->{value};
195 my $text = $p->get_text("/textarea");
196 $attr->{value} = $text;
197 $f->push_input("textarea", $attr);
199 elsif ($tag eq "select") {
200 # rename attributes reserved to come for the option tag
201 for ("value", "value_name") {
202 $attr->{"select_$_"} = delete $attr->{$_}
203 if exists $attr->{$_};
205 # count this new select option separately
206 $openselect{$attr->{name}}++;
208 while ($t = $p->get_tag) {
210 last if $tag eq "/select";
211 next if $tag =~ m,/?optgroup,;
212 next if $tag eq "/option";
213 if ($tag eq "option") {
215 # rename keys so they don't clash with %attr
217 next if $_ eq "value";
218 $a{"option_$_"} = delete $a{$_};
220 while (my($k,$v) = each %$attr) {
223 $a{value_name} = $p->get_trimmed_text;
224 $a{value} = delete $a{value_name}
225 unless defined $a{value};
226 $a{idx} = $openselect{$attr->{name}};
227 $f->push_input("option", \%a);
230 warn("Bad <select> tag '$tag' in $base_uri\n") if $verbose;
231 if ($tag eq "/form" ||
233 $tag eq "textarea" ||
237 # MSIE implictly terminate the <select> here, so we
238 # try to do the same. Actually the MSIE behaviour
239 # appears really strange: <input> and <textarea>
240 # do implictly close, but not <select>, <keygen> or
242 my $type = ($tag =~ s,^/,,) ? "E" : "S";
243 $p->unget_token([$type, $tag, @$t]);
249 elsif ($tag eq "keygen") {
250 $f->push_input("keygen", $attr);
254 elsif ($form_tags{$tag}) {
255 warn("<$tag> outside <form> in $base_uri\n") if $verbose;
262 wantarray ? @forms : $forms[0];
267 my $self = bless {}, $class;
268 $self->{method} = uc(shift || "GET");
269 $self->{action} = shift || Carp::croak("No action defined");
270 $self->{enctype} = lc(shift || "application/x-www-form-urlencoded");
271 $self->{inputs} = [@_];
278 my($self, $type, $attr) = @_;
280 my $class = $type2class{$type};
282 Carp::carp("Unknown input type '$type'") if $^W;
283 $class = "TextInput";
285 $class = "HTML::Form::$class";
287 push(@extra, readonly => 1) if $type eq "hidden";
289 delete $attr->{type}; # don't confuse the type argument
290 my $input = $class->new(type => $type, %$attr, @extra);
291 $input->add_to_form($self);
295 =item $method = $form->method
297 =item $form->method( $new_method )
299 This method is gets/sets the I<method> name used for the
300 C<HTTP::Request> generated. It is a string like "GET" or "POST".
302 =item $action = $form->action
304 =item $form->action( $new_action )
306 This method gets/sets the URI which we want to apply the request
309 =item $enctype = $form->enctype
311 =item $form->enctype( $new_enctype )
313 This method gets/sets the encoding type for the form data. It is a
314 string like "application/x-www-form-urlencoded" or "multipart/form-data".
319 # Set up some accesor
320 for (qw(method action enctype)) {
325 my $old = $self->{$m};
326 $self->{$m} = shift if @_;
330 *uri = \&action; # alias
333 =item $value = $form->attr( $name )
335 =item $form->attr( $name, $new_value )
337 This method give access to the original HTML attributes of the <form> tag.
338 The $name should always be passed in lower case.
342 @f = HTML::Form->parse( $html, $foo );
343 @f = grep $_->attr("id") eq "foo", @f;
344 die "No form named 'foo' found" unless @f;
352 return undef unless defined $name;
354 my $old = $self->{attr}{$name};
355 $self->{attr}{$name} = shift if @_;
359 =item @inputs = $form->inputs
361 This method returns the list of inputs in the form. If called in
362 scalar context it returns the number of inputs contained in the form.
363 See L</INPUTS> for what methods are available for the input objects
371 @{$self->{'inputs'}};
375 =item $input = $form->find_input( $name )
377 =item $input = $form->find_input( $name, $type )
379 =item $input = $form->find_input( $name, $type, $index )
381 This method is used to locate specific inputs within the form. All
382 inputs that match the arguments given are returned. In scalar context
383 only the first is returned, or C<undef> if none match.
385 If $name is specified, then the input must have the indicated name.
387 If $type is specified, then the input must have the specified type.
388 The following type names are used: "text", "password", "hidden",
389 "textarea", "file", "image", "submit", "radio", "checkbox" and "option".
391 The $index is the sequence number of the input matched where 1 is the
392 first. If combined with $name and/or $type then it select the I<n>th
393 input with the given name and/or type.
399 my($self, $name, $type, $no) = @_;
403 for (@{$self->{'inputs'}}) {
405 next unless exists $_->{name};
406 next if $name ne $_->{name};
408 next if $type && $type ne $_->{type};
410 next if $no && $no != $c;
418 for (@{$self->{'inputs'}}) {
420 next unless exists $_->{name};
421 next if $name ne $_->{name};
423 next if $type && $type ne $_->{type};
434 for (@{$self->{'inputs'}}) {
440 =item $value = $form->value( $name )
442 =item $form->value( $name, $new_value )
444 The value() method can be used to get/set the value of some input. If
445 no input has the indicated name, then this method will croak.
447 If multiple inputs have the same name, only the first one will be
456 $form->find_input('foo')->value;
464 my $input = $self->find_input($key);
465 Carp::croak("No such field '$key'") unless $input;
466 local $Carp::CarpLevel = 1;
470 =item @names = $form->param
472 =item @values = $form->param( $name )
474 =item $form->param( $name, $value, ... )
476 =item $form->param( $name, \@values )
478 Alternative interface to examining and setting the values of the form.
480 If called without arguments then it returns the names of all the
481 inputs in the form. The names will not repeat even if multiple inputs
482 have the same name. In scalar context the number of different names
485 If called with a single argument then it returns the value or values
486 of inputs with the given name. If called in scalar context only the
487 first value is returned. If no input exists with the given name, then
488 C<undef> is returned.
490 If called with 2 or more arguments then it will set values of the
491 named inputs. This form will croak if no inputs have the given name
492 or if any of the values provided does not fit. Values can also be
493 provided as a reference to an array. This form will allow unsetting
494 all values with the given name as well.
496 This interface resembles that of the param() function of the CGI
506 for ($self->inputs) {
508 next if !defined($n) || $n ne $name;
514 die "No '$name' parameter exists" unless @inputs;
516 @v = @{$v[0]} if @v == 1 && ref($v[0]);
520 for my $i (0 .. @inputs-1) {
522 $inputs[$i]->value($v);
526 splice(@inputs, $i, 1);
534 # the rest of the input should be cleared
543 if (defined(my $v = $_->value)) {
547 return wantarray ? @v : $v[0];
551 # list parameter names
554 for ($self->inputs) {
556 next if !defined($n) || $seen{$n}++;
564 =item $form->try_others( \&callback )
566 This method will iterate over all permutations of unvisited enumerated
567 values (<select>, <radio>, <checkbox>) and invoke the callback for
568 each. The callback is passed the $form as argument. The return value
569 from the callback is ignored and the try_others() method itself does
578 for (@{$self->{'inputs'}}) {
579 my @not_tried_yet = $_->other_possible_values;
580 next unless @not_tried_yet;
581 push(@try, [\@not_tried_yet, $_]);
584 $self->_try($cb, \@try, 0);
589 my($self, $cb, $try, $i) = @_;
590 for (@{$try->[$i][0]}) {
591 $try->[$i][1]->value($_);
593 $self->_try($cb, $try, $i+1) if $i+1 < @$try;
598 =item $request = $form->make_request
600 Will return an C<HTTP::Request> object that reflects the current setting
601 of the form. You might want to use the click() method instead.
608 my $method = uc $self->{'method'};
609 my $uri = $self->{'action'};
610 my $enctype = $self->{'enctype'};
611 my @form = $self->form;
613 if ($method eq "GET") {
614 require HTTP::Request;
615 $uri = URI->new($uri, "http");
616 $uri->query_form(@form);
617 return HTTP::Request->new(GET => $uri);
619 elsif ($method eq "POST") {
620 require HTTP::Request::Common;
621 return HTTP::Request::Common::POST($uri, \@form,
622 Content_Type => $enctype);
625 Carp::croak("Unknown method '$method'");
630 =item $request = $form->click
632 =item $request = $form->click( $name )
634 =item $request = $form->click( $x, $y )
636 =item $request = $form->click( $name, $x, $y )
638 Will "click" on the first clickable input (which will be of type
639 C<submit> or C<image>). The result of clicking is an C<HTTP::Request>
640 object that can then be passed to C<LWP::UserAgent> if you want to
641 obtain the server response.
643 If a $name is specified, we will click on the first clickable input
644 with the given name, and the method will croak if no clickable input
645 with the given name is found. If $name is I<not> specified, then it
646 is ok if the form contains no clickable inputs. In this case the
647 click() method returns the same request as the make_request() method
650 If there are multiple clickable inputs with the same name, then there
651 is no way to get the click() method of the C<HTML::Form> to click on
652 any but the first. If you need this you would have to locate the
653 input with find_input() and invoke the click() method on the given
656 A click coordinate pair can also be provided, but this only makes a
657 difference if you clicked on an image. The default coordinate is
658 (1,1). The upper-left corner of the image is (0,0), but some badly
659 coded CGI scripts are known to not recognize this. Therefore (1,1) was
660 selected as a safer default.
668 $name = shift if (@_ % 2) == 1; # odd number of arguments
670 # try to find first submit button to activate
671 for (@{$self->{'inputs'}}) {
672 next unless $_->can("click");
673 next if $name && $_->name ne $name;
674 next if $_->disabled;
675 return $_->click($self, @_);
677 Carp::croak("No clickable input with name $name") if $name;
682 =item @kw = $form->form
684 Returns the current setting as a sequence of key/value pairs. Note
685 that keys might be repeated, which means that some values might be
686 lost if the return values are assigned to a hash.
688 In scalar context this method returns the number of key/value pairs
696 map { $_->form_name_value($self) } @{$self->{'inputs'}};
702 Returns a textual representation of current state of the form. Mainly
703 useful for debugging. If called in void context, then the dump is
711 my $method = $self->{'method'};
712 my $uri = $self->{'action'};
713 my $enctype = $self->{'enctype'};
714 my $dump = "$method $uri";
715 $dump .= " ($enctype)"
716 if $enctype ne "application/x-www-form-urlencoded";
717 $dump .= " [$self->{attr}{name}]"
718 if exists $self->{attr}{name};
720 for ($self->inputs) {
721 $dump .= " " . $_->dump . "\n";
723 print STDERR $dump unless defined wantarray;
728 #---------------------------------------------------
729 package HTML::Form::Input;
735 An C<HTML::Form> objects contains a sequence of I<inputs>. References to
736 the inputs can be obtained with the $form->inputs or $form->find_input
739 Note that there is I<not> a one-to-one correspondence between input
740 I<objects> and E<lt>inputE<gt> I<elements> in the HTML document. An
741 input object basically represents a name/value pair, so when multiple
742 HTML elements contribute to the same name/value pair in the submitted
743 form they are combined.
745 The input elements that are mapped one-to-one are "text", "textarea",
746 "password", "hidden", "file", "image", "submit" and "checkbox". For
747 the "radio" and "option" inputs the story is not as simple: All
748 E<lt>input type="radio"E<gt> elements with the same name will
749 contribute to the same input radio object. The number of radio input
750 objects will be the same as the number of distinct names used for the
751 E<lt>input type="radio"E<gt> elements. For a E<lt>selectE<gt> element
752 without the C<multiple> attribute there will be one input object of
753 type of "option". For a E<lt>select multipleE<gt> element there will
754 be one input object for each contained E<lt>optionE<gt> element. Each
755 one of these option objects will have the same name.
757 The following methods are available for the I<input> objects:
766 my $self = bless {@_}, $class;
772 my($self, $form) = @_;
773 push(@{$form->{'inputs'}}, $self);
782 Returns the type of this input. The type is one of the following
783 strings: "text", "password", "hidden", "textarea", "file", "image", "submit",
784 "radio", "checkbox" or "option".
793 =item $name = $input->name
795 =item $input->name( $new_name )
797 This method can be used to get/set the current name of the input.
799 =item $value = $input->value
801 =item $input->value( $new_value )
803 This method can be used to get/set the current value of an
806 If the input only can take an enumerated list of values, then it is an
807 error to try to set it to something else and the method will croak if
810 You will also be able to set the value of read-only inputs, but a
811 warning will be generated if running under C<perl -w>.
818 my $old = $self->{name};
819 $self->{name} = shift if @_;
826 my $old = $self->{value};
827 $self->{value} = shift if @_;
831 =item $input->possible_values
833 Returns a list of all values that an input can take. For inputs that
834 do not have discrete values, this returns an empty list.
843 =item $input->other_possible_values
845 Returns a list of all values not tried yet.
849 sub other_possible_values
854 =item $input->value_names
856 For some inputs the values can have names that are different from the
857 values themselves. The number of names returned by this method will
858 match the number of values reported by $input->possible_values.
860 When setting values using the value() method it is also possible to
861 use the value names in place of the value itself.
869 =item $bool = $input->readonly
871 =item $input->readonly( $bool )
873 This method is used to get/set the value of the readonly attribute.
874 You are allowed to modify the value of readonly inputs, but setting
875 the value will generate some noise when warnings are enabled. Hidden
876 fields always start out readonly.
882 my $old = $self->{readonly};
883 $self->{readonly} = shift if @_;
887 =item $bool = $input->disabled
889 =item $input->disabled( $bool )
891 This method is used to get/set the value of the disabled attribute.
892 Disabled inputs do not contribute any key/value pairs for the form
899 my $old = $self->{disabled};
900 $self->{disabled} = shift if @_;
904 =item $input->form_name_value
906 Returns a (possible empty) list of key/value pairs that should be
907 incorporated in the form value from this input.
914 my $name = $self->{'name'};
915 return unless defined $name;
916 return if $self->disabled;
917 my $value = $self->value;
918 return unless defined $value;
919 return ($name => $value);
925 my $name = $self->name;
926 $name = "<NONAME>" unless defined $name;
927 my $value = $self->value;
928 $value = "<UNDEF>" unless defined $value;
929 my $dump = "$name=$value";
931 my $type = $self->type;
933 $type .= " disabled" if $self->disabled;
934 $type .= " readonly" if $self->readonly;
935 return sprintf "%-30s %s", $dump, "($type)" unless $self->{menu};
939 for (@{$self->{menu}}) {
940 my $opt = $_->{value};
941 $opt = "<UNDEF>" unless defined $opt;
942 $opt .= "/$_->{name}"
943 if defined $_->{name} && length $_->{name} && $_->{name} ne $opt;
944 substr($opt,0,0) = "-" if $_->{disabled};
945 if (exists $self->{current} && $self->{current} == $i) {
946 substr($opt,0,0) = "!" unless $_->{seen};
947 substr($opt,0,0) = "*";
950 substr($opt,0,0) = ":" if $_->{seen};
956 return sprintf "%-30s %-10s %s", $dump, "($type)", "[" . join("|", @menu) . "]";
960 #---------------------------------------------------
961 package HTML::Form::TextInput;
962 @HTML::Form::TextInput::ISA=qw(HTML::Form::Input);
972 my $old = $self->{value};
973 $old = "" unless defined $old;
975 Carp::carp("Input '$self->{name}' is readonly")
976 if $^W && $self->{readonly};
978 my $n = exists $self->{maxlength} ? $self->{maxlength} : undef;
979 Carp::carp("Input '$self->{name}' has maxlength '$n'")
980 if $^W && defined($n) && defined($new) && length($new) > $n;
981 $self->{value} = $new;
986 #---------------------------------------------------
987 package HTML::Form::IgnoreInput;
988 @HTML::Form::IgnoreInput::ISA=qw(HTML::Form::Input);
996 #---------------------------------------------------
997 package HTML::Form::ListInput;
998 @HTML::Form::ListInput::ISA=qw(HTML::Form::Input);
1000 #select/option (val1, val2, ....)
1001 #input/radio (undef, val1, val2,...)
1002 #input/checkbox (undef, value)
1003 #select-multiple/option (undef, value)
1008 my $self = $class->SUPER::new(@_);
1010 my $value = delete $self->{value};
1011 my $value_name = delete $self->{value_name};
1012 my $type = $self->{type};
1014 if ($type eq "checkbox") {
1015 $value = "on" unless defined $value;
1017 { value => undef, name => "off", },
1018 { value => $value, name => $value_name, },
1020 $self->{current} = (delete $self->{checked}) ? 1 : 0;
1024 $self->{option_disabled}++
1025 if $type eq "radio" && delete $self->{disabled};
1027 {value => $value, name => $value_name},
1029 my $checked = $self->{checked} || $self->{option_selected};
1030 delete $self->{checked};
1031 delete $self->{option_selected};
1032 if (exists $self->{multiple}) {
1033 unshift(@{$self->{menu}}, { value => undef, name => "off"});
1034 $self->{current} = $checked ? 1 : 0;
1037 $self->{current} = 0 if $checked;
1045 my($self, $form) = @_;
1046 my $type = $self->type;
1048 return $self->SUPER::add_to_form($form)
1049 if $type eq "checkbox";
1051 if ($type eq "option" && exists $self->{multiple}) {
1052 $self->{disabled} ||= delete $self->{option_disabled};
1053 return $self->SUPER::add_to_form($form);
1056 die "Assert" if @{$self->{menu}} != 1;
1057 my $m = $self->{menu}[0];
1058 $m->{disabled}++ if delete $self->{option_disabled};
1060 my $prev = $form->find_input($self->{name}, $self->{type}, $self->{idx});
1061 return $self->SUPER::add_to_form($form) unless $prev;
1064 $prev->{current} = @{$prev->{menu}} if exists $self->{current};
1065 push(@{$prev->{menu}}, $m);
1071 if ($self->{type} eq "option" && !(exists $self->{current})) {
1072 $self->{current} = 0;
1074 $self->{menu}[$self->{current}]{seen}++ if exists $self->{current};
1080 my $type = $self->type;
1082 my $old = $self->{disabled} || _menu_all_disabled(@{$self->{menu}});
1085 $self->{disabled} = $v;
1086 for (@{$self->{menu}}) {
1087 $_->{disabled} = $v;
1093 sub _menu_all_disabled {
1095 return 0 unless $_->{disabled};
1104 $old = $self->{menu}[$self->{current}]{value} if exists $self->{current};
1110 for (@{$self->{menu}}) {
1111 if ((defined($val) && defined($_->{value}) && $val eq $_->{value}) ||
1112 (!defined($val) && !defined($_->{value}))
1116 $disabled = $_->{disabled};
1117 last unless $disabled;
1121 if (!(defined $cur) || $disabled) {
1123 # try to search among the alternative names as well
1126 my $lc_val = lc($val);
1127 for (@{$self->{menu}}) {
1128 if (defined $_->{name}) {
1129 if ($val eq $_->{name}) {
1130 $disabled = $_->{disabled};
1132 last unless $disabled;
1134 if (!defined($cur_ignorecase) && $lc_val eq lc($_->{name})) {
1135 $cur_ignorecase = $i;
1140 unless (defined $cur) {
1141 $cur = $cur_ignorecase;
1143 $disabled = $self->{menu}[$cur]{disabled};
1146 my $n = $self->name;
1147 Carp::croak("Illegal value '$val' for field '$n'");
1152 my $n = $self->name;
1153 Carp::croak("The '$n' field can't be unchecked");
1157 my $n = $self->name;
1158 Carp::croak("The value '$val' has been disabled for field '$n'");
1160 $self->{current} = $cur;
1161 $self->{menu}[$cur]{seen}++;
1168 Some input types represent toggles that can be turned on/off. This
1169 includes "checkbox" and "option" inputs. Calling this method turns
1170 this input on without having to know the value name. If the input is
1171 already on, then nothing happens.
1173 This has the same effect as:
1175 $input->value($input->possible_values[1]);
1177 The input can be turned off with:
1179 $input->value(undef);
1186 $self->{current} = 1;
1187 $self->{menu}[1]{seen}++;
1193 map $_->{value}, @{$self->{menu}};
1196 sub other_possible_values
1199 map $_->{value}, grep !$_->{seen}, @{$self->{menu}};
1205 for (@{$self->{menu}}) {
1207 $n = $_->{value} unless defined $n;
1214 #---------------------------------------------------
1215 package HTML::Form::SubmitInput;
1216 @HTML::Form::SubmitInput::ISA=qw(HTML::Form::Input);
1221 =item $input->click($form, $x, $y)
1223 Some input types (currently "submit" buttons and "images") can be
1224 clicked to submit the form. The click() method returns the
1225 corresponding C<HTTP::Request> object.
1231 my($self,$form,$x,$y) = @_;
1232 for ($x, $y) { $_ = 1 unless defined; }
1233 local($self->{clicked}) = [$x,$y];
1234 return $form->make_request;
1240 return unless $self->{clicked};
1241 return $self->SUPER::form_name_value(@_);
1245 #---------------------------------------------------
1246 package HTML::Form::ImageInput;
1247 @HTML::Form::ImageInput::ISA=qw(HTML::Form::SubmitInput);
1252 my $clicked = $self->{clicked};
1253 return unless $clicked;
1254 return if $self->{disabled};
1255 my $name = $self->{name};
1256 $name = (defined($name) && length($name)) ? "$name." : "";
1257 return ("${name}x" => $clicked->[0],
1258 "${name}y" => $clicked->[1]
1262 #---------------------------------------------------
1263 package HTML::Form::FileInput;
1264 @HTML::Form::FileInput::ISA=qw(HTML::Form::TextInput);
1268 If the input is of type C<file>, then it has these additional methods:
1274 This is just an alias for the value() method. It sets the filename to
1284 =item $filename = $input->filename
1286 =item $input->filename( $new_filename )
1288 This get/sets the filename reported to the server during file upload.
1289 This attribute defaults to the value reported by the file() method.
1295 my $old = $self->{filename};
1296 $self->{filename} = shift if @_;
1297 $old = $self->file unless defined $old;
1301 =item $content = $input->content
1303 =item $input->content( $new_content )
1305 This get/sets the file content provided to the server during file
1306 upload. This method can be used if you do not want the content to be
1307 read from an actual file.
1313 my $old = $self->{content};
1314 $self->{content} = shift if @_;
1318 =item @headers = $input->headers
1320 =item input->headers($key => $value, .... )
1322 This get/set additional header fields describing the file uploaded.
1323 This can for instance be used to set the C<Content-Type> reported for
1330 my $old = $self->{headers} || [];
1331 $self->{headers} = [@_] if @_;
1335 sub form_name_value {
1336 my($self, $form) = @_;
1337 return $self->SUPER::form_name_value($form)
1338 if $form->method ne "POST" ||
1339 $form->enctype ne "multipart/form-data";
1341 my $name = $self->name;
1342 return unless defined $name;
1343 return if $self->{disabled};
1345 my $file = $self->file;
1346 my $filename = $self->filename;
1347 my @headers = $self->headers;
1348 my $content = $self->content;
1349 if (defined $content) {
1350 $filename = $file unless defined $filename;
1352 unshift(@headers, "Content" => $content);
1354 elsif (!defined($file) || length($file) == 0) {
1358 # legacy (this used to be the way to do it)
1359 if (ref($file) eq "ARRAY") {
1360 my $f = shift @$file;
1361 my $fn = shift @$file;
1362 push(@headers, @$file);
1364 $filename = $fn unless defined $filename;
1367 return ($name => [$file, $filename, @headers]);
1370 package HTML::Form::KeygenInput;
1371 @HTML::Form::KeygenInput::ISA=qw(HTML::Form::Input);
1375 return $self->{challenge};
1380 return lc($self->{keytype} || 'rsa');
1391 L<LWP>, L<LWP::UserAgent>, L<HTML::Parser>
1395 Copyright 1998-2005 Gisle Aas.
1397 This library is free software; you can redistribute it and/or
1398 modify it under the same terms as Perl itself.