in reply to Both print and redirect STDOUT and STDERR

give me a complete, foolproof, runnable solution

Sure. My rate is $75 USD / hour, 2 hour minimum.

That is not how perlmonks works. The people here are volunteering their time. To demand a full and complete solution is rather presumptuous.

I have heard that File::Tee will do the job, but that is a link to a module, so you are probably not interested, even though it does look like the documentation even provides a full solution.

--MidLifeXis

  • Comment on Re: Both print and redirect STDOUT and STDERR

Replies are listed 'Best First'.
Re^2: Both print and redirect STDOUT and STDERR
by chessgui (Scribe) on Feb 09, 2012 at 16:03 UTC
    File::Tee fails on Windows.

    However this is not as simple a problem as I first thought. First I thought you should tie STDOUT to a self defined package. However if you use 'print' in method 'PRINT' it becomes recursive. I have given up on this.

    The only solution with which I can come up is to open the program as a pipe from an other script. This is my ad hoc very unpolished solution:
    my $what=shift; use IPC::Open3; use IO::Handle; use threads; unlink('out.txt'); print "Going to run $what.n"; $pid = open3( \*CHILD_IN, \*CHILD_OUT, \*CHILD_ERR, "perl.exe $what" ) +; autoflush CHILD_OUT; autoflush CHILD_ERR; threads->create(\&handle_child_out)->detach; threads->create(\&handle_child_err)->detach; while(1){}; sub handle_child_out { do { sysread CHILD_OUT, $content, 10; if($content ne '') { print "$content"; open OUT,'>>out.txt'; print OUT $content; close OUT; } } while(1); } sub handle_child_err { do { sysread CHILD_ERR, $content, 10; if($content ne '') { print "$content"; open OUT,'>>out.txt'; print OUT $content; close OUT; } } while(1); }

      IO::Tee then.

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

        IO::Tee won't install on Windows (or at least I'm not able to install it). However I played around with the GNU tee utility for Windows and arrived at this:
        tee |cmd.exe |tee -a tee.txt
        The above accepts input from keyboard, make cmd.exe act on it and also print and append output to tee.txt.