Interesting open3 redirection problem

Discussion in 'Perl Misc' started by usenet@schweikhardt.net, Jan 22, 2009.

  1. Guest

    Consider this little complete perl program, intended to imitate a
    shell's
    gcc -v -E -x c - </dev/null >/dev/null
    command:

    #!/usr/bin/env perl

    use warnings;
    use strict;
    use File::Spec;
    use IPC::Open3;

    # WORKS: shell$ gcc -v -E -x c - </dev/null >/dev/null
    # WORKS: my @cmd = ('gcc', '-v', '-E', '-x', 'c', '/dev/null');

    # This one never gets out of the while loop.
    my @cmd = ('gcc', '-v', '-E', '-x', 'c', '-');

    if (open (my $NULLOUT, ">", File::Spec->devnull)) {
    if (open (my $NULLIN, "<", File::Spec->devnull)) {
    my $pid = open3 ($NULLIN, $NULLOUT, \*CMD, @cmd);
    while (<CMD>) {
    print;
    }
    print "left while(<CMD>)\n";
    waitpid ($pid, 0);
    close CMD;
    close $NULLIN;
    }
    close $NULLOUT;
    }

    I wonder why it never gets out of the "while" loop, but apparently
    hangs somewhere waiting to read input that never arrives (after
    printing gcc's stderr correctly). ps(1) says its wait channel it
    piperd,
    $ ps -o wchan,args|grep perl
    piperd perl ./o3.pl (perl5.8.8)

    I'm stumped. Does any perl guru know what's going on and how to
    correct the situation?

    Regards, Jens
     
    , Jan 22, 2009
    #1
    1. Advertising

  2. wrote:
    > #!/usr/bin/env perl
    >
    > use warnings;
    > use strict;
    > use File::Spec;
    > use IPC::Open3;
    >
    >
    > # This one never gets out of the while loop.
    > my @cmd = ('gcc', '-v', '-E', '-x', 'c', '-');
    >
    > if (open (my $NULLOUT, ">", File::Spec->devnull)) {
    > if (open (my $NULLIN, "<", File::Spec->devnull)) {
    > my $pid = open3 ($NULLIN, $NULLOUT, \*CMD, @cmd);


    First you open $NULLOUT to be a handle hooked up to write to null.
    Then you close it, and reopen it to be a handle hooked up to write to
    gcc (which means gcc is hooked up to read, as its stdin, what you write
    to $NULLOUT). Since you neither write to nor close $NULLOUT, gcc blocks
    waiting for you to do one of those.

    > I'm stumped. Does any perl guru know what's going on and how to
    > correct the situation?


    If you want gcc to read directly from the handle, (rather than read what
    Perl writes *to* that handle) then the docs to open3 say you specify the
    first argument to open3 to start with "<&" (which means you probably
    can't use a lexical filehandle, at least not easily)

    Of course, to do that, you should open the handle $NULLIN to be read
    from, not written to, or else gcc will complain that its stdin is not
    open for reading.

    But if you want @cmd to inherit a close file stdin, why not just specify
    a first argument of undef to open3?

    Xho
     
    Xho Jingleheimerschmidt, Jan 23, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Newberry
    Replies:
    0
    Views:
    673
    Dave Newberry
    Oct 17, 2004
  2. Yuri Leikind

    using open3.rb from a thread

    Yuri Leikind, Jan 23, 2004, in forum: Ruby
    Replies:
    1
    Views:
    110
    Joel VanderWerf
    Jan 23, 2004
  3. Francis Hwang

    Closing files opened with Open3.popen

    Francis Hwang, Jun 2, 2004, in forum: Ruby
    Replies:
    2
    Views:
    180
    Paul Brannan
    Jun 2, 2004
  4. J. Romano
    Replies:
    0
    Views:
    180
    J. Romano
    Feb 10, 2004
  5. VANOSPAM
    Replies:
    2
    Views:
    137
    VANOSPAM
    Sep 24, 2009
Loading...

Share This Page