perl-5.24.0-gcc620 on linux
I have some existing code in a perl module that uses IO::Tee to direct stdout to both a file and the terminal...
use strict;
use IO::Tee;
our $tee;
sub common {
open $tee, ">", "my.log";
$tee = new IO::Tee(\*STDOUT, $tee);
etc...
}
.
.
.
sub some_sub {
print $tee "I want this to go to the log file and the terminal. Works fine.\n";
}
Now, I want to use "some_sub" using a perl script that does not run the "common" sub which does the IO::Tee piece. So $tee will be undefined and I'll get compilation errors when I try to use it.
The hack I did is...
if($tee) {
print $tee "I want to print this\n";
} else {
print "I want to print this";
}
But the print statements are all over the place and I want a more elegant way of dealing with this. What I want to do is something like this...
if(!($tee)) {
$tee = STDOUT;
}
... then leave all the existing "print $tee ..." statements alone.
But this gives me a bareword error because I'm using "use strict"...
Bareword "STDOUT" not allowed while "strict subs" in use at ./change_tee_STDOUT.pl line 4.
If I get rid of the "use strict", it seems to work.
How can I get the functionality of "$tee = STDOUT" while retaining the "use strict" ? I feel that perhaps there is a way to cast STDOUT to a filehandle type, or something like that. Am I close ?