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

Hi, For some reason I can access the page and submit one value into the form (the only one on the page) and then save the content to a file, but when I try to loop over an array of values, I get a POST error..?
#!/usr/bin/perl use strict; use warnings; my @date; open(IN, "<c:/perl/scripts/dates/dates.txt"); while (<IN>) { push (@date, $_); } close(IN); use WWW::Mechanize; foreach my $date (@date) { my $mech = WWW::Mechanize->new(); $mech->get( "http://bub2.meteo.psu.edu/wxstn/wxstn.htm" ); $mech->form_number(1); $mech->field( 'dtg' , $date ); $mech->click(); $mech->content(); my $file = "c:/perl/scripts/dates/$date.txt"; $mech->save_content($file); $mech->back(); }

Replies are listed 'Best First'.
Re: WWW::Mechanize - can't submit each value in an array
by ikegami (Patriarch) on Jun 19, 2009 at 00:24 UTC

    Looks like a HTTP 500 error is being returned by web server. Not sure why, but I'm sure it doesn't help that you send a newline as part of the date. chomp!

    my @date; open(IN, "<c:/perl/scripts/dates/dates.txt"); while (<IN>) { push (@date, $_); } close(IN);

    should be

    open(my $in, '<', "c:/perl/scripts/dates/dates.txt") or die "open: $!\n"; my @date; while (<$in>) { chomp; push(@date, $_); } close($in);

    which can be written as

    open(my $in, '<', "c:/perl/scripts/dates/dates.txt") or die "open: $!\n"; chomp( my @date = <$in> ); close($in);
Re: WWW::Mechanize - can't submit each value in an array
by Anonymous Monk on Jun 18, 2009 at 23:38 UTC
    What error?
      Ah sorry.. here's the error returned:

      Uncaught exception from user code: Error POSTing http://bub2.met.psu.edu/cgi-win/WXDaily.EXE: Internal Erro r at C:\Perl\scripts\i.pl line 21 at C:/Perl/lib/WWW/Mechanize.pm line 2615 WWW::Mechanize::_die('Error ', 'POST', 'ing ', 'URI::http=SCALAR(0x237c6 3c)', ': ', 'Internal Error') called at C:/Perl/lib/WWW/Mechanize.pm line 2602 WWW::Mechanize::die('WWW::Mechanize=HASH(0x229b7a4)', 'Error ', 'POST', 'ing ', 'URI::http=SCALAR(0x237c63c)', ': ', 'Internal Error') called at C:/Perl /lib/WWW/Mechanize.pm line 2261 WWW::Mechanize::_update_page('WWW::Mechanize=HASH(0x229b7a4)', 'HTTP::Re quest=HASH(0x249be6c)', 'HTTP::Response=HASH(0x24a9ba4)') called at C:/Perl/lib/ WWW/Mechanize.pm line 2117 WWW::Mechanize::request('WWW::Mechanize=HASH(0x229b7a4)', 'HTTP::Request =HASH(0x249be6c)') called at C:/Perl/lib/WWW/Mechanize.pm line 1642 WWW::Mechanize::click('WWW::Mechanize=HASH(0x229b7a4)') called at C:\Per l\scripts\i.pl line 21

        What date?