From: Gurusamy Sarathy Date: Tue, 7 Mar 2000 20:18:54 +0000 (+0000) Subject: support :void to enable croaking only in void context (from X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=91c7a8804c6d3adc7e73ca2956160b00f07dd6a2;p=p5sagit%2Fp5-mst-13.2.git support :void to enable croaking only in void context (from Simon Cozens ) p4raw-id: //depot/perl@5600 --- diff --git a/lib/Fatal.pm b/lib/Fatal.pm index d033d62..1496117 100644 --- a/lib/Fatal.pm +++ b/lib/Fatal.pm @@ -12,9 +12,15 @@ $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, <, 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 diff --git a/t/lib/fatal.t b/t/lib/fatal.t index 0192658..c17a0a2 100755 --- a/t/lib/fatal.t +++ b/t/lib/fatal.t @@ -3,11 +3,11 @@ BEGIN { chdir 't' if -d 't'; unshift @INC, '../lib'; - print "1..13\n"; + print "1..15\n"; } use strict; -use Fatal qw(open close); +use Fatal qw(open close :void opendir); my $i = 1; eval { open FOO, '