On Mon, Aug 25, 2003 at 05:39:54PM -0700, Gisle Aas wrote: > Mark Stosberg <[EMAIL PROTECTED]> writes: > > > Also functions could be added to make it easier to handle inputs with > > multiple values. For me, I think the functions like CGI.pm's "param" and "append" > > would be must intuitive. These functions would not care about ordering > > like value() does, they would just set the first value that works. > > Would this method work for you? > > sub HTML::Form::param { > my $self = shift; > if (@_) { > my $name = shift; > my @inputs; > for ($self->inputs) { > my $n = $_->name; > next if !defined($n) || $n ne $name; > push(@inputs, $_); > } > > if (@_) { > # set > die "No '$name' parameter exists" unless @inputs; > while (@_) { > my $v = shift; > my $err; > for my $i (0 .. @inputs-1) { > eval { > $inputs[$i]->value($v); > }; > unless ($@) { > undef($err); > splice(@inputs, $i, 1); > last; > } > $err ||= $@; > } > die $err if $err; > } > } > else { > # get > my @v; > for (@inputs) { > push(@v, $_->value); > } > return wantarray ? @v : $v[0]; > } > } > else { > # list parameter names > my @n; > my %seen; > for ($self->inputs) { > my $n = $_->name; > next if !defined($n) || $seen{$n}++; > push(@n, $n); > } > return @n; > } > }
Gisle, A big thanks for putting this together. It works almost exactly as I would like and expect. Below are a some tests I put together to test it. I did make one small change to make it work more like I expected. It's this: push(@v, $_->value) if defined $_->value; I only add values to the return list if they are defined. This fixed the "setting multiple values" test below. Without the patch, there was an extra "undef" returned in the array. I realize "undef" is a valid value for a form element, but returning it breaks the "do what I mean" interface of the new param() function that I have in mind. I'd rather have it this way, and people can use "value" if they want to tinker with the undefined values. Do you think this could make it in a future version? I'd volunteer to write the first draft of the POD. I may already use it this week as part of some automated website testing. Mark ############################# #!/usr/bin/perl use Test::More tests => 6; use Test::Differences; use HTML::Form; my($form) = HTML::Form->parse(<<"EOT", "http://example.com"); <form> <input type="hidden" name="hidden_1"> <input type="checkbox" name="checkbox_1" value="c1_v1" CHECKED> <input type="checkbox" name="checkbox_1" value="c1_v2" CHECKED> <input type="checkbox" name="checkbox_2" value="c2_v1" CHECKED> <select name="multi_select_field" multiple="1"> <option> 1 <option> 2 <option> 3 </select> </form> EOT my @params; eval { @params = $form->param; }; ok(!$@, 'passes basic eval{}'); eq_or_diff([EMAIL PROTECTED], [qw/hidden_1 checkbox_1 checkbox_2 multi_select_field/], 'returning parameter list in array context'); is($form->param('checkbox_2'), 'c2_v1', 'returning single value from single-valued field'); like($form->param('checkbox_1'), qr/^c1_v\d$/, 'returning single value from multi-valued field'); $form->param('hidden_1','hidden_1_value'); is($form->param('hidden_1'), 'hidden_1_value', 'setting a single value'); $form->param('multi_select_field',1,2); my @vals = $form->param('multi_select_field'); eq_or_diff([EMAIL PROTECTED], [qw/1 2/], 'setting multiple values'); __END__ -- http://mark.stosberg.com/