in reply to Google Images via CGI

The most important paragraph of your post is the following:
This works in a regular perl script. But in my cgi, it fails. Putting the above code in a separate script which is called by the cgi, writes to file and allows for the cgi to read the file also fails.
Whenever you experience such a situation, it's time for print (or any other command which allows you to get debug output).

Add print STDERR lines to your program to find out what is happening where.

  • Is $google undef?
  • Is $key different?
  • What is in $hashref1 (Data::Dumper may be a good help for this)?
  • Is your program not looping for any reason?
  • If everything else fails, compare the environments (%ENV) as this is the biggest difference between a shell and a CGI situation
  • Your programm may also succeed, but the webserver cancels the request before any output gets through (usually after 5 min, but it's configurabel down to 1 sec.) If your last line print STDERR "Done\n"; isn't shown, this could be the reason

  • WebService::Simple doesn't provide a ->dump method as far as I looked at it, but using Data::Dumper's Dumper() function on it may be worth a try. Expect a huge output, but looking through it may get you an HTTP error message or something else useful.

    If everything fails and you don't get any new indeas, please add an exact description what you mean by "fails", including the error message, if any.

    Replies are listed 'Best First'.
    Re^2: Google Images via CGI
    by dime (Novice) on Sep 03, 2009 at 08:35 UTC

      My code is spiked with print statements after nearly every line, but I figured you wouldn't want to read them. $google is defined, $key is copied from the other program (which works) and is exactly identical, and $hashref1 never happens. The error message I get is

       request to http://ajax.googleapis.com/ajax/services/search/images?hl=ja&rsz=small&q=%E4%BA%BA&v=0.1&api_key=[my key]&start=0 failed at [my program] line 59

      for line 59 being

      my $hashref1 = $google->get( {v => "0.1", q => "$query", rsz => "small", hl => "ja", start => "$i"} )

      I get this error message after some two minutes or so of nothing happening, which is exactly how my programs reacted before to a forgotten proxy. That is why I assumed it was a firewall problem, again.

      %ENV is indeed very different - none of the keys seem to be the same. I don't know how that helps me, though. Please explain?

        That error message sucks because it doesn't say why it failed. Try turn on debug option, maybe you will get better message.

          Outing myself as a total novice: How do I debug a cgi? Beyond checking beforehand with perl -wc my_program.pl and sending any errors to a file with use CGI::Carp qw(carpout);, what options do I have? Is there a way to run a cgi with -d in the shell, even though the cgi needs user input to get to the problematic lines of code?

        Very often a
        print Dumper($google)."\n";
        helps. It usually contains the request and the reply.

        Not a Perl solution, but when debugging SOAP-crap very often

        tcpdump -nXs 8192 -c 1000 port 80 >dump.log
        also helps alot, because you get the raw data stream. You man need to be more specific with the filter conditions depending on the load of your computer.
          Not WebService::Simple , its special like that :)