Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: why IPC::Open3 can't execute MS-DOS "dir" command?

by zentara (Archbishop)
on Dec 04, 2009 at 13:14 UTC ( #811081=note: print w/ replies, xml ) Need Help??


in reply to why IPC::Open3 can't execute MS-DOS "dir" command?

Any one knows what's the problem?

... yes...since you are trying to run the msdos dir command, i assume you are on win32 and not an msdos emulator under linux;-)

IPC::Open3 dosn't work on win32... see IPC::Open3 failure on Win32 for alternatives.....IIRC win32 dosn't have an easy select on pipes..... and IPC::Open3 requires that..... but maybe windows7 will correct that shortcoming


I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku


Comment on Re: why IPC::Open3 can't execute MS-DOS "dir" command?
Re^2: why IPC::Open3 can't execute MS-DOS "dir" command?
by ikegami (Pope) on Dec 04, 2009 at 19:41 UTC

    If passed the ends of socket pairs instead of unopened handled, you could use open3 on Windows. I think the following will allow you to use select on Windows too:

    use Socket qw( AF_INET SOCK_STREAM PF_UNSPEC ); use IPC::Open3 qw( open3 ); sub _pipe { socketpair($_[0], $_[1], AF_INET, SOCK_STREAM, PF_UNSPEC) or return undef; shutdown($_[0], 1); # No more writing for reader shutdown($_[1], 0); # No more reading for writer return 1; } sub _open3 { local (*TO_CHLD_R, *TO_CHLD_W); local (*FR_CHLD_R, *FR_CHLD_W); local (*FR_CHLD_ERR_R, *FR_CHLD_ERR_W); if ($^O =~ /Win32/) { _pipe(*TO_CHLD_R, *TO_CHLD_W ) or die $!; _pipe(*FR_CHLD_R, *FR_CHLD_W ) or die $!; _pipe(*FR_CHLD_ERR_R, *FR_CHLD_ERR_W) or die $!; } else { pipe(*TO_CHLD_R, *TO_CHLD_W ) or die $!; pipe(*FR_CHLD_R, *FR_CHLD_W ) or die $!; pipe(*FR_CHLD_ERR_R, *FR_CHLD_ERR_W) or die $!; } my $pid = open3('>&TO_CHLD_R', '<&FR_CHLD_W', '<&FR_CHLD_ERR_W', @ +_); return ( $pid, *TO_CHLD_W, *FR_CHLD_R, *FR_CHLD_ERR_R ); } my ($pid, $to_chld, $fr_chld, $fr_chld_err) = _open3(...);

      See also Win32::Socketpair


      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.
      Got an error like this:
      Unknown error at C:\811150.pl line 18
      use Socket qw( AF_INET SOCK_STREAM PF_UNSPEC ); use IPC::Open3 qw( open3 ); sub _pipe { socketpair($_[0], $_[1], AF_INET, SOCK_STREAM, PF_UNSPEC) or return undef; shutdown($_[0], 1); # No more writing for reader shutdown($_[1], 0); # No more reading for writer return 1; } sub _open3 { local (*TO_CHLD_R, *TO_CHLD_W); local (*FR_CHLD_R, *FR_CHLD_W); local (*FR_CHLD_ERR_R, *FR_CHLD_ERR_W); if ($^O =~ /Win32/) { _pipe(*TO_CHLD_R, *TO_CHLD_W ) or die $!; _pipe(*FR_CHLD_R, *FR_CHLD_W ) or die $!; _pipe(*FR_CHLD_ERR_R, *FR_CHLD_ERR_W) or die $!; } else { pipe(*TO_CHLD_R, *TO_CHLD_W ) or die $!; pipe(*FR_CHLD_R, *FR_CHLD_W ) or die $!; pipe(*FR_CHLD_ERR_R, *FR_CHLD_ERR_W) or die $!; } my $pid = open3('>&TO_CHLD_R', '<&FR_CHLD_W', '<&FR_CHLD_ERR_W', @ +_); return ( $pid, *TO_CHLD_W, *FR_CHLD_R, *FR_CHLD_ERR_R ); } my $log="TestOpen3.log"; open FHO, ">$log"; select FHO; my ($pid, $to_chld, $fr_chld, $fr_chld_err) = _open3("cmd /c dir * \/b +\/s"); if((my $returnCode = $? >> 8) !=0){ print "[ERROR] ($returnCode) : "; while(<$fr_chld_err>) { print; } close $fr_chld_err; die $returnCode; } while(<$fr_chld>) { print "\t".$_; } close $fr_chld; close FHO;

        the code was untested.

        I don't have time to look at this just now, but it's surely a problem with socketpair's args.

        Start by changing

        _pipe(*TO_CHLD_R, *TO_CHLD_W ) or die $!; _pipe(*FR_CHLD_R, *FR_CHLD_W ) or die $!; _pipe(*FR_CHLD_ERR_R, *FR_CHLD_ERR_W) or die $!;
        to
        _pipe(*TO_CHLD_R, *TO_CHLD_W ) or die $^E; _pipe(*FR_CHLD_R, *FR_CHLD_W ) or die $^E; _pipe(*FR_CHLD_ERR_R, *FR_CHLD_ERR_W) or die $^E;
        to get a better error message.
        Fixed:
        use strict; use warnings; use IO::Select qw( ); use IPC::Open3 qw( open3 ); use Socket qw( AF_UNIX SOCK_STREAM PF_UNSPEC ); sub _pipe { socketpair($_[0], $_[1], AF_UNIX, SOCK_STREAM, PF_UNSPEC) or return undef; shutdown($_[0], 1); # No more writing for reader shutdown($_[1], 0); # No more reading for writer return 1; } sub _open3 { local (*TO_CHLD_R, *TO_CHLD_W); local (*FR_CHLD_R, *FR_CHLD_W); local (*FR_CHLD_ERR_R, *FR_CHLD_ERR_W); if ($^O =~ /Win32/) { _pipe(*TO_CHLD_R, *TO_CHLD_W ) or die $^E; _pipe(*FR_CHLD_R, *FR_CHLD_W ) or die $^E; _pipe(*FR_CHLD_ERR_R, *FR_CHLD_ERR_W) or die $^E; } else { pipe(*TO_CHLD_R, *TO_CHLD_W ) or die $!; pipe(*FR_CHLD_R, *FR_CHLD_W ) or die $!; pipe(*FR_CHLD_ERR_R, *FR_CHLD_ERR_W) or die $!; } my $pid = open3('>&TO_CHLD_R', '<&FR_CHLD_W', '<&FR_CHLD_ERR_W', @ +_); return ( $pid, *TO_CHLD_W, *FR_CHLD_R, *FR_CHLD_ERR_R ); } my ($pid, $to_chld, $fr_chld, $fr_chld_err) = _open3('cmd /c "dir /s/b"'); my %objs; my $in_sel = IO::Select->new(); my $out_sel = IO::Select->new(); for my $fh ($fr_chld, $fr_chld_err) { my $obj = { buf => '', }; $objs{ fileno($fh) } = $obj; $in_sel->add($fh); } close($to_chld); while ($in_sel->count() + $out_sel->count()) { my ($ins, $outs) = IO::Select::select($in_sel, $out_sel, undef); for my $fh (@$ins) { my $obj = $objs{ fileno($fh) }; our $buf; local *buf = \( $obj->{buf} ); my $bytes_read = sysread($fh, $buf, 64*1024, length($buf)); if (!$bytes_read) { warn("Error reading from child: $!\n") if !defined($bytes_read); $in_sel->remove($fh); } } for my $fh (@$outs) { } } waitpid($pid, 0); print("STDOUT:\n$objs{ fileno( $fr_chld ) }{buf}"); print("\n"; print("STDERR:\n$objs{ fileno( $fr_chld_err ) }{buf}");
Re^2: why IPC::Open3 can't execute MS-DOS "dir" command?
by Anonymous Monk on Dec 07, 2009 at 01:23 UTC
    Yes, I'm on windows xp 32-bit.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://811081]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2014-10-02 13:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (60 votes), past polls