mnj200g has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use strict; use warnings; my ($buf); { local *STDOUT; open( STDOUT, '>', \$buf ) or die "Write to buffer failed\n"; mysub(); } print "buffer: $buf\n"; sub mysub{ print "mysub output\n"; }
The sub's printouts go to STDOUT, which is to my screen. Using the above code, STDOUT is redirected to the variable $buf so no printouts go to the screen. I want to be able to get a copy of the data that is going to STDOUT without redirecting STDOUT. In other words, I want the data to go to the variable "$buf" in the above code and to continue to go to the screen. Is there a way to do this?
I could just print the variable $buf after its done too. However, some of data are very big and the sub that I'm trying to capture takes a long time to execute. During that time, no printouts goes to the screen so there's nothing indicating that the program is still executing correctly.
Also, is there a way to capture STDERR at the same time? I mean, can I redirect a copy of STDERR and STDOUT to the same $variable? I was able to do it to two variables, but then, I don't know which error occur when. It would be nice to have the STDOUT and STDERR in the same spot in chronological order so I know which error occurred after which STDOUT data.
Thanks for any help.
MNJ
|
|---|