4
\$\begingroup\$
eval {
    # here is put some code that may throw exception
    1;  # Why is the "1;" here ?
} or do {
   my $error = $@;
   # Handle error.
};
  1. Does the following style protect against $@ not being set correctly ?
  2. What does the following "1;" protect against?
  3. Is the "or do{" better than the saying "if ($@){ " ? If so, why?
\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

Ad. 1. Yes and no - there are still some pitfalls (please read further).

Ad. 2. You must remember about returning truthy value from eval block because it evaluates to last value in block unless some code died, in which case undef is returned, so 1; as last statement in eval block is important.

You also have to localize $@ somehow (my $error = $@ in your example) because it's a global variable which can be changed silently (see Conclusion).

Ad. 3. if ($@) can have one pitfall, i.e. it doesn't catch error when it's false:

eval {
  die '';
  do_something();
};
if ($@) {
  # ooops... we don't get here
}

while or do { } block will always be activated in case of exception

eval {
  die '';
  do_something();
  1;
} or do {
  # caught!
};

Conclusion:

I personally use "eval or do # process error" style of catching errors but common and widely used alternative, which eleminates boilerplate code, is to use Try::Tiny. It's a simple, 0-dependency CPAN module which addresses $@ issues. Its documentation (especially BACKGROUND section) is really worth reading as it answers some of your questions and with even more details.

See also this SO question.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Great documented answer. I don't know Perl, but enjoyed reading it. :) \$\endgroup\$ Commented May 23, 2013 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.