Subject: | [PATCH] Better support for finding <SELECT> list values. |
Currently there is no easy way that I can tell to find all the values for a <SELECT> list.
The fundamental issue seems to be that the that API is not as user-friendly as it could
be. While it's more common to think of a <SELECT> tag a single form input, HTML::Form
works directly with the <OPTION> sub-tag, and seems to fairly much ignore the SELECT
tag.
This makes it difficult to perform the function: "Show me all the inputs in the select tag
named FOO". Currently, if you try "find_input()" or possible_values(), only one value will
be returned. This didn't make sense to me until I read the docs in detail to find out how
SELECT and OPTION are supported.
While I'm not sure a better long-term API to support SELECT is, below is a patch to
HTML::Form that does allow one to easily fetch all the values in a <SELECT> list.
This is accomplished by altering find_input() to return a list of values in list context. This
at least makes the current API more functional and intuitive for me.
--- /usr/local/lib/perl5/site_perl/5.8.0/HTML/Form.pm Wed Jan 1 11:42:50 2003
+++ HTML/Form.pm Fri Aug 22 09:43:48 2003
@@ -276,22 +276,33 @@
first. If combined with $name and/or $type then it select the I<n>th
input with the given name and/or type.
+In list context, a list of matching inputs is returned, or undef if none are found.
+
=cut
sub find_input
{
my($self, $name, $type, $no) = @_;
- $no ||= 1;
- for (@{$self->{'inputs'}}) {
- if (defined $name) {
- next unless exists $_->{name};
- next if $name ne $_->{name};
+
+ # In list context, default to no index, otherwise "1"
+ $no ||= (wantarray ? undef : 1);
+ my @results;
+ for (@{$self->{'inputs'}}) {
+ if (defined $name) {
+ next unless exists $_->{name};
+ next if $name ne $_->{name};
+ }
+ next if $type && $type ne $_->{type};
+ next if ($no && --$no);
+
+ push @results, $_;
+ }
+ if (@results) {
+ return wantarray ? @results : $results[0];
+ }
+ else {
+ return undef;
}
- next if $type && $type ne $_->{type};
- next if --$no;
- return $_;
- }
- return undef;
}