From: Mischa Poslawsky Date: Fri, 1 Aug 2008 11:25:22 +0000 (+0200) Subject: do not modify parameters in radio/check X-Git-Url: http://git.shiar.net/perl/html-form-simple.git/commitdiff_plain/48891d33a852d6db31113e4e7c10e1cda5771ce3 do not modify parameters in radio/check --- diff --git a/lib/HTML/Form/Simple.pm b/lib/HTML/Form/Simple.pm index 3a58c79..911b864 100644 --- a/lib/HTML/Form/Simple.pm +++ b/lib/HTML/Form/Simple.pm @@ -164,38 +164,42 @@ sub select { sub radio { my $self = shift; - my ($name, $label, $value, $attr) = $self->_attr(3, @_); + my ($name, $label, $rows, $attr) = $self->_attr(3, @_); - if (not defined $value) { + if (not defined $rows) { if (defined $label) { - $value = ref $label eq 'ARRAY' ? [1 .. $#$label+1] : [1]; + $rows = ref $label eq 'ARRAY' ? [1 .. $#$label+1] : [1]; } else { - $value = [{}]; + $rows = [{}]; } } - elsif (ref $value ne 'ARRAY') { - $value = [$value]; + elsif (ref $rows ne 'ARRAY') { + $rows = [$rows]; } if (defined $label) { - $_ = ref $_ eq 'HASH' ? {%$_} : {value => $_} for @$value; - $_->{label} = ref $label eq 'ARRAY' ? shift @$label : $label for @$value; + $rows = [ map { ref $_ eq 'HASH' ? {%$_} : {value => $_} } @$rows ]; + if (ref $label eq 'ARRAY') { + $rows->[$_]->{label} = $label->[$_] for 0 .. $#$rows; + } else { + $_->{label} = $label for @$rows; + } } - $self->select($name, $value, {%$attr, type => 'radio'}); + $self->select($name, $rows, {%$attr, type => 'radio'}); } sub check { my $self = shift; my ($name, $label, $checked, $attr) = $self->_attr(3, @_); - my $rows = defined $label ? ref $label eq 'ARRAY' ? $label : [$label] : [{}]; + my $rows = defined $label ? ref $label eq 'ARRAY' ? [@$label] : [$label] : [{}]; ref $_ eq 'HASH' or $_ = {label => $_} for @$rows; if (defined $checked) { if (ref $checked eq 'ARRAY') { - $_->{checked} = shift @$checked for @$rows; - push @$rows, map { {checked => $_} } @$checked; + $rows->[$_]->{checked} = $checked->[$_] for 0 .. $#$rows; + push @$rows, map { {checked => $_} } @$checked[@$rows .. $#$checked]; } else { $_->{checked} = $checked for @$rows; diff --git a/t/html.t b/t/html.t index 2fea804..b4420e1 100644 --- a/t/html.t +++ b/t/html.t @@ -5,7 +5,7 @@ use warnings; use Test::More; -plan tests => 55; +plan tests => 59; use_ok('HTML::Form::Simple'); @@ -356,6 +356,39 @@ is_deeply( 'multiple radios with custom values' ); +{ + # make sure arguments aren't modified + my @args = (foo => [0, 1], [0, {value => 1}], {name => 0, value => 1}); + my @orgs = (foo => [0, 1], [0, {value => 1}], {name => 0, value => 1}); + my @output = ( + '', + '', + ); + + is_deeply( + [ $form->radio(@args) ], + \@output, + 'options var to radio' + ); + + is_deeply( + [ $form->check(@args) ], + [ + '', + '', + ], + 'options var to check' + ); + + is( + scalar $form->radio(@args), + join('', @output), + 'options var again to radio' + ); + + is_deeply(\@args, \@orgs, 'options var unmodified'); +} + # check is(