← Index
NYTProf Performance Profile
«
line view
»
For /usr/local/libexec/sympa/task_manager-debug.pl
Run on Tue Jun 1 22:32:51 2021
Reported on Tue Jun 1 22:35:13 2021
Filename
/usr/local/lib/perl5/5.32/IPC/Open2.pm
Statements
Executed 0 statements in 0s
Subroutines
Calls
P
F
Exclusive
Time
Inclusive
Time
Subroutine
0
0
0
0s
0s
IPC::Open2::::BEGIN@3
IPC::Open2::
BEGIN@3
0
0
0
0s
0s
IPC::Open2::::open2
IPC::Open2::
open2
Call graph for these subroutines as a
Graphviz
dot language file
.
Line
State
ments
Time
on line
Calls
Time
in subs
Code
1
package IPC::Open2;
2
3
use strict;
4
our ($VERSION, @ISA, @EXPORT);
5
6
require 5.000;
7
require Exporter;
8
9
$VERSION = 1.05;
10
@ISA = qw(Exporter);
11
@EXPORT = qw(open2);
12
13
=head1 NAME
14
15
IPC::Open2 - open a process for both reading and writing using open2()
16
17
=head1 SYNOPSIS
18
19
use IPC::Open2;
20
21
my $pid = open2(my $chld_out, my $chld_in,
22
'some', 'cmd', 'and', 'args');
23
# or passing the command through the shell
24
my $pid = open2(my $chld_out, my $chld_in, 'some cmd and args');
25
26
# read from parent STDIN and write to already open handle
27
open my $outfile, '>', 'outfile.txt' or die "open failed: $!";
28
my $pid = open2($outfile, '<&STDIN', 'some', 'cmd', 'and', 'args');
29
30
# read from already open handle and write to parent STDOUT
31
open my $infile, '<', 'infile.txt' or die "open failed: $!";
32
my $pid = open2('>&STDOUT', $infile, 'some', 'cmd', 'and', 'args');
33
34
# reap zombie and retrieve exit status
35
waitpid( $pid, 0 );
36
my $child_exit_status = $? >> 8;
37
38
=head1 DESCRIPTION
39
40
The open2() function runs the given command and connects $chld_out for
41
reading and $chld_in for writing. It's what you think should work
42
when you try
43
44
my $pid = open(my $fh, "|cmd args|");
45
46
The $chld_in filehandle will have autoflush turned on.
47
48
If $chld_out is a string (that is, a bareword filehandle rather than a glob
49
or a reference) and it begins with C<< >& >>, then the child will send output
50
directly to that file handle. If $chld_in is a string that begins with
51
C<< <& >>, then $chld_in will be closed in the parent, and the child will
52
read from it directly. In both cases, there will be a L<dup(2)> instead of a
53
L<pipe(2)> made.
54
55
If either reader or writer is the empty string or undefined, this will be
56
replaced by an autogenerated filehandle. If so, you must pass a valid lvalue
57
in the parameter slot so it can be overwritten in the caller, or
58
an exception will be raised.
59
60
open2() returns the process ID of the child process. It doesn't return on
61
failure: it just raises an exception matching C</^open2:/>. However,
62
C<exec> failures in the child are not detected. You'll have to
63
trap SIGPIPE yourself.
64
65
open2() does not wait for and reap the child process after it exits.
66
Except for short programs where it's acceptable to let the operating system
67
take care of this, you need to do this yourself. This is normally as
68
simple as calling C<waitpid $pid, 0> when you're done with the process.
69
Failing to do this can result in an accumulation of defunct or "zombie"
70
processes. See L<perlfunc/waitpid> for more information.
71
72
This whole affair is quite dangerous, as you may block forever. It
73
assumes it's going to talk to something like L<bc(1)>, both writing
74
to it and reading from it. This is presumably safe because you
75
"know" that commands like L<bc(1)> will read a line at a time and
76
output a line at a time. Programs like L<sort(1)> that read their
77
entire input stream first, however, are quite apt to cause deadlock.
78
79
The big problem with this approach is that if you don't have control
80
over source code being run in the child process, you can't control
81
what it does with pipe buffering. Thus you can't just open a pipe to
82
C<cat -v> and continually read and write a line from it.
83
84
The L<IO::Pty> and L<Expect> modules from CPAN can help with this, as
85
they provide a real tty (well, a pseudo-tty, actually), which gets you
86
back to line buffering in the invoked command again.
87
88
=head1 WARNING
89
90
The order of arguments differs from that of open3().
91
92
=head1 SEE ALSO
93
94
See L<IPC::Open3> for an alternative that handles STDERR as well. This
95
function is really just a wrapper around open3().
96
97
=cut
98
99
# &open2: tom christiansen, <tchrist@convex.com>
100
#
101
# usage: $pid = open2('rdr', 'wtr', 'some cmd and args');
102
# or $pid = open2('rdr', 'wtr', 'some', 'cmd', 'and', 'args');
103
#
104
# spawn the given $cmd and connect $rdr for
105
# reading and $wtr for writing. return pid
106
# of child, or 0 on failure.
107
#
108
# WARNING: this is dangerous, as you may block forever
109
# unless you are very careful.
110
#
111
# $wtr is left unbuffered.
112
#
113
# abort program if
114
# rdr or wtr are null
115
# a system call fails
116
117
require IPC::Open3;
118
119
sub open2 {
120
local $Carp::CarpLevel = $Carp::CarpLevel + 1;
121
return IPC::Open3::_open3('open2', $_[1], $_[0], '>&STDERR', @_[2 .. $#_]);
122
}
123
124
1