Hello again, dear monks !
First of all, I would like to thank everyone who took time to answer my question. Your wisdom greatly inspired me !
The first (and only) thing I needed to do to fix my problem was to follow
ysth's advice. Indeed, instead of coldly close STDOUT in the calling CGI script, I just redirect it to null. This way, the process still gets "unhooked" from the browser AND calls to IPC::Open3 work as if STDOUT was never altered.
However, it would have been to bad not to follow the other advices. I decided to try to enhance my method, using IO::Select. I found
a very good node to help me in that process. So in the end, here is how I reimplemented it:
use strict;
use IPC::Open3;
use IO::Select;
use Symbol;
sub _run_ReadWrite {
my ($invocant, $cmd, $input) = @_;
# Declare needed variables
my ($child_pid, $output, $errors, $select, $fh, @ready);
my ($child_in, $child_out, $child_err) = (gensym, gensym, gensym);
# Try to connect to specified process
eval {
$child_pid = open3($child_in, $child_out, $child_err, $cmd)
};
# ..... throw error if $@ .....
# Feed input to process, then close input
print $child_in $input; close ($child_in);
# Create a select object and add fhs
$select = IO::Select->new();
$select->add($child_out, $child_err);
# This loop will block until data is available
while ( @ready = $select->can_read ) {
# We now have a list of readable fhs
foreach $fh ( @ready ) {
# Read 4096 bytes of data
my $data; my $len = sysread $fh, $data, 4096;
# There was a read error
if ( !defined $len ) {
# ..... throw error .....
}
# Current filehandle is empty, remove from select object
elsif ( $len == 0 ) { $select->remove($fh) and next }
# Data was read from a proper fh, add it to proper var
elsif ( $fh == $child_out ) { $output .= $data and next }
elsif ( $fh == $child_err ) { $errors .= $data and next }
# There was an unexpected error
else {
# ..... throw error .....
}
}
}
# Process can be reaped
waitpid($child_pid, 0);
# Return collected results
return { OUTPUT => $output , ERRORS => $errors };
}
I hope this can help someone else someday !
Until next time, have a good one and greetings from Paris, fellow monks !
Luc Gauthier
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.