Reading stdout and stderr separately

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ladygrinningsoul

    Reading stdout and stderr separately


    I have a Perl program which runs a number of external programs. I want to
    be able to read both stdout and stderr into separate variables. The 'Perl
    Cookbook' states (I quote):

    To read both a command's STDOUT and its STDERR separately, it's easiest and
    safest to redirect them separately to files, and then read from those files
    when the program is done: system("program args 1>/tmp/program.stdout
    2>/tmp/program.stderr" );


    There has got to be a better way. Anyone?






  • Joe Smith

    #2
    Re: Reading stdout and stderr separately

    ladygrinningsou l wrote:
    [color=blue]
    > There has got to be a better way.[/color]

    1) Look into IPC::Open3.
    2) Next time, post to comp.lang.perl. misc (not comp.lang.perl) .

    Comment

    • Jürgen Exner

      #3
      Re: Reading stdout and stderr separately

      ladygrinningsou l wrote:[color=blue]
      > I have a Perl program which runs a number of external programs. I
      > want to be able to read both stdout and stderr into separate
      > variables. The 'Perl Cookbook' states (I quote):
      >
      > To read both a command's STDOUT and its STDERR separately, it's
      > easiest and safest to redirect them separately to files, and then
      > read from those files when the program is done: system("program args
      > 1>/tmp/program.stdout 2>/tmp/program.stderr" );
      >
      >
      > There has got to be a better way. Anyone?[/color]

      Well, it's the same method as recommended by the FAQ. You did check the FAQ,
      didn't you?

      jue


      Comment

      • Alexey A. Kiritchun

        #4
        Re: Reading stdout and stderr separately

        ladygrinningsou l wrote:
        [color=blue]
        >
        > I have a Perl program which runs a number of external programs. I want to
        > be able to read both stdout and stderr into separate variables. The 'Perl
        > Cookbook' states (I quote):[/color]
        [color=blue]
        > There has got to be a better way. Anyone?[/color]

        You should have read the book a little further. See recipe 16.9, (or at
        least it is under that number in my copy), or jump straight to perldoc
        IPC::Open3.

        Using it is rather prone to deadlock, though.

        If you are doing it as an excercise only, it is also posible to do
        everything via fork(), exec(), and POSIX::dup().

        --
        Alexey 'Kaa the Snake' Kiritchun

        Comment

        Working...