line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package IPC::ReadpipeX; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
55285
|
use strict; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
21
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
19
|
|
5
|
1
|
|
|
1
|
|
4
|
use Carp 'croak'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
32
|
|
6
|
1
|
|
|
1
|
|
5
|
use Exporter 'import'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
51
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.003'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @EXPORT = 'readpipex'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub readpipex { |
13
|
1
|
|
|
1
|
|
6
|
no warnings 'exec'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
126
|
|
14
|
4
|
100
|
|
4
|
1
|
6822
|
open my $stdout, '-|', @_ or croak "readpipex '$_[0]' failed: $!"; |
15
|
|
|
|
|
|
|
my @output = wantarray ? readline($stdout) |
16
|
3
|
100
|
|
|
|
1568
|
: do { local $/; scalar readline $stdout }; |
|
2
|
|
|
|
|
35
|
|
|
2
|
|
|
|
|
2955
|
|
17
|
3
|
|
|
|
|
90
|
close $stdout; |
18
|
3
|
100
|
|
|
|
197
|
return wantarray ? @output : $output[0]; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
1; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 NAME |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
IPC::ReadpipeX - List form of readpipe/qx/backticks for capturing output |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 SYNOPSIS |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
use IPC::ReadpipeX; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $path = '/file path/with$shell/characters&'; |
32
|
|
|
|
|
|
|
my @entries = readpipex 'ls', '-l', $path; |
33
|
|
|
|
|
|
|
if ($?) { |
34
|
|
|
|
|
|
|
my $exit = $? >> 8; |
35
|
|
|
|
|
|
|
die "ls '$path' exited with status $exit"; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $hostname = readpipex 'hostname', '-f'; |
39
|
|
|
|
|
|
|
chomp $hostname; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 DESCRIPTION |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
The built-in L function, also known as the C |
44
|
|
|
|
|
|
|
operator or backticks (C<``>), runs a command and captures the output (STDOUT). |
45
|
|
|
|
|
|
|
However, unlike L and L, the |
46
|
|
|
|
|
|
|
command will always be parsed by the shell, and it does not provide a list form |
47
|
|
|
|
|
|
|
to bypass shell parsing when multiple arguments are passed. L"readpipex"> |
48
|
|
|
|
|
|
|
provides this capability in a simple copy-pastable function. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
For other methods of redirecting output, capturing STDERR, and interacting with |
51
|
|
|
|
|
|
|
the process, consider the modules listed in L"SEE ALSO">. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 FUNCTIONS |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
C is exported by default. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head2 readpipex |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my $output = readpipex $cmd, @args; |
60
|
|
|
|
|
|
|
my @output = readpipex $cmd, @args; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Runs the given command, capturing STDOUT and returning it as a single string in |
63
|
|
|
|
|
|
|
scalar context, or an array of lines in list context. If more than one argument |
64
|
|
|
|
|
|
|
is passed, the command will be executed directly rather than via the shell, as |
65
|
|
|
|
|
|
|
in L and L. The command and |
66
|
|
|
|
|
|
|
each argument will be passed directly to the L system call, so the |
67
|
|
|
|
|
|
|
program will receive the arguments exactly as passed, without first |
68
|
|
|
|
|
|
|
interpreting shell metacharacters. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Errors forking or running the command will raise an exception, and |
71
|
|
|
|
|
|
|
L<$!|perlvar/"$!"> will be set to the error code. The exit status of the |
72
|
|
|
|
|
|
|
process is otherwise available in L<$?|perlvar/"$?"> as normal. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
The code of this function can easily be copy-pasted and is shown below. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub readpipex { |
77
|
|
|
|
|
|
|
no warnings 'exec'; |
78
|
|
|
|
|
|
|
open my $stdout, '-|', @_ or die "readpipex '$_[0]' failed: $!"; |
79
|
|
|
|
|
|
|
my @output = wantarray ? readline($stdout) |
80
|
|
|
|
|
|
|
: do { local $/; scalar readline $stdout }; |
81
|
|
|
|
|
|
|
close $stdout; |
82
|
|
|
|
|
|
|
return wantarray ? @output : $output[0]; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
The above code snippet may be considered to be licensed under |
86
|
|
|
|
|
|
|
L |
87
|
|
|
|
|
|
|
for the purpose of copying without attribution or warranty. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 CAVEATS |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=over |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=item * |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Behavior when passing no arguments is unspecified. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=item * |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
The shell can still be invoked if only one argument is passed. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=item * |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
The C<-|> open mode requires Perl 5.8 or newer on a system that supports |
104
|
|
|
|
|
|
|
forking, or Perl 5.22 or newer on Windows. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item * |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Errors while reading or closing the pipe, though exceedingly rare, are ignored, |
109
|
|
|
|
|
|
|
as in the core readpipe. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=back |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 BUGS |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Report any issues on the public bugtracker. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 AUTHOR |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Dan Book |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
This software is Copyright (c) 2019 by Dan Book. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
This is free software, licensed under: |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 SEE ALSO |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=over |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=item * |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
L - provides C and C functions with |
136
|
|
|
|
|
|
|
optional exit status checking and variants that always bypass the shell |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item * |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
L - run a process and direct STDIN, STDOUT, and STDERR |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item * |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
L - capture STDOUT and STDERR in any wrapped code |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item * |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
L - complete asynchronous control over a process and its |
149
|
|
|
|
|
|
|
handles |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=back |