summaryrefslogtreecommitdiff
path: root/lib/Fatal.pm
blob: 0d9c51b113c89ed62b316a49b495824c7ae33788 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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<Fatal> 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<die>, 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<import>
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