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

Hi Monks,

I have an legacy application which prints message to tty through print command. Now I need to print messages to the tty as well as the to a file. I can't use IO::Tee as I need to change all the print statements to print to the tee handle instead of printing to STDOUT. I am looking for duping the STDOUT handle a file. Is there anyway to redirect STDOUT message to tty as well as to a file ?

-perl_lover-

Replies are listed 'Best First'.
Re: STDOUT to multiple files
by ikegami (Patriarch) on Sep 13, 2006 at 05:37 UTC
      I can do with IO::Tee and with select. But this application is a light weight application which should not use any modules other than perl builtin functions and modules. Is there any other way to achieve this without using IO::Tee ?
        IO::Tee is a Pure Perl module with no non-core dependancies. Installing it is a simple as placing Tee.pm in a subdir named IO. What's the problem?
Re: STDOUT to multiple files
by Skeeve (Parson) on Sep 13, 2006 at 07:54 UTC

    Not a perl answer, but maybe it helps?

    If you run the program on some flavour of UNIX, there should be a tee command which should do exactly what you requested:

    /path/to/my/command -o ptions | tee /path/to/logfile

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: STDOUT to multiple files
by perl_lover (Chaplain) on Sep 13, 2006 at 09:14 UTC
    Thanks for your reply. Unix tee command helped me this time.
    open(STDOUT,"| tee $log_file"); print "Testing";

    This piece of code writes testing to the log file as well as to the terminal.
    -perl_lover-

      But you need not call it inside the script itself. You can also append the tee to the script invocation.

      OTOH: When it's your script, you can also change all your print commands to "logger" commands and do the proper output there. That way you don't depend on any other module and need not call an external program.

      #each print "this is my ",$test; # would become logger("this is my ", $test);

      You will also need to have a logger function and a LOGFILE. Something like this

      open LOGFILE, '>>', $log_file; sub logger { print LOGFILE @_ if defined $log_file; print @_ }

      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: STDOUT to multiple files
by jdporter (Paladin) on Sep 13, 2006 at 15:56 UTC
Re: STDOUT to multiple files
by greatshots (Pilgrim) on Sep 13, 2006 at 07:31 UTC
    hi perl_lover,

    seems that you are trying to write a same content to multiple file. I am still thinking about such requirement in application. because in my software experience I have never come across such a requirement. so, I am really very much interested to get to know about the requirement. could you please explain more on this ? If it is really a useful feature, I can try to implement such requirement in my applications if needed in future.

    thanks a lot.

      The application will execute some commands in the remote boxes and I need a per server logging as well as it should be displayed in the terminal too. So I need to redirect the command output to STDOUT as well as to a file.
Re: STDOUT to multiple files
by OfficeLinebacker (Chaplain) on Sep 13, 2006 at 05:40 UTC
    Greeting, esteemed monk!

    Doesn't IPC::Run support filehandle duping? Sounds like you're trying to put one over or somebody.

    Update: I think the voting on this node indicates that I did not communicate very well. I don't mean that any poster on this thread, the OP nor anyone else, is or was trying to trick anyone. I simply had a little internal LOL @ the fact that "dupe" means "to deceive or mislead" (or, alternately, someone who is easy to "dupe"). I am sorry that I came across as critical.

    Terrence

    _________________________________________________________________________________

    I like computer programming because it's like Legos for the mind.