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
20 Command::Runner runs external commands and Perl code refs
21
23 new
24 A constructor, which takes:
25
26 command
27 an array of external commands, a string of external programs, or a
28 Perl code ref. If an array of external commands is specified, it
29 is automatically quoted on Windows.
30
31 timeout
32 timeout second. You can set float second.
33
34 redirect
35 if this is true, stderr redirects to stdout
36
37 keep
38 by default, even if stdout/stderr is consumed, it is preserved for
39 return value. You can disable this behavior by setting keep option
40 false.
41
42 stdout / stderr
43 a code ref that will be called whenever stdout/stderr is available
44
45 env set environment variables.
46
47 Command::Runner->new(..., env => \%env)->run
48
49 is roughly equivalent to
50
51 {
52 local %ENV = %env;
53 Command::Runner->new(...)->run;
54 }
55
56 cwd set the current directory.
57
58 Command::Runner->new(..., cwd => $dir)->run
59
60 is roughly equivalent to
61
62 {
63 require File::pushd;
64 my $guard = File::pushd::pushd($dir);
65 Command::Runner->new(...)->run;
66 }
67
68 run
69 Run command. It returns a hash reference, which contains:
70
71 result
72 timeout
73 stdout
74 stderr
75 pid
76
78 I develop a CPAN client App::cpm, where I need to execute external
79 commands and Perl code refs with:
80
81 timeout
82 quoting
83 flexible logging
84
85 While App::cpanminus has excellent APIs for such use, I still needed to
86 tweak them in App::cpm.
87
88 So I ended up creating a seperate module, Command::Runner.
89
91 Shoichi Kaji <skaji@cpan.org>
92
94 Copyright 2017 Shoichi Kaji <skaji@cpan.org>
95
96 This library is free software; you can redistribute it and/or modify it
97 under the same terms as Perl itself.
98
99
100
101perl v5.36.0 2023-01-20 Command::Runner(3pm)