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

I'm using LWP. I have a CSV file with 0 to 20+ usable fields per line. I have a Web page with a form. It has a multiple-select box with hundreds of options in it. As you might guess, the fields in the CSV file match options in the menu. So here's the problem: how do I send multiple entries for that menu?
$webpage = $browser->post($form_url, [ username => $username, widget => $widget{1}, # I need to repeat this line n times. created => '1126175051' ],Content_Type => 'form-data'));
I can brute-force it, and just manually copy & paste that line 100 times -- I don't think more than 100 widgets are ever selected, so it's a safe ceiling -- but I was hoping that there was something more elegant. For example, could I put an array on each side of the => operator and have it iterate automagically? Or something else?

Replies are listed 'Best First'.
Re: Using LWP's "post" to send n fields into a form?
by fizbin (Chaplain) on Sep 10, 2005 at 09:07 UTC
    Assuming that the code you've given so far is correct, then this should do the iteration for you:
    $webpage = $browser->post($form_url, [ username => $username, (map {widget => $widget{$_}} (keys %widget)), created => '1126175051' ],Content_Type => 'form-data'));
    Although I find it a bit odd that widget is a hash. If perhaps widget is really an array, then the code you need is this:
    $webpage = $browser->post($form_url, [ username => $username, (map {widget => $_} @widget), created => '1126175051' ],Content_Type => 'form-data'));
    To debug this, I would strongly recommend doing something like this in your code:
    my $lwparg = [ username => $username, (map {widget => $widget{$_}} (keys %widget)), created => '1126175051' ]; use Data::Dumper; print Data::Dumper->Dump([$lwparg],['lwparg']); $webpage = $browser->post($form_url, $lwparg, Content_Type => 'form-da +ta'));
    Then you can test out variants of lwparg until you get one that's passing the right stuff.
    --
    @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
      Thank you so much! You were right -- I was using an array, not a hash. And your code worked perfectly. -Tony
Re: Using LWP's "post" to send n fields into a form?
by davidrw (Prior) on Sep 10, 2005 at 13:29 UTC
    Consider using WWW::Mechanize to wrap up the LWP calls for you:
    use WWW::Mechanize; my $mech = WWW::Mechanize->new; $mech->get($url_of_form); # NOT necessarily the target of the + post # -- this is page w/the actual fo +rm elements $mech->select('widget', \@values); # assumes you get the data from the + CSV into @values # see [fizbin]'s replay above for c +omments on %widget $mech->submit_form( form_name => 'name_of_form', # or use the form_number param # form_number => 3, fields => { # simply set the other form values +here: username => $username, created => '1126175051', } );
    (Note you might be able to just put widget => \@values in the fields hash, too, but i didn't test it and doesn't explicitly say one way or the other in the docs)