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 && $sub !~ /^CORE::GLOBAL::/) {
103 # Stray user subroutine
104 die "$sub is not a Perl subroutine"
105 } else { # CORE subroutine
106 $proto = eval { prototype "CORE::$name" };
107 die "$name is neither a builtin, nor a Perl subroutine"
109 die "Cannot make a non-overridable builtin fatal"
110 if not defined $proto;
112 $call = "CORE::$name";
114 if (defined $proto) {
115 $real_proto = " ($proto)";
122 local(\$", \$!) = (', ', 0);
124 my @protos = fill_protos($proto);
125 $code .= write_invocation($core, $call, $name, $void, @protos);
127 print $code if $Debug;
129 no strict 'refs'; # to avoid: Can't use string (...) as a symbol ref ...
130 $code = eval("package $pkg; use Carp; $code");
132 no warnings; # to avoid: Subroutine foo redefined ...
143 Fatal - replace functions with equivalents which succeed or die
147 use Fatal qw(open close);
150 import Fatal 'juggle';
154 C<Fatal> provides a way to conveniently replace functions which normally
155 return a false value when they fail with equivalents which raise exceptions
156 if they are not successful. This lets you use these functions without
157 having to test their return values explicitly on each call. Exceptions
158 can be caught using C<eval{}>. See L<perlfunc> and L<perlvar> for details.
160 The do-or-die equivalents are set up simply by calling Fatal's
161 C<import> routine, passing it the names of the functions to be
162 replaced. You may wrap both user-defined functions and overridable
163 CORE operators (except C<exec>, C<system> which cannot be expressed
164 via prototypes) in this way.
166 If the symbol C<:void> appears in the import list, then functions
167 named later in that import list raise an exception only when
168 these are called in void context--that is, when their return
169 values are ignored. For example
171 use Fatal qw/:void open close/;
173 # properly checked, so no exception raised on error
174 if(open(FH, "< /bogotic") {
175 warn "bogo file, dude: $!";
178 # not checked, so error raises an exception
183 You should not fatalize functions that are called in list context, because this
184 module tests whether a function has failed by testing the boolean truth of its
185 return value in scalar context.
191 Prototype updates by Ilya Zakharevich <ilya@math.ohio-state.edu>.