1Command::Runner(3pm) User Contributed Perl Documentation Command::Runner(3pm)
2
3
4
6 Command::Runner - run external commands and Perl code refs
7
9 use Command::Runner;
10
11 my $cmd = Command::Runner->new(
12 command => ['ls', '-al'],
13 timeout => 10,
14 stdout => sub { warn "out: $_[0]\n" },
15 stderr => sub { warn "err: $_[0]\n" },
16 );
17 my $res = $cmd->run;
18
19 my $untar = Command::Runner->new;
20 $untar->commandf(
21 '%q -dc %q | %q tf -',
22 'C:\\Program Files (x86)\\GnuWin32\\bin\\gzip.EXE',
23 'File-ShareDir-Install-0.13.tar.gz'
24 'C:\\Program Files (x86)\\GnuWin32\\bin\\tar.EXE',
25 );
26 my $capture = $untar->run->{stdout};
27
29 Command::Runner runs external commands and Perl code refs
30
32 new
33 A constructor, which takes:
34
35 command
36 an array of external commands, a string of external programs, or a
37 Perl code ref. If an array of external commands is specified, it
38 is automatically quoted on Windows.
39
40 commandf
41 a command string by "sprintf"-like syntax. You can use positional
42 formatting together with a conversion %q (with quoting).
43
44 Here is an example:
45
46 my $cmd = Command::Runner->new(
47 commandf => [ '%q %q >> %q', '/path/to/cat', 'foo bar.txt', 'out.txt' ],
48 );
49
50 # or, you can set it separately
51 my $cmd = Command::Runner->new;
52 $cmd->commandf('%q %q >> %q', '/path/to/cat', 'foo bar.txt', 'out.txt');
53
54 timeout
55 timeout second. You can set float second.
56
57 redirect
58 if this is true, stderr redirects to stdout
59
60 keep
61 by default, even if stdout/stderr is consumed, it is preserved for
62 return value. You can disable this behavior by setting keep option
63 false.
64
65 stdout / stderr
66 a code ref that will be called whenever stdout/stderr is available
67
68 env set environment variables.
69
70 Command::Runner->new(..., env => \%env)->run
71
72 is equivalent to
73
74 {
75 local %ENV = %env;
76 Command::Runner->new(...)->run;
77 }
78
79 run
80 Run command. It returns a hash reference, which contains:
81
82 result
83 timeout
84 stdout
85 stderr
86 pid
87
89 I develop a CPAN client App::cpm, where I need to execute external
90 commands and Perl code refs with:
91
92 timeout
93 quoting
94 flexible logging
95
96 While App::cpanminus has excellent APIs for such use, I still needed to
97 tweak them in App::cpm.
98
99 So I ended up creating a seperate module, Command::Runner.
100
102 Shoichi Kaji <skaji@cpan.org>
103
105 Copyright 2017 Shoichi Kaji <skaji@cpan.org>
106
107 This library is free software; you can redistribute it and/or modify it
108 under the same terms as Perl itself.
109
110
111
112perl v5.30.1 2020-01-29 Command::Runner(3pm)