in reply to Writing to Screen and file on Win32

You neglected to say what problem you are having with IO::Tee?

You might like to try this barebones implementation. save the following into a directory\file called

x:\perl\site\lib\My\Tee.pm

My::Tee

package My::Tee; use Tie::Handle; our @ISA = qw[ Tie::Handle ]; sub TIEHANDLE { die "Usage: tie *STDxxx, 'My::Tee', 'logfilepath'" unless @_ == 2; open my $std, '>', 'CON' or die "Couldn't reopen STDxxx : $!" and return; my( $flush, $old ) = ( $|, select( $std ) ); $| = $flush; select( $old ); open my $log, '>', $_[1] or die "Couldn't open $_[1] for output: $!" and return; bless { log => $log, stdout => $std }, $_[0]; } sub PRINT { my $self = shift; print { $self->{ log } } @_; print { $self->{ stdout } } @_; } sub PRINTF { my $self = shift; printf { $self->{ log } } @_; printf { $self->{ stdout } } @_; } 1;

The add this line to the top of your program where xxx is either OUT or ERR. Add two lines if you want to redirect both, but you will have to use separate log files for them in this case!.

tie *STDxxx, 'My::Tee', '\path\to\logfile';

You should then find that any print or printf statements end up in the log file. If you want to use syswrite etc. you'll need to add to the module. See perltie for more information if you want to attempt this.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.

Replies are listed 'Best First'.
Re: Re: Writing to Screen and file on Win32
by banesong (Acolyte) on Oct 03, 2003 at 12:36 UTC
    I was having problems getting IO::Tee to work, period. I attempted to use your suggestion, but it errors out with a 'Can't locate object method "TIEHANDLE" via package "My::Tee" at myprogram.pl line 29.' Just need to make sure everything is installed right. Sorry, long day......
      Ignore the previous. I made a tpyo. works like a charm... now to try it in the prod environment.