From: Perl 5 Porters Date: Wed, 15 May 1996 06:46:05 +0000 (+0000) Subject: Add Fatal library module X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=902662995003194c4ae9313f14d74b42cf7e4963;hp=93d3b392e52f4dbaeb643dd62c1be55b27ef77d4;p=p5sagit%2Fp5-mst-13.2.git Add Fatal library module --- diff --git a/lib/Fatal.pm b/lib/Fatal.pm new file mode 100644 index 0000000..0d9c51b --- /dev/null +++ b/lib/Fatal.pm @@ -0,0 +1,82 @@ +package Fatal; + +use Carp; +use strict; +use vars qw( $AUTOLOAD $Debug ); + +$Debug = 0; + +sub import { + my $self = shift(@_); + my($sym, $pkg); + $pkg = (caller)[0]; + foreach $sym (@_) { + &_make_fatal($sym, $pkg); + } +}; + +sub AUTOLOAD { + my $cmd = $AUTOLOAD; + $cmd =~ s/.*:://; + &_make_fatal($cmd, (caller)[0]); + goto &$AUTOLOAD; +} + +sub _make_fatal { + my($sub, $pkg) = @_; + my($name, $code, $sref); + + $sub = "${pkg}::$sub" unless $sub =~ /::/; + $name = $sub; + $name =~ s/.*::// or $name =~ s/^&//; + print "# _make_fatal: sub=$sub pkg=$pkg name=$name\n" if $Debug; + croak "Bad subroutine name for Fatal: $name" unless $name =~ /^\w+$/; + $code = "sub $name {\n\tlocal(\$\", \$!) = (', ', 0);\n"; + if (defined(&$sub)) { + # user subroutine + $sref = \&$sub; + $code .= "\t&\$sref"; + } else { + # CORE subroutine + $code .= "\tCORE::$name"; + } + $code .= "\(\@_\) || croak \"Can't $name\(\@_\): \$!\";\n}\n"; + print $code if $Debug; + eval($code); + die($@) if $@; + local($^W) = 0; # to avoid: Subroutine foo redefined ... + no strict 'refs'; # to avoid: Can't use string (...) as a symbol ref ... + *{$sub} = \&{"Fatal::$name"}; +} + +1; + +__END__ + +=head1 NAME + +Fatal - replace functions with equivalents which succeed or die + +=head1 SYNOPSIS + + use Fatal qw(open print close); + + sub juggle { . . . } + import Fatal 'juggle'; + +=head1 DESCRIPTION + +C provides a way to conveniently replace functions which normally +return a false value when they fail with equivalents which halt execution +if they are not successful. This lets you use these functions without +having to test their return values explicitly on each call. Errors are +reported via C, so you can trap them using C<$SIG{__DIE__}> if you +wish to take some action before the program exits. + +The do-or-die equivalents are set up simply by calling Fatal's C +routine, passing it the names of the functions to be replaced. You may +wrap both user-defined functions and CORE operators in this way. + +=head1 AUTHOR + +Lionel.Cons@cern.ch