File Coverage

blib/lib/IPC/ReadpipeX.pm
Criterion Covered Total %
statement 21 21 100.0
branch 6 6 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 34 34 100.0


line stmt bran cond sub pod time code
1             package IPC::ReadpipeX;
2              
3 1     1   68823 use strict;
  1         7  
  1         28  
4 1     1   5 use warnings;
  1         2  
  1         25  
5 1     1   5 use Carp 'croak';
  1         2  
  1         41  
6 1     1   5 use Exporter 'import';
  1         2  
  1         50  
7              
8             our $VERSION = '0.001';
9              
10             our @EXPORT = 'readpipex';
11              
12             sub readpipex {
13 1     1   5 no warnings 'exec';
  1         2  
  1         198  
14 4 100   4 1 7421 open my $stdout, '-|', @_ or croak "readpipex '$_[0]' failed: $!";
15             my @output = wantarray ? readline($stdout)
16 3 100       1843 : do { local $/; scalar readline $stdout };
  2         56  
  2         3533  
17 3         108 close $stdout;
18 3 100       218 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 @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 function, also known as the C
43             operator or backticks (C<``>), runs a command and captures the output (STDOUT).
44             However, unlike L and L, 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
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.
51              
52             =head1 FUNCTIONS
53              
54             C 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 and L.
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
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 - provides C and C functions with
124             optional exit status checking and variants that always bypass the shell
125              
126             =item *
127              
128             L - run a process and direct STDIN, STDOUT, and STDERR
129              
130             =item *
131              
132             L - capture STDOUT and STDERR in any wrapped code
133              
134             =item *
135              
136             L - complete asynchronous control over a process and its
137             handles
138              
139             =back