odrevet has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I'd like to know when exaclty $@ get cleared
For example, it seems that WWW::Mechanize's reload function clear $@...
do{ eval{ $browser->get('http://www.example.org/?nod +e=whatever'); }; if($@){ print "WARNING: $@"; + #$browser->reload(); #would kill $@ } }while($@);

Replies are listed 'Best First'.
Re: when do $@ get cleared ?
by kennethk (Abbot) on Jul 14, 2010 at 17:38 UTC
    As described in Error Indicators in perlvar,
    on every eval() is always set on failure and cleared on success

    It gets cleared every time an eval block is run. It is certainly within WWW::Mechanize's rights to use an eval. If you need it to be persistent across a call to reload, you should be able to localize the variable (untested):

    do{ eval{ $browser->get('http://www.example.org/?node=whatever'); }; if($@){ print "WARNING: $@"; local $@; $browser->reload(); #kills if-level $@, but not do-level $@ } }while($@);
      for (;;) { eval { $browser->get('http://www.example.org/?node=whatever'); }; last if !$@; print "WARNING: $@"; $browser->reload(); }
      while (!eval { $browser->get('http://www.example.org/?node=whatever'); }) { print "WARNING: $@"; $browser->reload(); }