Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Split output to STDOUT as well as a file

by razmeth (Acolyte)
on Jan 05, 2016 at 04:45 UTC ( #1151910=perlquestion: print w/replies, xml ) Need Help??
razmeth has asked for the wisdom of the Perl Monks concerning the following question:

I'm using Windows for this scripting, and I'd like to perform something which seems to be less simple than I'd expect.

I want to use a standard print command, and have the ouput printed to the screen as well as to a file. I do not need to modify STDERR, though that would be an added benefit, and I don't want to print to a specific function which then prints to multiple locations. Is this simpler than my time spent searching has implied?

Desired code:
<Some way to split the output>
print "Hello World!"

Desired output on screen:
Hello World!

Desired output in text file:
Hello World!
  • Comment on Split output to STDOUT as well as a file

Replies are listed 'Best First'.
Re: Split output to STDOUT as well as a file
by BrowserUk (Pope) on Jan 05, 2016 at 05:03 UTC

    IO::Tee?

    Update: a worked example:

    #! perl -slw use strict; use IO::Tee; open LOG, '>', 'my.log' or die $!; my $tee = IO::Tee->new( \*STDOUT, \*LOG ); select $tee; for ( 1 .. 10 ) { print "STDOUT via print"; printf "STDOUT via printf\n"; } close LOG; __END__ C:\test>1151911 STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf C:\test>type my.log STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf STDOUT via print STDOUT via printf

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      This all looks wonderful, I will be able to test it within the next few days, thank you all!
Re: Split output to STDOUT as well as a file
by Athanasius (Chancellor) on Jan 05, 2016 at 06:34 UTC

    Hello razmeth, and welcome to the Monastery!

    The IO::Tee module recommended by BrowserUk is likely the best fit for your stated requirements. But if you need something fancier, consider Tie::STDOUT:

    #! perl use strict; use warnings; my $file = 'test.txt'; open(my $fh, '>', $file) or die "Cannot open file '$file' for writing: $!"; use Tie::STDOUT print => sub { print @_; print $fh '[', scalar localtime, ']: ', @_; }, printf => sub { printf @_; print $fh '[', scalar localtime, ']: '; printf $fh @_; }; print "Perl is cool!\n"; printf "Pi is %f\n", atan2(1, 1) * 4; close $fh or die "Cannot close file '$file': $!";

    Output:

    16:32 >perl 1504_SoPW.pl Perl is cool! Pi is 3.141593 16:32 >

    Contents of file “test.txt”:

    [Tue Jan 5 16:32:03 2016]: Perl is cool! [Tue Jan 5 16:32:03 2016]: Pi is 3.141593

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Split output to STDOUT as well as a file -- multiplexing
by Discipulus (Monsignor) on Jan 05, 2016 at 18:03 UTC
    hello razmeth and welcome to the monastery

    the key concept behind your question is multiplexing.
    Follow in your production code the wiser advices you already received but, in the case you want to explore that way and reinvent the wheel (see my sgnature..) you can have a look at my attempt to do exactly the same thing: Multiplexing log output: Log4perl of the poors
    Also Loops's attempt at Output to STDOUT and print last line to console is reach of useful hints.

    Hth
    L*


    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1151910]
Approved by BrowserUk
Front-paged by kcott
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2018-01-21 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (227 votes). Check out past polls.

    Notices?