blib/lib/System/Explain.pm

filter: Statementstmt 86.9% 40 / 46 Branchbran 46.8% 15 / 32 Conditioncond 66.6% 10 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 67.8% 76 / 112 CC 22 SCAR 30.3
CC
22
Cov
70%
_print_explanation_of
41.5
sys
5.4
import
5.0
CRAP 20.7
↑ Summary
1 # Original author: Paul Johnson
2 # Created: Fri 12 Mar 1999 10:25:51 am
3  
4 package System::Explain;
5 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = "0.02";
10  
11 =encoding utf-8
12  
13 =head1 NAME
14  
15 System::Explain - run a system command and explain the result
16  
17 =head1 SYNOPSIS
18  
19 use System::Explain "command, verbose, errors";
20 sys qw(ls -al);
21  
22 The C<sys> function runs a system command, checks the result, and comments on
23 it to STDOUT.
24  
25 =head1 DESCRIPTION
26  
27 System::Explain is a standalone release of L<System>, part of L<Gedcom>
28 v1.20 and earlier.
29  
30 =head1 FUNCTIONS
31  
32 =cut
33  
34 2 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 2 our @EXPORT = qw(sys dsys);
37  
38 2 my $Command = 0;
39 2 my $Errors = 0;
40 2 my $Verbose = 0;
41  
42 =head1 import
43  
44 Say C<use System::Explain "list, of, options"> to use this module.
45 The options are: C<command> (to print the command before running it),
46 C<error> (to report on the exit status), and C<verbose> (to do both of those).
47  
48 =cut
49  
50 sub import
51 2 ▶{
Subroutine import: called CC=5 SCAR=16.2
52 2 my $class = shift;
53 2 my $args = "@_";
54 2 $Command = $args =~ /\bcommand\b/i;
55 2 $Errors = $args =~ /\berror\b/i;
56 2 $Verbose = $args =~ /\bverbose\b/i;
57 2 ▶ $Command ||= $Verbose;
Condition66%2/3
$Command ||= $Verbose
l!l&&r!l&&!r
011
MC/DC50%1/2
$Command ||= $Verbose
$Command $Verbose
Truth table66%2/3
A ||= B
A= $CommandB= $Verbose
ABresult
1X1
011
000
58 2 ▶ $Errors ||= $Verbose;
Condition66%2/3
$Errors ||= $Verbose
l!l&&r!l&&!r
011
MC/DC50%1/2
$Errors ||= $Verbose
$Errors $Verbose
Truth table66%2/3
A ||= B
A= $ErrorsB= $Verbose
ABresult
1X1
011
000
59 2 ▶ $class->export_to_level(1, "sys") if $args =~ /\bsys\b/i;
Branch: 1 branch
BranchTrueFalseTotal
if $args =~ /\bsys\b/i 1 1 2
60 2 ▶ $class->export_to_level(1, "dsys") if $args =~ /\bdsys\b/i;
Branch: 1 branch
BranchTrueFalseTotal
if $args =~ /\bdsys\b/i 1 1 2
61 }
62  
63 =head1 sys
64  
65 C<sys(@command);> runs C<@command> (by passing C<@command> to C<system()>) and
66 optionally prints human-readable information about the result (specifically,
67 about the return value of C<system()>).
68  
69 Returns the return value of the C<system()> call.
70  
71 =cut
72  
73 sub sys
74 2 ▶{
Subroutine sys: called CC=5 SCAR=16.8
Pod undocumented
75 2 my (@command) = @_;
76 2 local $| = 1;
77 2 ▶ print "@command" if $Command;
Branch: 1 branch
BranchTrueFalseTotal
if $Command 2 0 2
78 2 my $rc = 0xffff & system @command;
79 2 ▶ print "\n" if $Command && !$rc && !$Verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $Command and not $rc and not $Verbose 0 2 2
Condition66%4/6
$Command and not $rc and not $Verbose
!ll&&!rl&&r
110
$Command and not $rc
!ll&&!rl&&r
011
MC/DC0%0/3
$Command and not $rc and not $Verbose
$Command not $rc not $Verbose
Truth table50%2/4
A and not(B) and not(C)
A= $CommandB= not $rcC= not $Verbose
ABCresult
0XX0
10X0
1100
1111
80 2 _print_explanation_of($rc);
81 }
82  
83 =head1 dsys
84  
85 As L</sys>, but dies if the C<system()> call fails.
86  
87 =cut
88  
89 sub dsys
90 1 ▶{
Subroutine dsys: called CC=2 SCAR=7.6
Pod undocumented
91 1 ▶ die "@_ failed" if sys @_;
Branch: 1 branch
BranchTrueFalseTotal
if sys(@_) 1 0 1
92 }
93  
94 # Print the explanation
95 sub _print_explanation_of
96 2 ▶{
Subroutine _print_explanation_of: called CC=13 SCAR=37.3
97 2 my ($rc) = @_;
98 2 ▶ printf " returned %#04x: ", $rc if $Errors && $rc;
Branch: 1 branch
BranchTrueFalseTotal
if $Errors and $rc 1 1 2
Condition66%2/3
$Errors and $rc
!ll&&!rl&&r
011
MC/DC50%1/2
$Errors and $rc
$Errors $rc
Truth table66%2/3
A and B
A= $ErrorsB= $rc
ABresult
0X0
100
111
99 2 ▶ if ($rc == 0)
Branch: 3 branches
BranchTrueFalseTotal
if ($rc == 0) { } 1 1 2
elsif ($rc == 65280) { } 0 1 1
elsif ($rc > 128) { } 1 0 1
100 {
101 1 ▶ print "ran with normal exit\n" if $Verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $Verbose 1 0 1
102 }
103 elsif ($rc == 0xff00)
104 {
105 0 ▶ print "command failed: $!\n" if $Errors;
Branch: 1 branch
BranchTrueFalseTotal
if $Errors 0 0 0
106 }
107 elsif ($rc > 0x80)
108 {
109 1 $rc >>= 8;
110 1 ▶ print "ran with non-zero exit status $rc\n" if $Errors;
Branch: 1 branch
BranchTrueFalseTotal
if $Errors 1 0 1
111 }
112 else
113 {
114 0 ▶ print "ran with " if $Errors;
Branch: 1 branch
BranchTrueFalseTotal
if $Errors 0 0 0
115 0 ▶ if ($rc & 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if ($rc & 128) 0 0 0
116 {
117 0 $rc &= ~0x80;
118 0 ▶ print "coredump from " if $Errors;
Branch: 1 branch
BranchTrueFalseTotal
if $Errors 0 0 0
119 }
120 0 ▶ print "signal $rc\n" if $Errors;
Branch: 1 branch
BranchTrueFalseTotal
if $Errors 0 0 0
121 }
122 2 return $rc;
123 }
124  
125 2 1;
126 __END__
↑ Summary