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.