in reply to Using Google APIs for Lattitude

try adding this

use JSON; my $json=new JSON; my $unjson; eval{ $unjson=$json->allow_nonref->decode($message);} ; warn 'json decode error:'.$@ ."\n" if ($@); my $lat = $unjson->{"location"}{"lat"};

Replies are listed 'Best First'.
Re^2: Using Google APIs for Lattitude
by stevieb (Canon) on Jul 03, 2017 at 21:27 UTC

    Nice huck, but I'd just like to give you a tidbit...

    $@ is an interesting variable. As soon as you go into an eval, you overwrite (clear) that var immediately. Worse, because of its nature, it could be set elsewhere even while you're in your eval call, rendering your use of it useless, or worse, confusing. For these reasons, base a warn or other activities on whether the eval caught a throw directly, not on $@ which could be set somewhere else, even far away, that's not related even remotely to your check:

    use JSON; my $json = new JSON; my $unjson; my $statement_ok = eval { $unjson = $json->allow_nonref->decode($message); 1; }; if (! $statement_ok){ # $@ can be interpolated ;) warn "json decode error: $@\n"; } my $lat = $unjson->{"location"}{"lat"};

    If eval catches an exception, the true (1;) will never be returned, rendering $statement_ok undef, allowing you act based on the result of the eval, not whether $@ is set or not. You could further it a bit (untested):

    my $ok = eval { foo(); 1; }; if (! $ok){ if ($@ =~ /internal error/){ warn "foo() fsck'd up!\n"; } else { warn "foo() fsck'd up, with unexpected err: $@\n"; } }

    Looking at Try::Tiny, it explains what I've said in detail. That module is a different approach to your excellent example.

Re^2: Using Google APIs for Lattitude
by mattpower (Initiate) on Jul 04, 2017 at 08:02 UTC

    Huck

    Spot on worked like a treat, the main goal here is to use my pi to locate my phone, and if its a certain distance from home switch off my heating. I already have my heating controlled by the weather and if my solar panels are generating, this will enable me to reach the next step.

    Many Thanks