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

I am trying to use the HTML::Form module to extract name/value pairs for a form on a page and store them in a hash with the form name being they key so for example -
$form{loginform} = ( name1 => value1, name2 => value2, name3 => value3, );

My code looks like this
@forms = HTML::Form->parse($response->content, $baseuri); foreach my $form (@forms) { foreach my $data ($form->inputs) { $hash{$data}=1; #print $data->form_name_value. "\n"; } }

This works partially and what I mean by that is that it will print the name/value pairs if the value has something in it. Otherwise it just exits. Please advise.

Replies are listed 'Best First'.
Re: help with HTML::Form
by jdporter (Paladin) on Nov 08, 2002 at 20:37 UTC
    HTML::Form is good for parsing the source of an HTML form, but not for dealing with the parameters passed to a submitted form. If you're trying to do the latter, use CGI.pm.

    But if you're really wanting to do the former, then keep in mind that, typically, few if any form inputs have values to start with.

    That being said, what you have above should work. But, two things:
    1. Forms don't usually have names. If you have more than one form, you'll need to "know" it by something else.
    2. In the inner loop, that $data variable will actually be a reference to an input object. You can use that as the key in a hash, but I'm not sure that's what you're looking for anyway. Perhaps you mean something like this:

    for my $input ( $form->inputs ) { my $name = $input->name; my $value = $input->value; if ( defined $name && defined $value ) { $hash{ $name } = $value; } }
    But if you could explain exactly what it is you're trying to accomplish, I might be able to show you better examples.

    jdporter
    ...porque es dificil estar guapo y blanco.

      Hi! I am using the HTML::Form to parse the form in order to collect information about the form (such as hidden parameters) and then do a POST/GET on the action with all the form parameters in place using LWP. My bigger picture of why I am doing all this, is to create a generic a web response time tool.
      If you have any better ideas/examples, I would appreciate it.