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

I need to write to STDOUT and to a file at the same time in Win32, but am running into difficulty. I have attempted to use IO::Tee, but am also going nuts. I have a huge script I am updating, and don't want to change every single print command if possible. Does anyone have a one or two line solution to this problem? Thanks in advance.

Replies are listed 'Best First'.
Re: Writing to Screen and file on Win32
by BrowserUk (Patriarch) on Oct 02, 2003 at 15:08 UTC

    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

    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.

      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.
Re: Writing to Screen and file on Win32
by jonadab (Parson) on Oct 02, 2003 at 16:06 UTC

    In a pinch, you can do it this way...

    sub myprint { print @_; # Send to standard output. print FILE @_; # Also send to another filehandle. }

    This approach has limitations, of course.


    $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
      This approach has limitations, of course.
      Like?

      Makeshifts last the longest.

        It only handles print not printf, though that's easily fixed.

        It requires the program to be modified to call myprint() and myprintf() instead of the system versions.


        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.

        For example, you shouldn't pass this routine any tied variables or blessed references that will have unwanted side-effects if stringified twice instead of once.


        $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
Re: Writing to Screen and file on Win32
by Aristotle (Chancellor) on Oct 02, 2003 at 20:07 UTC
    What is your problem with IO::Tee?

    Makeshifts last the longest.