in reply to Re^3: How to make Geo::Coder::Google run even if input location doesn't exist
in thread How to make Geo::Coder::Google run even if input location doesn't exist

Still when using "eval" and "$@" I get: "Can't use an undefined value as an ARRAY reference at geoTest.pl line 32." I suppose this refers to " @{ $response->{Point}{coordinates} }". The code looks like this:

#!/usr/bin/perl -w use strict; use locale; use warnings; #use diagnostics; use utf8; binmode(STDIN, "encoding(utf8)"); binmode(STDOUT, "encoding(utf8)"); binmode(STDERR, "encoding(utf8)"); use Geo::Coder::Google; my @place = ('Seattle', 'France', 'CorseMétéo', 'New Delhi'); my ($long, $lat); foreach my $place(@place){ my $geocoder = Geo::Coder::Google->new(apikey => '{MyAPIkeyHere}') +; my $response; until (defined $@ || defined $response){ eval{ $response = $geocoder->geocode(location => $place); } } if ($@){ print "Couldn't get location\n"; } ($long, $lat) = @{ $response->{Point}{coordinates} }; print "$long\n"; print "$lat\n"; }
  • Comment on Re^4: How to make Geo::Coder::Google run even if input location doesn't exist
  • Download Code

Replies are listed 'Best First'.
Re^5: How to make Geo::Coder::Google run even if input location doesn't exist
by Corion (Patriarch) on Mar 01, 2013 at 09:04 UTC

    Maybe you shouldn't try to get longitude and latitude when you already know that you didn't get a valid response?

    I recommend the following structure:

    if ($@){ print "Couldn't get location\n"; } else { ... retrieve and print longitude, latitude }

      It seem to work now... It get's coordinates even for the unexisting location like 'CorseMétéo'. However it still gives an error : "Useless use of string in void context at geoTest.pl line 27.". The code :

      #!/usr/bin/perl -w use strict; use locale; use warnings; #use diagnostics; use utf8; binmode(STDIN, "encoding(utf8)"); binmode(STDOUT, "encoding(utf8)"); binmode(STDERR, "encoding(utf8)"); use Geo::Coder::Google; my @place = ('Seattle', 'France', 'CorseMétéo', 'New Delhi'); my ($long, $lat); foreach my $place(@place){ my $geocoder = Geo::Coder::Google->new(apikey => '{MyAPIkeyHere}') +; my $response; until (defined $response){ eval{ $response = $geocoder->geocode(location => $place); if ($@){ "Couldn't get location : $place\n"; }else{ ($long, $lat) = @{ $response->{Point}{coordinates} }; } } } print "$place\n"; print "$long\n"; print "$lat\n"; }

      Do you think it's a problem if I use the code even if it gives an error bu it works?

        In your code at line 27 I see:

        if ($@){ "Couldn't get location : $place\n"; ### LINE 27 }else{ ($long, $lat) = @{ $response->{Point}{coordinates} }; }

        What is the purpose of defining a nice error message string and then doing absolutely nothing with it? That's the "Useless use of..." something. Did you mean to print this string?

        Do you think it's a problem...

        Do you think it's a problem? The 'error' you cite is not an error per se, but a warning (see warnings module). Your code will still compile and run when this warning is being generated and you are free to ignore it if you wish, but it's usually a strong signal that something's wrong, as in this case: no  print() call.

        my $response; until (defined $response){ eval{ $response = $geocoder->geocode(location => $place); if ($@){ "Couldn't get location : $place\n"; }else{ ($long, $lat) = @{ $response->{Point}{coordinates} }; } } }

        Whoa. I just took a look at the broader picture, and this chunk of code seems highly suspect. First off, let me say that I'm completely unfamiliar with Geo::Coder::Google and the Google service(s) involved, but one should never let compete ignorance prevent one from offering advice.

        The primary problem I see is that the  $@ eval error variable is being evaluated within the eval block!

        The docs for  $@ say (emphases added):

        $EVAL_ERROR
        $@    The Perl syntax error message from the last "eval()" operator.
                  If $@ is the null string, the last "eval()" parsed and executed
                  correctly (although the operations you invoked may have failed
                  in the normal fashion).
                  ...

        It may be that the  geocode() method throws an exception (dies) and traps that exception, thus setting  $@ to a meaningful value; again, I'm not familiar with this module. If this is not the case, the test of  $@ in a given loop may actually be testing the value set on the previous iteration of the  until loop; however, I don't know if  eval() clears  $@ to a non-error value at the start of its execution.

        Either of these possibilities (or perhaps some other) may explain why you are still getting latitude and longitude coordinates for places that do not exist, at least insofar as Google is aware, which does not seem to be helpful behavior for your program and, yes, does seem to be a problem!

        Perhaps see the discussion of Error Variables in perlvar, likewise eval.