in reply to How do I redirect outut from system () to a file and also to screen

The system() built-in doesn't return the output of the command. Whatever command runs shares the same standard output and standard error as the perl process. Are you sure you aren't seeing the output from "cleartool describe" on the screen?

Besides that, the tee is only going to work from the Perl program. The system() call won't know anything about it. You can collect the output from the command with the backticks or qx// operator (they're the same thing), then print it to wherever you want to put it.

The normal advice about collecting standard error involves some unix shell stuff, and I'm not sure how that works on Windows so I'll have to defer to someone else about that. I tend to like IPC::Open3, but I've heard mumblings about people who have had problems with that on Windows.

--
brian d foy <brian@stonehenge.com>
  • Comment on Re: How do I redirect outut from system () to a file and also to screen

Replies are listed 'Best First'.
Re^2: How do I redirect outut from system () to a file and also to screen
by Anonymous Monk on May 24, 2005 at 23:40 UTC
    Brian,

    The backticks work fine!! Thanks for your help.

    use IO::Tee;
    my $tee = IO::Tee -> new(">stdout.log", \*STDOUT);

    my $clrout = `cleartool describe lbtype:MYLABEL`;
    print $tee "\n$clrout also prints to screen and redirects to file!\n";
    print $tee "\nThis will print to screen and redirects to file!\n";

      You won't tee your STDERR with backticks, though. All they do is point the command's STDOUT to a string, which is then given back to you. STDERR still goes to the original STDERR, unless you do  `command 2>&1`, which will include the error messages too.

      This seems to work even under Win, even though it's bash syntax.
      Update: as eyepopslikeamosquito already points out below

      This, of course, shoves everything in the same log, which is not optimal - Log::Log4perl is probably your best friend here, and it will do you a ton of good in the future too, once you learn its ways