aikiPupil has asked for the wisdom of the Perl Monks concerning the following question:

I am having trouble getting the values from a set of radio buttons. I need to read the values from a radio button groups choices and do pattern matching to make a decision on which radio button to select before submitting a form. I am able to select the input from the form representing the radio buttons and when I use Dumper to show the object it looks like this:

$VAR1 = bless( { 'type' => 'radio', 'menu' => [ { 'value' => 'file-20150610220001.zip', 'name' => '' }, { 'value' => 'file-20150611050002.zip', 'name' => '' }, { 'value' => 'file-20150610220001.zip', 'name' => '' } ], 'name' => 'FileName', '/' => '/' }, 'HTML::Form::ListInput' );

The problem I am having is when I try to access the properties of the object I am not getting what I expect. I have printed the Dumper output of the $input->{menu}[0] hash and expected to see the contents of the Hash, what I got is the following:

$VAR1 = 'HASH(0x7fd53685e830)'; $VAR2 = undef;

The following is an example of the code I have tried using that does not work and I can not find an example of code that reads through all the values in a radio button group.

for my $input ($form->inputs) { if (q{FileName} eq $input->{name}){ print Dumper($input); # Show First output block my %hash =$input->{menu}[0]; print Dumper(%hash); # Show Second output block #Here iterate the radio button values so I can select the butt +on representing the file with the newest date. #for( this is where I am getting stumped) # do work! #} } }

Any help would be appreciated as I can not seem to find any examples that read the values, all examples I can find show how to set values when you know for certain what the value choices will be in advance which is not applicable in my case. Thanks in advance!

Replies are listed 'Best First'.
Re: HTML::Form and reading radio button values
by tangent (Parson) on Jun 11, 2015 at 22:27 UTC
    The way Perl treats hashes and hash references can be confusing at first - what you are dealing with in this structure is an array reference which contains a list of hash references, and you need to know how to de-reference the structure. These changes to your code should help:
    for my $input ($form->inputs) { if (q{FileName} eq $input->{name}){ print Dumper($input); # Show First output block my $hash = $input->{menu}[0]; # '$hash' not '%hash' # $hash is the first button print Dumper($hash); # to iterate the radio button values my $menu = $input->{menu}; for my $button ( @$menu ) { my $file = $button->{value}; print "$file\n"; } } }
      Thank you very much for the reply, I had a suspicion I was confusing the hash/array/list usage. What you provided above worked for me!
Re: HTML::Form and reading radio button values
by Anonymous Monk on Jun 12, 2015 at 01:05 UTC
      Thank you very much for the reply, it did not occur to me to use the possible_values of the input object despite having looked right at it while reviewing the documentation.