File Coverage

blib/lib/Devel/System.pm
Criterion Covered Total %
statement 18 18 100.0
branch 6 6 100.0
condition n/a
subroutine 5 5 100.0
pod n/a
total 29 29 100.0


line stmt bran cond sub pod time code
1 1     1   47108 use strict;
  1         2  
  1         46  
2             package Devel::System;
3 1     1   715 use String::ShellQuote qw( shell_quote );
  1         818  
  1         74  
4 1     1   7 use Carp qw( croak );
  1         7  
  1         288  
5             our $VERSION = '0.01';
6              
7             =head1 NAME
8              
9             Devel::System - intercept calls to C to add extra diagnostics
10              
11             =head1 SYNOPSIS
12              
13             use Devel::System;
14             $Devel::System::dry_run = 1; # don't really do it
15              
16             system qw( rm -rf / );
17              
18             or from the command line:
19              
20             perl -MDevel::System=dry_run -e'system qw( rm -rf / )'
21              
22             =head1 DESCRIPTION
23              
24             Devel::System hooks the system builtin to add diagnostic output about
25             what system calls are being made. It's like the -x switch for /bin/sh
26             all over again.
27              
28             =head2 Variables
29              
30             The behaviour of the substitued C builtin can be swayed by the
31             following package variables in the C namespace
32              
33             =over
34              
35             =item $dry_run
36              
37             Don't actually perform the command. Always returns $return
38              
39             =cut
40              
41             our $dry_run;
42              
43              
44             =item $return
45              
46             The return value to use when $dry_run is active. Defaults to 0
47              
48             =cut
49              
50             our $return = 0;
51              
52              
53             =item $fh
54              
55             The filehandle to print the diagnostics to. Defaults to \*STDERR
56              
57             =back
58              
59             =cut
60              
61             our $fh = \*STDERR;
62              
63             *CORE::GLOBAL::system = sub {
64 5 100   5   1487 print $fh "+ ", @_ > 1 ? shell_quote(@_) : @_, "\n";
65 5 100       127 return $return if $dry_run;
66              
67 1         11759 return CORE::system @_;
68             };
69              
70              
71             =head2 Options
72              
73             In addition there are the following import symbols that you can use to
74             set options from the commands line.
75              
76             =over
77              
78             =item dry_run
79              
80             Sets $dry_run to a true value.
81              
82             =back
83              
84             =cut
85              
86              
87             sub import {
88 3     3   16 my $class = shift;
89 3         20 for (@_) {
90 2 100       24 /^dry_run$/ and do { $dry_run = 1; next };
  1         4  
  1         8  
91 1         265 croak "unknown option '$_'";
92             }
93             }
94              
95             1;
96             __END__