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

Hi,
Im trying to open a binary through "open". Normally this is an easy task. But I need to pipe info from one program to the next, then parse the output (regardless of success of failure)

If I do:
open(SY, "echo blablabla|") or die "$!"; while(<SY>){ print ">>$_"; }
it works. But I need to do:
open(SY, "echo blablabla|/usr/bin/js|") or die "$!"; while(<SY>){ print ">>$_"; }

This pipes "blablabla" to the js program. And the output of the program should be an error. I want to catch that error (or any result from the /usr/bin/js program).

What am I doing wrong here?I think the pipe is messing everything up.

Replies are listed 'Best First'.
Re: perl, open and pipe
by JavaFan (Canon) on May 09, 2010 at 17:58 UTC
    You are opening a pipe that connects the STDOUT of /usr/bin/js to your SY handle. I'd expect /usr/bin/js to write errors to its STDERR.

    Either open a pipe to its STDERR (for instance, by using IPC::Open3, or by first redirecting the STDERR of /usr/bin/js to its STDOUT, using your shells syntax.

Re: perl, open and pipe
by shmem (Chancellor) on May 10, 2010 at 09:43 UTC
    What am I doing wrong here?I think the pipe is messing everything up.

    No, the pipe is fine. Jusr redirect stderr (fileno 2) to stdout (fileno 1):

    open(SY, "echo blablabla|/usr/bin/js 2>&1|") or die "$!"; while(<SY>){ print ">>$_"; }

      Depending on what /usr/bin/js does, that might be a bad idea. 2>&1 mixes STDERR and STDOUT into a single stream, so you may have a hard time "unmixing" them. If you need both STDOUT and STDERR, don't use open "...2>&1 |", but create two pipes manually or using IPC::Open3. If you need only STDOUT, use open "... 2> /dev/null |", if you need only STDERR, use open "... 2>&1 > /dev/null |".

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Since in the OP 'twas said
        I want to catch that error (or any result from the /usr/bin/js program).

        a single stream fits the task. Managing two file descriptors would be a YAGNI in that case.

Re: perl, open and pipe
by ikegami (Patriarch) on May 11, 2010 at 05:35 UTC