File Coverage

blib/lib/System/Explain.pm
Criterion Covered Total %
statement 31 40 77.5
branch 8 32 25.0
condition 7 15 46.6
subroutine 7 8 87.5
pod 0 3 0.0
total 53 98 54.0


line stmt bran cond sub pod time code
1             package System::Explain;
2 2     2   119041 use 5.008001;
  2         16  
3 2     2   8 use strict;
  2         3  
  2         33  
4 2     2   7 use warnings;
  2         2  
  2         66  
5              
6             our $VERSION = "0.01";
7              
8             # Copyright 1999-2012 by Paul Johnson (paul@pjcj.net)
9              
10             # documentation at __END__
11              
12             # Original author: Paul Johnson
13             # Created: Fri 12 Mar 1999 10:25:51 am
14              
15 2     2   372 use parent 'Exporter';
  2         307  
  2         11  
16              
17             our @EXPORT = qw(sys dsys);
18              
19             my $Command = 0;
20             my $Errors = 0;
21             my $Verbose = 0;
22              
23             sub import
24             {
25 2     2   15 my $class = shift;
26 2         5 my $args = "@_";
27 2         6 $Command = $args =~ /\bcommand\b/i;
28 2         3 $Errors = $args =~ /\berror\b/i;
29 2         4 $Verbose = $args =~ /\bverbose\b/i;
30 2   66     16 $Command ||= $Verbose;
31 2   66     8 $Errors ||= $Verbose;
32 2 100       75 $class->export_to_level(1, "sys" ) if $args =~ /\bsys\b/i;
33 2 50       1282 $class->export_to_level(1, "dsys") if $args =~ /\bdsys\b/i;
34             }
35              
36             sub sys
37             {
38 1     1 0 1372 my (@command) = @_;
39 1         5 local $| = 1;
40 1 50       40 print "@command" if $Command;
41 1         14457 my $rc = 0xffff & system @command;
42 1 50 33     91 print "\n" if $Command && !$rc && !$Verbose;
      33        
43 1         79 ret($rc);
44             }
45              
46             sub dsys
47             {
48 0 0   0 0 0 die "@_ failed" if sys @_;
49             }
50              
51             sub ret
52             {
53 1     1 0 8 my ($rc) = @_;
54 1 50 33     16 printf " returned %#04x: ", $rc if $Errors && $rc;
55 1 50       4 if ($rc == 0)
    0          
    0          
56             {
57 1 50       62 print "ran with normal exit\n" if $Verbose;
58             }
59             elsif ($rc == 0xff00)
60             {
61 0 0       0 print "command failed: $!\n" if $Errors;
62             }
63             elsif ($rc > 0x80)
64             {
65 0         0 $rc >>= 8;
66 0 0       0 print "ran with non-zero exit status $rc\n" if $Errors;
67             }
68             else
69             {
70 0 0       0 print "ran with " if $Errors;
71 0 0       0 if ($rc & 0x80)
72             {
73 0         0 $rc &= ~0x80;
74 0 0       0 print "coredump from " if $Errors;
75             }
76 0 0       0 print "signal $rc\n" if $Errors;
77             }
78 1         67 $rc;
79             }
80              
81             1;
82             __END__