X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FFatal.pm;h=0ea5963df0b0eb6f6941dee81f67b0bb69ef33df;hb=57c7bc0807db52907ce13229410345ba8f983f8a;hp=5b832f6427889bbc78ce8638b631baca6c26bd1a;hpb=cb50131aab68ac6dda048612c6e853b8cb08701e;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/Fatal.pm b/lib/Fatal.pm index 5b832f6..0ea5963 100644 --- a/lib/Fatal.pm +++ b/lib/Fatal.pm @@ -1,20 +1,26 @@ package Fatal; -use 5.005_64; +use 5.006_001; use Carp; use strict; our($AUTOLOAD, $Debug, $VERSION); -$VERSION = 1.02; +$VERSION = 1.03; $Debug = 0 unless defined $Debug; sub import { my $self = shift(@_); my($sym, $pkg); + my $void = 0; $pkg = (caller)[0]; foreach $sym (@_) { - &_make_fatal($sym, $pkg); + if ($sym eq ":void") { + $void = 1; + } + else { + &_make_fatal($sym, $pkg, $void); + } } }; @@ -42,11 +48,11 @@ sub fill_protos { } sub write_invocation { - my ($core, $call, $name, @argvs) = @_; + my ($core, $call, $name, $void, @argvs) = @_; if (@argvs == 1) { # No optional arguments my @argv = @{$argvs[0]}; shift @argv; - return "\t" . one_invocation($core, $call, $name, @argv) . ";\n"; + return "\t" . one_invocation($core, $call, $name, $void, @argv) . ";\n"; } else { my $else = "\t"; my (@out, @argv, $n); @@ -56,7 +62,7 @@ sub write_invocation { push @out, "$ {else}if (\@_ == $n) {\n"; $else = "\t} els"; push @out, - "\t\treturn " . one_invocation($core, $call, $name, @argv) . ";\n"; + "\t\treturn " . one_invocation($core, $call, $name, $void, @argv) . ";\n"; } push @out, < provides a way to conveniently replace functions which normally -return a false value when they fail with equivalents which halt execution +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. 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. +having to test their return values explicitly on each call. Exceptions +can be caught using C. See L and L for details. The do-or-die equivalents are set up simply by calling Fatal's C routine, passing it the names of the functions to be @@ -151,6 +162,21 @@ replaced. You may wrap both user-defined functions and overridable CORE operators (except C, C 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 + close FH; + =head1 AUTHOR Lionel.Cons@cern.ch