blib/lib/Proc/WaitStat.pm

filter: Statementstmt 90.9% 40 / 44 Branchbran 65.0% 13 / 20 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 84.5% 71 / 84 CC 12 SCAR 12.6
CC
12
Cov
82%
waitstat
5.2
waitstat_reuse
5.2
close_die
5.1
CRAP 3.5
↑ Summary
1 # $Id: WaitStat.pm,v 1.3 1999-10-21 12:39:43-04 roderick Exp $
2 #
3 # Copyright (c) 1997 Roderick Schertler. All rights reserved. This
4 # program is free software; you can redistribute it and/or modify it
5 # under the same terms as Perl itself.
6  
7 =head1 NAME
8  
9 Proc::WaitStat - Interpret and act on wait() status values
10  
11 =head1 SYNOPSIS
12  
13 $description = waitstat $?;
14 exit waitstat_reuse $?;
15 waitstat_die $?, 'program-name';
16 close_die COMMAND, 'program-name';
17  
18 =head1 DESCRIPTION
19  
20 This module contains functions for interpreting and acting on wait
21 status values.
22  
23 Nothing is exported by default.
24  
25 =over
26  
27 =cut
28  
29 package Proc::WaitStat;
30  
31 1 ▶use 5.003_98; # piped close errno resetting
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use vars qw($VERSION @ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use IPC::Signal qw(sig_name);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use POSIX qw(:sys_wait_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 1 $VERSION = '1.00';
41 1 @ISA = qw(Exporter);
42 1 @EXPORT_OK = qw(waitstat waitstat_reuse waitstat_die close_die);
43  
44 =item B<waitstat> I<wait-status>
45  
46 Returns a string representation of wait() status value I<wait-status>.
47 Values returned are like C<"0"> and C<"64"> and C<"killed (SIGHUP)">.
48  
49 This function is prototyped to take a single scalar argument.
50  
51 =cut
52  
53 7 ▶sub waitstat ($) {
Subroutine waitstat: called CC=4 SCAR=16.4
Pod documented
54 7 my $status = shift;
55  
56 7 ▶ if (WIFEXITED $status) {
Branch: 3 branches
BranchTrueFalseTotal
if (WIFEXITED($status)) { } 5 2 7
elsif (WIFSIGNALED($status)) { } 2 0 2
elsif (WIFSTOPPED($status)) { } 0 0 0
57 5 WEXITSTATUS $status
58 }
59 elsif (WIFSIGNALED $status) {
60 # XXX WCOREDUMP
61 2 'killed (SIG' . sig_name(WTERMSIG $status) . ')'
62 }
63 elsif (WIFSTOPPED $status) {
64 0 'stopped (SIG' . sig_name(WSTOPSIG $status) . ')'
65 }
66 # XXX WIFCONTINUED
67 else {
68 0 "invalid wait status $status"
69 }
70 }
71  
72 =item B<waitstat_reuse> I<wait-status>
73  
74 Turn I<wait-status> into a value which can be passed to B<exit>, converted
75 in the same manner the shell uses. If I<wait-status> indicates a normal
76 exit, return the exit value. If I<wait-status> instead indicates death by
77 signal, return 128 plus the signal number.
78  
79 This function is prototyped to take a single scalar argument.
80  
81 =cut
82  
83 5 ▶sub waitstat_reuse ($) {
Subroutine waitstat_reuse: called CC=4 SCAR=16.4
Pod documented
84 5 my $status = shift;
85  
86 5 ▶ if (WIFEXITED $status) {
Branch: 3 branches
BranchTrueFalseTotal
if (WIFEXITED($status)) { } 4 1 5
elsif (WIFSIGNALED($status)) { } 1 0 1
elsif (WIFSTOPPED($status)) { } 0 0 0
87 4 WEXITSTATUS $status
88 }
89 elsif (WIFSIGNALED $status) {
90 1 128 + WTERMSIG $status
91 }
92 elsif (WIFSTOPPED $status) {
93 0 128 + WSTOPSIG $status
94 }
95 else {
96 0 croak "Invalid wait status $status";
97 }
98 }
99  
100 =item B<waitstat_die> I<wait-status> I<program-name>
101  
102 die() if I<wait-status> is non-zero (mentioning I<program-name> as the
103 source of the error).
104  
105 This function is prototyped to take two scalar arguments.
106  
107 =cut
108  
109 2 ▶sub waitstat_die ($$) {
Subroutine waitstat_die: called CC=2 SCAR=6.9
Pod documented
110 2 my ($status, $program) = @_;
111 2 ▶ croak "Non-zero exit (" . waitstat($status) .
Branch: 1 branch
BranchTrueFalseTotal
if $status 1 1 2
112 ") from $program"
113 if $status;
114 }
115  
116 =item B<close_die> I<filehandle> I<name>
117  
118 Close I<filehandle>, if that fails die() with an appropriate message
119 which refers to I<name>. This handles failed closings of both programs
120 and files properly.
121  
122 This function is prototyped to take a filehandle (actually, a glob ref)
123 and a scalar.
124  
125 =cut
126  
127 2 ▶sub close_die (*$) {
Subroutine close_die: called CC=5 SCAR=16.2
Pod documented
128 2 my ($fh, $name) = @_;
129  
130 2 ▶ unless (ref $fh || ref \$fh eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $fh or ref \$fh eq "GLOB") 1 1 2
Condition66%2/3
ref $fh or ref \$fh eq "GLOB"
l!l&&r!l&&!r
101
MC/DC50%1/2
ref $fh or ref \$fh eq "GLOB"
ref $fh ref \$fh eq "GLOB"
Truth table66%2/3
A or B
A= ref $fhB= ref \$fh eq "GLOB"
ABresult
1X1
011
000
131 1 require Symbol;
132 1 $fh = Symbol::qualify_to_ref($fh, caller);
133 }
134  
135 2 ▶ unless (close $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $fh) 1 1 2
136 1 ▶ croak "Error closing $name: ",
Branch: 1 branch
BranchTrueFalseTotal
$! + 0 ? : 0 1 1
137 $!+0 ? "$!" : 'non-zero exit (' . waitstat($?) . ')';
138 }
139 }
140  
141 1 1
142  
143 __END__
↑ Summary