| 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
MC/DC50%1/2
$Command ||= $Verbose
$Command
$Verbose
Truth table66%2/3
A ||= B
A= $CommandB= $Verbose
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | ▶ | $Errors ||= $Verbose; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$Errors ||= $Verbose
MC/DC50%1/2
$Errors ||= $Verbose
$Errors
$Verbose
Truth table66%2/3
A ||= B
A= $ErrorsB= $Verbose
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | $class->export_to_level(1, "sys") if $args =~ /\bsys\b/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | $class->export_to_level(1, "dsys") if $args =~ /\bdsys\b/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | my $rc = 0xffff & system @command; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | ▶ | print "\n" if $Command && !$rc && !$Verbose; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$Command and not $rc and not $Verbose
$Command and not $rc
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$Errors and $rc
MC/DC50%1/2
$Errors and $rc
$Errors
$rc
Truth table66%2/3
A and B
A= $ErrorsB= $rc
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | ▶ | if ($rc == 0) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | ▶ | print "ran with normal exit\n" if $Verbose; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | elsif ($rc == 0xff00) | ||||||||||||||||||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | print "command failed: $!\n" if $Errors; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | else | ||||||||||||||||||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | print "ran with " if $Errors; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if ($rc & 0x80) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $rc &= ~0x80; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | print "coredump from " if $Errors; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | print "signal $rc\n" if $Errors; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 2 | return $rc; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 126 | __END__ | ||||||||||||||||||||||||||||||||||||||||||