HiveBrain v1.2.0
Get Started
← Back to all entries
debugMinor

What changes, if any, are required to make the following Perl eval bullet proof?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thewhatperlarebulletanymakefollowingchangeseval

Problem

eval {
    # here is put some code that may throw exception
    1;  # Why is the "1;" here ?
} or do {
   my $error = $@;
   # Handle error.
};


  • Does the following style protect against $@ not being set


correctly ?

  • What does the following "1;" protect against?



  • Is the "or do{" better than the saying "if ($@){ " ? If so, why?

Solution

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.

Code Snippets

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

Context

StackExchange Code Review Q#26512, answer score: 6

Revisions (0)

No revisions yet.