On 2014-03-28 21:56, Andy Bach wrote:

On Fri, Mar 28, 2014 at 3:05 PM, Dr.Ruud <rvtol+use...@isolution.nl
<mailto:rvtol+use...@isolution.nl>> wrote:

        if ($@) {
              die "$@";
        }


    Never test $@, only use the value of $@ after a failed eval.


Why not?

Because $@ is a global variable, which can (AKA will) get changed between the failure and your test.

So the answer is: Why gamble if you don't need to?


  my $obj;
  eval {
      $obj = get_obj();
      1;  # success
  }
  or do {
      my $eval_error = $@ || "Zombie Error";
      warn "WARNING: ", $eval_error;
      ...;
  };


Recent Perls localize $@ more, so the odds of getting into trouble with code that is testing $@, have become smaller over the years. Which is good because there is plenty of code around that still does it.

--
Ruud


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to