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, |
In reply to Re: Split output to STDOUT as well as a file
by Athanasius
in thread Split output to STDOUT as well as a file
by razmeth
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |