in reply to Re: Using Google APIs for Lattitude
in thread Using Google APIs for Lattitude

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.