ETLp/Utility/Command.pm untested

filter: Statementstmt 0.0% 0 / 27 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 4 Podpod n/a totaltotal 0.0% 0 / 44 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package ETLp::Utility::Command;
2  
3 use MooseX::Declare;
4  
5 class ETLp::Utility::Command {
6 use Data::Dumper;
7 use IPC::Cmd;
8 use Exception::Class ('CommandError');
9
10 has 'errstr' => (is => 'rw', isa => 'Str', required => 0, default => '');
11 has 'time_out' => (is => 'rw', isa => 'Int', required => 0, default => 0);
12  
13  
14 =head1 NAME
15  
16 ETLp::Utility::Command - Runs Operating System Commands
17  
18 =head1 SYNOPSIS
19  
20 use FindBin qw($Bin);
21 use ETLp::Utility::Command;
22
23 my $os = ETLp::Utility::Command->new();
24
25 my ($output, $retcode) = $os->run('ls -l');
26
27 if ($retcode) {
28 printf("Error %s\n", $os->errstr);
29 printf("Outout %s\n", $output);
30 }
31  
32 =head1 METHODS
33  
34 =head2 new
35  
36 Create a new ETLp::Utility::Command object.
37  
38 Parameters
39  
40 * time_out. optional. How long the command should run before aborting
41
42 =head2 run
43  
44 Executes the supplied comand
45  
46 =head3 Parameters
47  
48 * An Operating System command (as a string);
49
50 =head3 Returns
51  
52 * In a scalar context
53 o The command's return code
54
55 * In an array context
56 o The output of the command
57 o The command's return code
58
59 =head2 errstr
60  
61 Returns any error from the last run command
62
63 Paramaters
64  
65 * None
66
67 Returns
68  
69 * An error message (if there is one)
70
71 =cut
72  
73 method run (Str $command) {
74
75 $self->errstr('');
76
77 my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
78 IPC::Cmd::run(
79 command => $command,
80 timeout => $self->time_out
81 );
82
83 if ($error_code && $error_code =~ / exited with value (\d+)/) {
84 $error_code = $1;
85 if ($^O eq "MSWin32") {
86 $error_code = $error_code >> 8;
87 }
88 }
89
90 unless ($success) {
91 $self->errstr(join("", @$stderr_buf));
92 }
93
94 if (wantarray) {
95 return (join("", @$stdout_buf), $error_code);
96 } else {
97 return $error_code;
98 }
99 }
100 }
101  
102 =head1 LICENSE AND COPYRIGHT
103  
104 Copyright 2010 Redbone Systems Ltd
105  
106 This program is free software; you can redistribute it and/or modify it
107 under the terms of either: the GNU General Public License as published
108 by the Free Software Foundation; or the Artistic License.
109  
110 The terms are in the LICENSE file that accompanies this application
111  
112 1;
113  
114