Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Tee Module

by Ballistic (Initiate)
on Mar 15, 2013 at 11:36 UTC ( #1023671=perlquestion: print w/ replies, xml ) Need Help??
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!

Comment on Tee Module
Select or Download Code
Re: Tee Module
by salva (Monsignor) 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 (Parson) 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!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (15)
As of 2014-07-31 13:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls