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

I asked a question originally here, and rec'd some good input. I am posting this as a second thread because I view it as a seperate question, I hope I am not wrong in doing so. Combining the advice in that thread with some of my code, I came up with the following method to copy STDERR and STDOUT on win32 systems. I am no guru by any means and am open to any advice and/or criticism on this method.
#### Use cpprint in place of print #### cpprint("HELLO OUTPUT\n"); #### WARN STATEMENTS WILL BE WRITTEN TO STDOUT AND ERRFILE #### warn "HELLO ERR FILE"; #### DIE STATEMENTS WILL BE WRITTEN TO ERRFILE, SINCE DIE EXECUTES NO +MATTER WHAT IS IN THE INIT DIE STATEMENTS WILL GO TO STDERR ANYWAY ## +## die "HELLO DEATH\n"; INIT { my $outfile = "myoutfile.out"; my $errfile = "myerrfile.err"; open(ERRFILE,'>>', "$errfile") or die "Could not open error file $ +errfile $!\n"; open(OUTFILE,'>>', "$outfile") or die "Could not open error file $ +outfile $!\n"; $SIG{'__DIE__'} = \&ondie; $SIG{'__WARN__'} = \&onwarn; } END { close ERRFILE; close OUTFILE; } sub ondie { my $date = localtime(time); print ERRFILE "$date\t@_\n"; } sub onwarn { print "WARNING: @_\n"; my $date = localtime(time); print ERRFILE "$date\tWARNING: @_\n"; } sub cpprint{ my $date = localtime(time); print OUTFILE "$date\t@_"; print @_; }

Replies are listed 'Best First'.
Re: trying to copy STDERR STDOUT on win32
by ww (Archbishop) on Aug 17, 2005 at 19:36 UTC
    ...and the question is?

    Perhaps your statement that you are "open to any advice and/or criticism" is intended as a question. If so, it would be clearer if phrased as a question.

    And, to continue in quasi-Socratic fashion: does it work, as is? Well, output appears to match your description of your intent, so if this is for some production purpose, remember the advice "if it ain't broke, don't fix it. But if you're seeking advice on improvements ... well I'll leave that to those with better fu.

    And since you've created a new thread, it would be well to go back to your earlier post and update the top node with a "see also" reference to this thread.

Re: trying to copy STDERR STDOUT on win32
by DrWhy (Chaplain) on Aug 17, 2005 at 21:44 UTC
    I'm not sure this is what you want, but this code will cause die message to got to STDERR and ERRFILE, but warnings will go to STDOUT and ERRFILE.

    All-in-all, You might want to look at IO::Tee and/or Tie::FileHandle::MultiPlex to accomplish this more cleanly.

    I accomplished a similar thing with Tie::FileHandle::MultiPlex and it worked well for me.

    --DrWhy

    "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

      Tie::FileHandle::MultiPlex looks promising but I don't see how this can send my STDOUT to a file and STDOUT at the same time. I was able to send all STDERR and STDOUT to a file but was unable to also have it display it ti STDOUT. In a nutshell I want to capture all errors and output to a log as well as display it to the user. Were you able to accomplish this with Tie::FileHandle::MultiPlex? If so would I be out of line by asking you for some sample code?
        You have to dup STDOUT and then tie STDOUT with Tie::F::M, giving the dup of STDOUT as one of the 'real' handles to write to. Here's an example:
        open F, '>outfile.txt' or die "opening output file: $!\n"; open DUPOUT, '>&STDOUT' or die "Can't dup stdout: $!\n"; tie *STDOUT, 'Tie::FileHandle::MultiPlex', *DUPOUT, *F;

        --DrWhy

        "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."