blib/lib/Process/Status.pm

filter: Statementstmt 93.2% 55 / 59 Branchbran 79.1% 19 / 24 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 88.0% 22 / 25 Podpod 90.0% 9 / 10 totaltotal 87.6% 106 / 121 CC 15 SCAR 8.2
CC
15
Cov
88%
as_struct
4.0
__signal_name
4.0
assert_ok
3.1
CRAP 2.3
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Process::Status 0.010;
4 # ABSTRACT: a handle on process termination, like $?
5  
6 1 ▶use Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 #pod =head1 OVERVIEW
9 #pod
10 #pod When you run a system command with C<system> or C<qx``> or a number of other
11 #pod mechanisms, the process termination status gets put into C<$?> as an integer.
12 #pod In C, it's just an integer, and it stores a few pieces of data in different
13 #pod bits.
14 #pod
15 #pod Process::Status just provides a few simple methods to make it easier to
16 #pod inspect. It exists almost entirely to provide C<as_struct> and C<as_string>,
17 #pod which provide a simple decomposition of C<$?>.
18 #pod
19 #pod Methods called on C<Process::Status> without first calling a constructor will
20 #pod work on an implicitly-constructed object using the current value of C<$?>. To
21 #pod get an object for a specific value, you can call C<new> and pass an integer.
22 #pod You can also call C<new> with no arguments to get an object for the current
23 #pod value of C<$?>, if you want to keep that ugly variable out of your code.
24 #pod
25 #pod =method new
26 #pod
27 #pod my $ps = Process::Status->new( $status );
28 #pod my $ps = Process::Status->new; # acts as if you'd passed $?
29 #pod
30 #pod =cut
31  
32 19 ▶sub _self { ref $_[0] ? $_[0] : $_[0]->new($?); }
Subroutine _self: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 18 1 19
33  
34 4 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
35 4 ▶ my $status = defined $_[1] ? $_[1] : $?;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 4 0 4
36 4 ▶ return bless \$status, $_[0] if $status >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $status >= 0 2 2 4
37  
38 2 return bless [ $status, "$!", 0+$! ], 'Process::Status::Negative';
39 }
40  
41 #pod =method status_code
42 #pod
43 #pod This returns the value of the integer return value, as you might have found in
44 #pod C<$?>.
45 #pod
46 #pod =cut
47  
48 2 ▶sub status_code {
Subroutine status_code: called CC=1 SCAR=0.0
Pod documented
49 2 ${ $_[0]->_self }
50 }
51  
52 0 ▶sub pid_t {
Subroutine pid_t: not called CC=1 SCAR=6.9
Pod undocumented
53 # historical nonsense
54 0 ${ $_[0]->_self }
55 }
56  
57 #pod =method is_success
58 #pod
59 #pod This method returns true if the status code is zero.
60 #pod
61 #pod =cut
62  
63 2 ▶sub is_success { ${ $_[0]->_self } == 0 }
Subroutine is_success: called CC=1 SCAR=0.0
Pod documented
64  
65 #pod =method exitstatus
66 #pod
67 #pod This method returns the exit status encoded in the status. In other words,
68 #pod it's the number in the top eight bits.
69 #pod
70 #pod =cut
71  
72 2 ▶sub exitstatus { ${ $_[0]->_self } >> 8 }
Subroutine exitstatus: called CC=1 SCAR=0.0
Pod documented
73  
74 #pod =method signal
75 #pod
76 #pod This returns the signal caught by the process, or zero.
77 #pod
78 #pod =cut
79  
80 2 ▶sub signal { ${ $_[0]->_self } & 127 }
Subroutine signal: called CC=1 SCAR=0.0
Pod documented
81  
82 #pod =method cored
83 #pod
84 #pod This method returns true if the process dumped core.
85 #pod
86 #pod =cut
87  
88 2 ▶sub cored { !! (${ $_[0]->_self } & 128) }
Subroutine cored: called CC=1 SCAR=0.0
Pod documented
89  
90 #pod =method as_struct
91 #pod
92 #pod This method returns a hashref describing the status. Its exact contents may
93 #pod change over time; it is meant for human, not computer, consumption.
94 #pod
95 #pod =cut
96  
97 2 ▶sub as_struct {
Subroutine as_struct: called CC=4 SCAR=13.9
Pod documented
98 2 my $self = $_[0]->_self;
99  
100 2 my $rc = $self->status_code;
101  
102 return {
103 2 ▶ status_code => $rc,
Branch: 3 branches
BranchTrueFalseTotal
$rc == -1 ? : 0 2 2
$rc & 128 ? : 1 1 2
$rc & 127 ? : 1 1 2
104 ($rc == -1 ? () : (
105 exitstatus => $rc >> 8,
106 cored => ($rc & 128) ? 1 : 0,
107  
108 (($rc & 127) ? (signal => $rc & 127) : ())
109 )),
110 };
111 }
112  
113 1 my %SIGNAME;
114 2 ▶sub __signal_name {
Subroutine __signal_name: called CC=4 SCAR=13.9
115 2 my ($signal) = @_;
116 2 ▶ unless (%SIGNAME) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%SIGNAME) 1 1 2
117 1 my @names = split /\x20/, $Config::Config{sig_name};
118 1 $SIGNAME{$_} = "SIG$names[$_]" for (1 .. $#names);
119 }
120  
121 2 ▶ return($SIGNAME{ $signal } || "signal $signal");
Condition50%1/2
$SIGNAME{$signal} || "signal $signal"
l!l
20
MC/DC0%0/1
$SIGNAME{$signal} || "signal $signal"
$SIGNAME{$signal}
Truth table50%1/2
A
A= $SIGNAME{$signal}
Aresult
11
00
122 }
123  
124 #pod =method as_string
125 #pod
126 #pod This method returns a string describing the status. Its exact contents may
127 #pod change over time; it is meant for human, not computer, consumption.
128 #pod
129 #pod Roughly, you might get things like this:
130 #pod
131 #pod exited 0
132 #pod exited 92
133 #pod exited 2, caught SIGDERP
134 #pod exited 2, caught SIGSEGV; dumped core
135 #pod
136 #pod =cut
137  
138 4 ▶sub as_string {
Subroutine as_string: called CC=3 SCAR=11.0
Pod documented
139 4 my $self = $_[0]->_self;
140 4 my $rc = $$self;
141 4 my $str = "exited " . ($rc >> 8);
142 4 ▶ $str .= ", caught " . __signal_name($rc & 127) if $rc & 127;
Branch: 1 branch
BranchTrueFalseTotal
if $rc & 127 2 2 4
143 4 ▶ $str .= "; dumped core" if $rc & 128;
Branch: 1 branch
BranchTrueFalseTotal
if $rc & 128 2 2 4
144  
145 4 return $str;
146 }
147  
148 #pod =method assert_ok
149 #pod
150 #pod Process::Status->assert_ok($program_name);
151 #pod
152 #pod This method does nothing if C<$?> is 0. Otherwise, it croaks with a message
153 #pod like:
154 #pod
155 #pod your-program-name exited 13, caught SIGNES
156 #pod
157 #pod If a program name is not provided, "program" is used.
158 #pod
159 #pod =cut
160  
161 3 ▶sub assert_ok {
Subroutine assert_ok: called CC=3 SCAR=11.3
Pod documented
162 3 my $self = $_[0]->_self;
163 3 ▶ return if $self->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_success 0 3 3
164 3 ▶ my $name = @_ > 1 ? $_[1] : "program";
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 3 0 3
165  
166 3 require Carp;
167 3 Carp::croak("$name " . $self->as_string);
168 }
169  
170 {
171 package Process::Status::Negative 0.010;
172  
173 1 ▶ BEGIN { our @ISA = 'Process::Status' }
Subroutine BEGIN: called CC=1 SCAR=0.0
174 0 ▶ sub status_code { $_[0][0] }
Subroutine status_code: not called CC=1 SCAR=6.9
175 0 ▶ sub pid_t { $_[0][0] } # historical nonsense
Subroutine pid_t: not called CC=1 SCAR=6.9
176 1 ▶ sub is_success { return }
Subroutine is_success: called CC=1 SCAR=0.0
177 1 ▶ sub exitstatus { $_[0][0] }
Subroutine exitstatus: called CC=1 SCAR=0.0
178 1 ▶ sub signal { 0 }
Subroutine signal: called CC=1 SCAR=0.0
179 1 ▶ sub cored { return }
Subroutine cored: called CC=1 SCAR=0.0
180  
181 1 ▶ sub as_struct {
Subroutine as_struct: called CC=1 SCAR=0.0
182 return {
183 1 status_code => $_[0][0],
184 strerror => $_[0][1],
185 errno => $_[0][2],
186 }
187 }
188  
189 3 ▶ sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
190 3 qq{did not run; \$? was $_[0][0], \$! was "$_[0][1]" (errno $_[0][2])}
191 }
192  
193 1 ▶ sub assert_ok {
Subroutine assert_ok: called CC=2 SCAR=7.1
194 1 require Carp;
195 1 ▶ my $name = @_ > 1 ? $_[1] : "program";
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 1 0 1
196 1 Carp::croak("$name " . $_[0]->as_string);
197 }
198 }
199  
200 1 1;
201  
202 __END__
↑ Summary