our($AUTOLOAD, $Debug, $VERSION);
$Debug = 0 unless defined $Debug;
&_make_fatal
($sym, $pkg, $void);
&_make_fatal
($cmd, (caller)[0]);
my ($n, $isref, @out, @out1, $seen_semi) = -1;
push(@out1,[$n,@out]) if $seen_semi;
push(@out, $1 . "{\$_[$n]}"), next if $proto =~ s/^\s*\\([\@%\$\&])//;
push(@out, "\$_[$n]"), next if $proto =~ s/^\s*([*\$&])//;
push(@out, "\@_[$n..\$#_]"), last if $proto =~ s/^\s*(;\s*)?\@//;
$seen_semi = 1, $n--, next if $proto =~ s/^\s*;//; # XXXX ????
die "Unknown prototype letters: \"$proto\"";
my ($core, $call, $name, $void, @argvs) = @_;
if (@argvs == 1) { # No optional arguments
return "\t" . one_invocation
($core, $call, $name, $void, @argv) . ";\n";
push @out, "$ {else}if (\@_ == $n) {\n";
"\t\treturn " . one_invocation
($core, $call, $name, $void, @argv) . ";\n";
die "$name(\@_): Do not expect to get ", scalar \@_, " arguments";
my ($core, $call, $name, $void, @argv) = @_;
return qq/(defined wantarray)?$call(@argv):
$call(@argv) || croak "Can
't $name(\@_)/ .
($core ? ': $!' : ', \
$! is
\"$!\"') . '"'
return qq{$call(@argv) || croak "Can
't $name(\@_)} .
($core ? ': $!' : ', \
$! is
\"$!\"') . '"';
my($sub, $pkg, $void) = @_;
my($name, $code, $sref, $real_proto, $proto, $core, $call);
$sub = "${pkg
}::$sub" unless $sub =~ /::/;
$name =~ s/.*::// or $name =~ s/^&//;
print "# _make_fatal: sub=$sub pkg=$pkg name=$name void=$void\n" if $Debug;
croak
"Bad subroutine name for Fatal: $name" unless $name =~ /^\w+$/;
if (defined(&$sub)) { # user subroutine
$proto = prototype $sref;
} elsif ($sub eq $ini) { # Stray user subroutine
die "$sub is not a Perl subroutine"
} else { # CORE subroutine
$proto = eval { prototype "CORE::$name" };
die "$name is neither a builtin, nor a Perl subroutine"
die "Cannot make a non-overridable builtin fatal"
$real_proto = " ($proto)";
local(\$", \$!) = (', ', 0);
my @protos = fill_protos
($proto);
$code .= write_invocation
($core, $call, $name, $void, @protos);
no strict
'refs'; # to avoid: Can't use string (...) as a symbol ref ...
$code = eval("package $pkg; use Carp; $code");
no warnings
; # to avoid: Subroutine foo redefined ...
Fatal - replace functions with equivalents which succeed or die
use Fatal qw(open close);
C<Fatal> provides a way to conveniently replace functions which normally
return a false value when they fail with equivalents which raise exceptions
if they are not successful. This lets you use these functions without
having to test their return values explicitly on each call. Exceptions
can be caught using C<eval{}>. See L<perlfunc> and L<perlvar> for details.
The do-or-die equivalents are set up simply by calling Fatal's
C<import> routine, passing it the names of the functions to be
replaced. You may wrap both user-defined functions and overridable
CORE operators (except C<exec>, C<system> which cannot be expressed
via prototypes) in this way.
If the symbol C<:void> appears in the import list, then functions
named later in that import list raise an exception only when
these are called in void context--that is, when their return
values are ignored. For example
use Fatal qw/:void open close/;
# properly checked, so no exception raised on error
if(open(FH, "< /bogotic") {
warn "bogo file, dude: $!";
# not checked, so error raises an exception
prototype updates by Ilya Zakharevich ilya@math.ohio-state.edu