Elegant has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm currently trying to port a project over from linux to Windows and hit a bit of a snag when using Open3 and IO::Select to read both STDOUT and STDERR.

It appears that if I dump the select handle I never see STDERR get set and I never got into my loop because can_read is never ready. I'm not quite sure of how to work around this on Windows and could really use some help!

sub sys { my $self = shift; my $app = shift; my ($pid, $in, $out, $err, $sel, $buf); $err = gensym(); more(); TRACE "sys > $app @_"; $pid = open3($in, $out, $err, $app, @_) or LOGDIE "failed to open $app +: @_"; $sel = new IO::Select; $sel->add($out, $err); SYSLOOP: while(my @ready = $sel->can_read) { foreach my $fh (@ready) { my $line = <$fh>; if(not defined $line) { $sel->remove($fh); next; } if($fh == $out) { TRACE "sys < $line"; $buf .= $line; } elsif($fh == $err) { TRACE "sys !! $line"; $buf .= $line; } else { ERROR "Shouldn't be here\n"; return undef; } } } waitpid($pid, 0); less(); return $buf; }

Using strawberry perl 5.18.1.1 64bit on Windows 7 x64.

Replies are listed 'Best First'.
Re: Open3 and IO:Select on Win32
by BrowserUk (Patriarch) on Sep 12, 2014 at 05:29 UTC

    Win32 pipes do not support select, thus using open3 that way on win32 doesn't work.

    As an alternative, consider Win32::SocketPair::winopen2_5().


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Alright, I'll look into it. Documentation is a bit rough (new to Perl) but I should be able to figure this out...

        Documentation is a bit rough

        I've annotated a short test script that was kicking around in my test directory. Maybe it'll clarify things a little?

        #! perl -slw use strict; use Win32::Socketpair qw[ winopen2_5 ]; ## Open a pipe to perl.exe -c ## Retrieving the pid and the socket handle my( $pid, $sock ) = winopen2_5( 'perl.exe', '-c' ); ## Print a short script to it for syntax checking print $sock <<'EOP'; my $x = 365.25**3; my $y = qx[ perl -c -e"say 'boo'"; ]; my $z = sub { print $sock <<; my $x = 365.25**3; my $y = qx[ perl -c -e"say 'boo'"; ]; my $z = sub { print $sock <<; my $x = 365.25**3; my $y = qx[ perl -c -e"say 'boo'"; ]; my $z = sub { 1; }; }; }; EOP ## Tell the socket that we have finsihed writing to it ## Has the effect of allowing the pelr process to know ## that no more data is coming, so it can process the script shutdown $sock, 1; ## Read back all the output from the perl process ## -- stdout & stderr -- and display on terminal print while <$sock>; ## Close the socket close $sock; ## Start another copy of perl with a one liner argument ## that outputs to both stdout and stderr ( $pid, $sock ) = winopen2_5( 'perl.exe', q[-E"say 'hello'; warn; die; +"] ); ## Tell the socket we've finished writing shutdown $sock, 1; ## print teh retrieved output to the terminal print while <$sock>; ## and done close $sock; ## The output produced is below __END__ C:\test>winopen2_5 - syntax OK Warning: something's wrong at -e line 1. Died at -e line 1. hello

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Open3 and IO:Select on Win32
by zentara (Cardinal) on Sep 12, 2014 at 11:23 UTC