in reply to When to use eval
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.
🦛
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: When to use eval
by Anonymous Monk on Feb 01, 2024 at 18:03 UTC | |
by hippo (Archbishop) on Feb 01, 2024 at 19:08 UTC | |
by stevieb (Canon) on Feb 01, 2024 at 18:43 UTC | |
by haj (Vicar) on Feb 01, 2024 at 19:30 UTC | |
by stevieb (Canon) on Feb 02, 2024 at 07:32 UTC |