Loading ...
Sorry, an error occurred while loading the content.

Redirecting STDERR & multiplexing STDOUT

Expand Messages
  • Madani, Srikanth, VF-DE
    A very happy new year to all, I posted this on another Perl forum yesterday; got no replies though - perhaps it s trivial? I m getting some unexpected output
    Message 1 of 2 , Jan 4, 2005
    • 0 Attachment
      A very happy new year to all,

      I posted this on another Perl forum yesterday; got no replies though -
      perhaps it's trivial?

      I'm getting some unexpected output in a text file to which I've
      redirected standard error, and also multiplexed standard output.

      Here's the relevant code, followed by the output, and the contents of
      the text file.


      #____________________________code snippet
      starts_________________________________
      use IO::Tee;
      my $log = "LOGFILE";
      open (LOG, ">$log") || die "\n$0: Fatal error - can't create log file
      $log : $!\n";

      my $tee = IO::Tee->new ( ">$log", ">&1"); #Bind the logfile and STDOUT

      open(STDERR, ">>$log") || die "\nCan't redirect STDERR: $!\n"; #Redirect
      STDERR to the logfile

      select(STDERR); $| = 1; #No buffering
      select(STDOUT); $| = 1; #No buffering

      print $tee "\nThis goes to STDOUT and LOG\n";

      chdir "holiday" || die "\nThis goes to STDERR: $!\n"; #Attempt to cd to
      a non-existent directory.

      $tee->flush();
      close LOG;
      #____________________________code snippet
      starts_________________________________


      When I execute this:

      $> ./triple_handle.2.pl

      This goes to STDOUT and LOG
      $>


      The LOGFILE contains the following:

      $> cat LOGFILE

      This goes to STDOUT and LOG
      code:

      This goes to STDERR: No such file or directoryy $>


      1. What's "code:" and the extra new-line doing in my logfile?

      2. Can I use IO:Tee to duplicate STDERR as well - so that anything
      written to STDERR will also be copied to the logfile?
      (Which means I'll see it on the screen and the logfile, just like stuff
      written by me to $tee)




      Cheers,

      Srikanth Madani
    • Madani, Srikanth, VF-DE
      Ok, I got it. Merely a case of multiple output streams corrupting each other while writing to the same file. Cheers, Srikanth Madani
      Message 2 of 2 , Jan 4, 2005
      • 0 Attachment
        Ok, I got it.

        Merely a case of multiple output streams corrupting each other while
        writing to the same file.

        Cheers,

        Srikanth Madani
      Your message has been successfully submitted and would be delivered to recipients shortly.