I´m trying to run a specific subroutine from a Perl library that prints a big amount of HTML code while executing. After doing some research, I found out that it is possible to use select()
method to temporarily change print
output to, say, STDERR, hiding it from the front end completely and then restoring it to STDOUT once the library has done its job.
Simple enough, but this won´t work for me. For some reason, someone created a method select()
inside a critical library that has nothing to do with the above, but is a method to interact with our database. Therefore, whenever I try to use my $filehandler = select(STDERR);
, I get the database method instead, which results in an error. And I can´t comment this method or remove it from the exported methods within that library without causing too much trouble on other scripts - including the library I'm trying to run.
I can´t even use HTML block comment to get out of this because the library also prints comments. Are there any other alternatives to run the correct select()
method? Or any other alternatives at all to temporarily prevent HTML from being printed?
EDIT: For reference, my Perl version is v5.10.1 (*) built for x86_64-linux-thread-multi
As already mentioned in a comment by toolic, the most obvious solution is to use CORE::select
. The CORE::
namespace always gives you the built-ins.
sub select { 1; }
sub html {
print "<html></html>";
}
open my $string_fh, '>', \my $string or die $!;
my $stdout = CORE::select $string_fh;
html();
CORE::select $stdout;
print "hello world";
As an alternative you could use Capture::Tiny, which really only does the same thing under the hood, but looks nicer.
use Capture::Tiny 'capture';
my ($stdout, undef, undef) = capture { html() };
As an aside, this might be a good time to refactor that select
function and all the code that uses it, and to tell the colleague how much you like them. Also, watch this talk from the Swiss Perl Workshop 2017, which is relevant to the topic.