File Coverage

blib/lib/File/Coda.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             # -*- perl -*-
2             #
3             # File::Coda - a global destructor that closes stdout, with error-checking
4             #
5             # This package is intended to be "use"d very early on.
6             # It simply sets up actions to be executed at the end of execution.
7             #
8             # Why ``Coda''? Its definition strikes me as particularly apt:
9             #
10             # coda, n:
11             # A few measures added beyond the natural termination of a composition.
12             # --- 1913 Webster
13              
14             package File::Coda;
15              
16 1     1   5798 use strict;
  1         3  
  1         45  
17 1     1   6 use warnings;
  1         3  
  1         182  
18              
19             # Program name of our caller
20             our $ME = $0;
21             our $VERSION = '1.94';
22              
23             # Set $? to this value upon failure to close stdout.
24             our $Exit_status = 1;
25              
26             END {
27             # Nobody ever checks the status of print()s. That's okay, because
28             # if any do fail, we're usually[*] guaranteed to get an indicator
29             # when we close() the file handle.
30             # [*] Beware the exception, due to a long-standing bug in Perl,
31             # fixed in 5.9.1. See the report and patch here:
32             # http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2004-12/msg00072.html
33             #
34             # If stdout is already closed, we're done.
35             defined fileno STDOUT
36             or return;
37             # Close stdout now, and if that succeeds, simply return.
38             close STDOUT
39             and return;
40              
41             # Errors closing stdout. Indicate that, and hope stderr is OK.
42             warn $ME . ": closing standard output: $!\n";
43              
44             # Don't be so arrogant as to assume that we're the first END handler
45             # defined, and thus the last one invoked. There may be others yet
46             # to come. $? will be passed on to them, and to the final _exit().
47             #
48             # If it isn't already an error, make it one (and if it _is_ an error,
49             # preserve the value: it might be important).
50             $? ||= $Exit_status;
51             }
52              
53             1;
54              
55             __DATA__