Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Redirect to STDOUT and file

by stevendel (Novice)
on Jan 09, 2008 at 16:09 UTC ( #661393=perlquestion: print w/ replies, xml ) Need Help??
stevendel has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to redirect STDOUT and STDERR to a file as well as to STDOUT or STDERR (depending on the intended destination). I am familiar with IO::Tee, so what's the problem? I want to write this in such a way that any messages, including runtime errors thrown by modules outside of my control, are sent to both places. The snippet below should show what I am looking for (and what has failed for me thus far):
use IO::Tee; use IO::File; open(OLDOUT, ">&STDOUT"); open(OLDERR, ">&STDERR"); my $output_file = new IO::File(">tt1.out"); my $TeeOut = new IO::Tee(\*OLDOUT, $output_file); my $TeeErr = new IO::Tee(\*OLDERR, $output_file); open(STDERR, ">&", \*$TeeErr); select(STDERR); $| = 1; print $TeeErr "This goes to both STDERR and to the file.\n"; print STDERR "I would like this to do the same as above, but it goes n +owhere.\n";
Any thoughts/suggestions?

Comment on Redirect to STDOUT and file
Download Code
Re: Redirect to STDOUT and file
by pilcrow (Sexton) on Jan 09, 2008 at 18:19 UTC
    open(STDERR, ">&", \*$TeeErr);

    This line doesn't do what you think. I suspect you've got a file named something like IO::Tee=GLOB(0x123456) on your system, with the output you desire.

    Something like *STDERR = $TeeErr comes closer to what you want, but isn't perfect.

    The surest way to trap and tee all of a program's output is, it seems to me, external: simply run the code piped to a teeing process.

    -Mike

      open isn't going to create a file (named IO::Tee=GLOB(0x123456) or anything else) when it wasn't asked to create a file. It returns the error "Invalid argument".

        open isn't going to create a file (named IO::Tee=GLOB(0x123456) or anything else) when it wasn't asked to create a file. It returns the error "Invalid argument".

        Not for me under 5.8.8:

        $ ls $ perl -Mstrict -MIO::Tee -le 'my $tee=IO::Tee->new(">/dev/null"); ope +n(FH, ">&", \*$tee)' $ ls IO::Tee=GLOB(0x8802e2c) $ perl -e 'printf "%vd\n", $^V' 5.8.8
Re: Redirect to STDOUT and file
by alexm (Chaplain) on Jan 09, 2008 at 18:46 UTC
    File-Tee seems quite simpler and meets your needs too. Since your specially interested in teeing STDOUT and STDERR, I think you'll find File::Tee a lot easier to use.
      File::Tee works very well for me on Unix. Unfortunately, I would like this to work on Windows, too, and even with Cygwin, it does not quite work (it does not capture the output of subprocesses).
Re: Redirect to STDOUT and file
by starbolin (Hermit) on Jan 09, 2008 at 20:09 UTC

    See 535209



    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
Re: Redirect to STDOUT and file
by absolut.todd (Monk) on Jan 10, 2008 at 01:51 UTC
Re: Redirect to STDOUT and file
by ikegami (Pope) on Jan 10, 2008 at 05:30 UTC

    This post explains pilcrow's solution.

    open '>&' creates a duplicate of a system file handle. tie doesn't create a system file handle, just a Perl one, so it can't be duplicated using open '>&'.

    However, Perl handles can be duplicated. Simply use the assignment operator.
    In general: *DST = *SRC{IO};
    In this case: *STDERR = *$TeeErr{IO};

    open '>&' is normally preferred because child processes can inherit the resulting handle. However, since IO::Tee doesn't create a real (system) file handle, neither the original nor the copy can be inherited by child processes.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://661393]
Approved by citromatik
Front-paged by citromatik
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2014-11-24 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (141 votes), past polls