What does eval catch? A function that issues a die?
It allows the execution of code which may error (ie. die) without terminating the enclosing running instance.
$ perl -E 'eval { die "Ay, caramba!"; }; say "foo"' foo
For instance ,open , returns undef when failing but it doesn't kill the rest of the program due to the 'exception'.
It does if you use autodie; so you might use a block eval in that situation.
Note that there are cleaner ways to trap and check for exceptions. In a recent enough perl you can use try. For older perls there are modules such as TryCatch, Try::Tiny, etc.
🦛
In reply to Re: When to use eval
by hippo
in thread When to use eval
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |