blib/lib/IPC/ReadpipeX.pm

filter: Statementstmt 100% 24 / 24 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 100% 37 / 37 SCAR 13.9
CC
4
Cov
100%
readpipex
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 4.0
↑ Summary
1 package IPC::ReadpipeX;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.002';
9  
10 1 our @EXPORT = 'readpipex';
11  
12 sub readpipex {
13 1 ▶ no warnings 'exec';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶ open my $stdout, '-|', @_ or croak "readpipex '$_[0]' failed: $!";
Subroutine readpipex: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
open my $stdout, "-|", @_ or croak("readpipex '$_[0]' failed: $!") 1 3 4
15 my @output = wantarray ? readline($stdout)
16 3 ▶ : do { local $/; scalar readline $stdout };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 2 3
17 3 close $stdout;
18 3 ▶ return wantarray ? @output : $output[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 2 3
19 }
20  
21 1 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 @entries = readpipex 'ls', '-l', $path;
32 if ($?) {
33 my $exit = $? >> 8;
34 die "ls '$path' exited with status $exit";
35 }
36  
37 my $hostname = readpipex 'hostname', '-f';
38 chomp $hostname;
39  
40 =head1 DESCRIPTION
41  
42 The built-in L<readpipe|perlfunc/"readpipe"> function, also known as the C<qx>
43 operator or backticks (C<``>), runs a command and captures the output (STDOUT).
44 However, unlike L<system|perlfunc/"system"> and L<exec|perlfunc/"exec">, the
45 command will always be parsed by the shell, and it does not provide a list form
46 to bypass shell parsing when multiple arguments are passed. L</"readpipex">
47 provides this capability in a simple copy-pastable function.
48  
49 For other methods of redirecting output, capturing STDERR, and interacting with
50 the process, consider the modules listed in L</"SEE ALSO">.
51  
52 =head1 FUNCTIONS
53  
54 C<readpipex> is exported by default.
55  
56 =head2 readpipex
57  
58 my $output = readpipex $cmd, @args;
59 my @output = readpipex $cmd, @args;
60  
61 Runs the given command, capturing STDOUT and returning it as a single string in
62 scalar context, or an array of lines in list context. If more than one argument
63 is passed, the command will be executed directly rather than via the shell, as
64 in L<system|perlfunc/"system"> and L<exec|perlfunc/"exec">.
65  
66 Errors forking or running the command will raise an exception, and
67 L<$!|perlvar/"$!"> will be set to the error code. The exit status of the
68 process is otherwise available in L<$?|perlvar/"$?"> as normal.
69  
70 The code of this function can easily be copy-pasted and is shown below.
71  
72 sub readpipex {
73 no warnings 'exec';
74 open my $stdout, '-|', @_ or die "readpipex '$_[0]' failed: $!";
75 my @output = wantarray ? readline($stdout)
76 : do { local $/; scalar readline $stdout };
77 close $stdout;
78 return wantarray ? @output : $output[0];
79 }
80  
81 =head1 CAVEATS
82  
83 =over
84  
85 =item *
86  
87 Behavior when passing no arguments is unspecified.
88  
89 =item *
90  
91 The C<-|> open mode requires Perl 5.8 or newer on a system that supports
92 forking, or Perl 5.22 or newer on Windows.
93  
94 =item *
95  
96 Errors while reading or closing the pipe, though exceedingly rare, are ignored,
97 as in the core readpipe.
98  
99 =back
100  
101 =head1 BUGS
102  
103 Report any issues on the public bugtracker.
104  
105 =head1 AUTHOR
106  
107 Dan Book <dbook@cpan.org>
108  
109 =head1 COPYRIGHT AND LICENSE
110  
111 This software is Copyright (c) 2019 by Dan Book.
112  
113 This is free software, licensed under:
114  
115 The Artistic License 2.0 (GPL Compatible)
116  
117 =head1 SEE ALSO
118  
119 =over
120  
121 =item *
122  
123 L<IPC::System::Simple> - provides C<system> and C<capture> functions with
124 optional exit status checking and variants that always bypass the shell
125  
126 =item *
127  
128 L<IPC::Run3> - run a process and direct STDIN, STDOUT, and STDERR
129  
130 =item *
131  
132 L<Capture::Tiny> - capture STDOUT and STDERR in any wrapped code
133  
134 =item *
135  
136 L<IO::Async::Process> - complete asynchronous control over a process and its
137 handles
138  
139 =back
↑ Summary