6 our($AUTOLOAD, $Debug, $VERSION);
10 $Debug = 0 unless defined $Debug;
18 if ($sym eq ":void") {
22 &_make_fatal($sym, $pkg, $void);
30 &_make_fatal($cmd, (caller)[0]);
36 my ($n, $isref, @out, @out1, $seen_semi) = -1;
37 while ($proto =~ /\S/) {
39 push(@out1,[$n,@out]) if $seen_semi;
40 push(@out, $1 . "{\$_[$n]}"), next if $proto =~ s/^\s*\\([\@%\$\&])//;
41 push(@out, "\$_[$n]"), next if $proto =~ s/^\s*([*\$&])//;
42 push(@out, "\@_[$n..\$#_]"), last if $proto =~ s/^\s*(;\s*)?\@//;
43 $seen_semi = 1, $n--, next if $proto =~ s/^\s*;//; # XXXX ????
44 die "Unknown prototype letters: \"$proto\"";
46 push(@out1,[$n+1,@out]);
50 sub write_invocation {
51 my ($core, $call, $name, $void, @argvs) = @_;
52 if (@argvs == 1) { # No optional arguments
53 my @argv = @{$argvs[0]};
55 return "\t" . one_invocation($core, $call, $name, $void, @argv) . ";\n";
60 @argv = @{shift @argvs};
62 push @out, "$ {else}if (\@_ == $n) {\n";
65 "\t\treturn " . one_invocation($core, $call, $name, $void, @argv) . ";\n";
69 die "$name(\@_): Do not expect to get ", scalar \@_, " arguments";
76 my ($core, $call, $name, $void, @argv) = @_;
79 return qq/(defined wantarray)?$call(@argv):
80 $call(@argv) || croak "Can't $name(\@_)/ .
81 ($core ? ': $!' : ', \$! is \"$!\"') . '"'
83 return qq{$call(@argv) || croak "Can't $name(\@_)} .
84 ($core ? ': $!' : ', \$! is \"$!\"') . '"';
89 my($sub, $pkg, $void) = @_;
90 my($name, $code, $sref, $real_proto, $proto, $core, $call);
93 $sub = "${pkg}::$sub" unless $sub =~ /::/;
95 $name =~ s/.*::// or $name =~ s/^&//;
96 print "# _make_fatal: sub=$sub pkg=$pkg name=$name void=$void\n" if $Debug;
97 croak "Bad subroutine name for Fatal: $name" unless $name =~ /^\w+$/;
98 if (defined(&$sub)) { # user subroutine
100 $proto = prototype $sref;
102 } elsif ($sub eq $ini) { # Stray user subroutine
103 die "$sub is not a Perl subroutine"
104 } else { # CORE subroutine
105 $proto = eval { prototype "CORE::$name" };
106 die "$name is neither a builtin, nor a Perl subroutine"
108 die "Cannot make a non-overridable builtin fatal"
109 if not defined $proto;
111 $call = "CORE::$name";
113 if (defined $proto) {
114 $real_proto = " ($proto)";
121 local(\$", \$!) = (', ', 0);
123 my @protos = fill_protos($proto);
124 $code .= write_invocation($core, $call, $name, $void, @protos);
126 print $code if $Debug;
128 no strict 'refs'; # to avoid: Can't use string (...) as a symbol ref ...
129 $code = eval("package $pkg; use Carp; $code");
131 no warnings; # to avoid: Subroutine foo redefined ...
142 Fatal - replace functions with equivalents which succeed or die
146 use Fatal qw(open close);
149 import Fatal 'juggle';
153 C<Fatal> provides a way to conveniently replace functions which normally
154 return a false value when they fail with equivalents which raise exceptions
155 if they are not successful. This lets you use these functions without
156 having to test their return values explicitly on each call. Exceptions
157 can be caught using C<eval{}>. See L<perlfunc> and L<perlvar> for details.
159 The do-or-die equivalents are set up simply by calling Fatal's
160 C<import> routine, passing it the names of the functions to be
161 replaced. You may wrap both user-defined functions and overridable
162 CORE operators (except C<exec>, C<system> which cannot be expressed
163 via prototypes) in this way.
165 If the symbol C<:void> appears in the import list, then functions
166 named later in that import list raise an exception only when
167 these are called in void context--that is, when their return
168 values are ignored. For example
170 use Fatal qw/:void open close/;
172 # properly checked, so no exception raised on error
173 if(open(FH, "< /bogotic") {
174 warn "bogo file, dude: $!";
177 # not checked, so error raises an exception
184 prototype updates by Ilya Zakharevich ilya@math.ohio-state.edu