Fatal

The Fatal module makes functions fail more spectacularly. It replaces functions that normally return false upon failure with wrappers that raise an exception if the real function returns false. That way you can safely use these functions without testing their return values explicitly on each call.

Both user-defined functions and built-in functions may be wrapped, except for those built-ins that cannot be expressed via prototypes. Attempting to override a nonoverridable built-in raises an exception. These include system, print, printf, exec, split, grep, and map—or more generally, any FUNC for which prototype "CORE::FUNC" returns false, including the prototype function itself.

If the symbol :void appears in the import list, functions named later in the list are limited to raising an exception when the function is called in void context—that is, when the return value is ignored. (Be careful about the last statement in a subroutine.) For example:

use Fatal qw(:void open close);

# open properly checked, so no exception is raised on failure.
if (open(FH, "< /nonesuch") { warn "no /nonesuch: $!"; }

# close not properly checked, so failure raises an exception.
close FH;

Get Programming Perl, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.