Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

print the output on the screen and to a file

by rkshyam (Acolyte)
on Mar 30, 2012 at 07:09 UTC ( [id://962541]=perlquestion: print w/replies, xml ) Need Help??

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

Hi I have a simple question: I need to print the output on the screen and to a file. I know how to do this using tee command on Linux/Unix. But I dont know in perl. I tried using tee command here too but didnt work.Should I to install any perl modules like IO::tee or File::Tee etc to make it work? I tried downloading these packages using ppm but again it failed.I have also searched in perl monk and didnot get the required correct information. Can you please help me? Thanks in advance. Sample output to print both on screen and file: print "File split start time is ", eval 'scalar localtime';

  • Comment on print the output on the screen and to a file

Replies are listed 'Best First'.
Re: print the output on the screen and to a file
by Anonymous Monk on Mar 30, 2012 at 07:12 UTC

      My question was different..should I to have packages installed to get the output using tee? I have followed all the required steps to install packages using ppm but of no use. As soon as open the ppm or try commandline option to install any package getting the results as "ppm install DateTime" Downloading ActiveState Package Repository packlist...not found ppm install failed: Can't find any package that provides DateTime I have tried setting the http proxy as provided in the guide but no luck.

Re: print the output on the screen and to a file
by ramlight (Friar) on Mar 30, 2012 at 12:59 UTC

    One other possibility is consolidate all for your print statements into calls to one central printing function that prints to both the screen and the file. This has the advantage that all of your I/O is passes through a single function. If, sometime in the future, you want to change how you do your output (use one of the Log:: modules or some similar change) then your changes are limited to one routine.

    Such a change is simple to implement at the start of development, but it is a large, time-consuming, and error-prone task when applied to existing code. (I've had to do both.)

Re: print the output on the screen and to a file
by wwe (Friar) on Mar 30, 2012 at 11:36 UTC
    Where does the output come from is this an output of your perl program? If yes just use multiple print statements:
    open my $out_file, '>>', 'myoutputfilename.txt' or die "$!"; my $output = 'blablabla'; print $output; # print to screen print $out_file $output; # print to file
    UPDATE: corrected some typos and added the die statement to open

      should I to use 2 seperate print statement each time I need to print? There will be many such statements. Can't I use single print statement which prints to the screen and to the file simultaneously as I have the option in UNIX with | tee option?

        You can install a gnu version of tee.exe on your Windows machine or just use your own tee. You like tee, so just use tee.

        Update: I tried using tee command here too but didnt work - the below works on my Windows machine.
        tee.pl

        #!usr/bin/perl -w use strict; $|=1; #turn autoflush on sub usage () { print "TEE USAGE:\n". " program | tee outfile\n". " sends stdout from program to outfile\n"; exit; } my $filename = shift @ARGV; usage unless $filename; open (OUTFILE, ">", "$filename") or (die "Can't open OUTFILE: $!"); while (<>) { print; print OUTFILE; }
        Update: In your program, unbuffer stdout. There is no need for the eval. There can be issues about how to run a perl program on Windows without having to say "perl" explictly - but that's a different subject (and this is possible to do).
        #!/usr/bin/perl -w use strict; $|=1; #unbuffer stdout print "File split start time is ", scalar localtime();
        prints: C:\TEMP>perl test.pl | perl tee.pl xxxxx File split start time is Fri Mar 30 05:57:41 2012 C:\TEMP>type xxxxx File split start time is Fri Mar 30 05:57:41 2012 C:\TEMP>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-23 15:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found