Skip Menu |

This queue is for tickets about the IPC-Cmd CPAN distribution.

Report information
The Basics
Id: 85912
Status: open
Priority: 0/
Queue: IPC-Cmd

People
Owner: Nobody in particular
Requestors: jdvf [...] optonline.net
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: IPC::Cmd run_forked issue with coderefs?
Date: Thu, 06 Jun 2013 00:01:13 -0400
To: bug-ipc-cmd [...] rt.cpan.org
From: John Devitofranceschi <jdvf [...] optonline.net>
Hi there... I'm trying to figure out why this simple script does not work as expected: ================================ #!/usr/bin/perl use IPC::Cmd 0.56 qw[run_forked]; sub runSub { my $blah = "blahblah"; my $out= $_[0]; my $err= $_[1]; my $s = sub { print "$blah\n"; print "$$: Hello $out\n"; warn "Boo!\n$err\n"; }; print "About to fork\n"; return run_forked($s); } print "current pid: $$\n"; my $retval= runSub("sailor", "eek!"); print "stdout:\n" . $retval->{"stdout"} . "\n"; print "stderr:\n" . $retval->{"stderr"} . "\n"; print "exit_code:\n" . $retval->{"exit_code"} . "\n"; ====================================== Here is the output: bash-3.2$ ./try.pl current pid: 61243 About to fork blahblah 61244: Hello sailor Boo! eek! stdout: stderr: exit_code: 1 I expected: bash-3.2$ ./try.pl current pid: 61243 About to fork stdout: blahblah 61244: Hello sailor stderr: Boo! eek! exit_code: 1 It seems that stderr and stdout are not being populated and the output is just being sent to they tty. or am I missing something? jd
Download smime.p7s
application/pkcs7-signature 2.3k

Message body not shown because it is not plain text.

On Thu Jun 06 00:01:27 2013, jdvf@optonline.net wrote: Show quoted text
> > I'm trying to figure out why this simple script does not work as > expected:
I guess run_forked against coderef doesn't actually handle stout and stderr - this is a feature which I myself didn't use a lot, just added it for convenience. What run_forked does is it starts your coderef in the child and passes it the following struct: $child_exit_code = $cmd->({ 'opts' => $opts, 'parent_info' => $parent_info_socket, 'parent_stdout' => $parent_stdout_socket, 'parent_stderr' => $parent_stderr_socket, 'child_stdin' => $opts->{'child_stdin'}, }); So you can modify your sub slightly and achieve what you want: my $s = sub { my ($opts) = @_; open STDOUT, '>&', $opts->{'parent_stdout'} || die "Nope: $!\n"; open STDERR, '>&', $opts->{'parent_stderr'} || die "Nope: $!\n"; print "$blah\n"; print "$$: Hello $out\n"; warn "Boo!\n$err\n"; }; Presumably I can patch IPC::Cmd::run_forked so it does this automatically, but this breaks backwards compatibility, not sure I should do this. On the other hand this seems to be expected behavior and since you seem to be the first external user, we might change this. Chris, what do you think?