in reply to Re^2: Can I catch a die from someone else's module?
in thread Can I catch a die from someone else's module?
Do I need the 1; ?
Generally, yes, you should include it. The reason is that eval returns undef on failure, which is what the or do { ... } is intended to react to. However, the or would also be triggered if the eval block were to return any other false value, such as 0 or the empty string. For example, the following incorrectly prints "Error from Module::That::Dies:". So unless you can be absolutely sure that the eval block always returns a true value, you should include the 1 (or any other true value) as the last statement of the block.
sub Module::That::Dies::die_here { return 0 } eval { Module::That::Dies::die_here(); #1; } or do { warn "Error from Module::That::Dies:\n$@\n"; };
Update: I tend to write it like the following, since the 1 on a line by itsself can look out of place and might be accidentally deleted:
eval { ... ;1 } or do { # catch ... }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Can I catch a die from someone else's module?
by stevieb (Canon) on Apr 19, 2022 at 19:29 UTC | |
by kcott (Archbishop) on Apr 20, 2022 at 00:47 UTC |