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

Hi Perl Monks!

I've finished writing a script, and now I want for it to print all messages both to screen and to a (log) file. So a little googling pointed me to this File::Tee module. Installed it (locally as I'm not root) and ran just a basic example.
But I have a problem making it work with STDIN. Here is a little code that doesn't work right, or at least it doesn't work as I thought it should.
#!/usr/bin/perl -w use lib '/path/to/local/lib/'; use File::Tee qw(tee); ################ ## No bug yet ## ################ #Print Starting Info print ("=============================\n"); print (" Just a simple bug example \n"); print ("=============================\n\n"); # Get user input print ("Enter anything: "); $CODE = <>; chomp ($CODE); print ("You used $CODE!\n=============================\n\n"); ################ ## Here it is ## ################ # Start with Tee tee(STDOUT, '>', 'logfile.txt'); #Print Starting Info print ("=============================\n"); print (" Just a simple bug example \n"); print ("=============================\n\n"); # Get user input print ("Enter anything: "); $CODE2 = <>; chomp ($CODE2); print ("You used $CODE2!\n=============================\n\n");
It works right until it comes to the tee(...) part. Then it just waits for my blind input before printing the input message... Here is the output
============================= Just a simple bug example ============================= Enter anything: PerlMonks You used PerlMonks! ============================= ============================= Just a simple bug example ============================= TeeTestPM perl/teetest> Enter anything: You used TeeTestPM! =============================
Anyone could help out?

Thanks a bunch!
Cheers!

Replies are listed 'Best First'.
Re: Tee Module
by salva (Canon) on Mar 15, 2013 at 12:02 UTC
    File::Tee is only intended to be used for output filehandles!
      Ah... I'll use IO::Tee then. Thanks salva!
Re: Tee Module
by vinoth.ree (Monsignor) on Mar 15, 2013 at 12:54 UTC
      Thanks!

      Reading this gave me the idea of doing it like this.
      # Open Log File for writting open (LOGFILE, "> logfile.txt") or die ("Unable to open LogFile.\n"); sub dualprint { print "@_"; print LOGFILE "@_"; } sub logprint { print LOGFILE "@_"; }
      And then all print commands are replaced with dualprint, and the input stuff is printed with logprint to a log file only, because it is printed to the screen anyway.

      Thank you so much!