I’m changing some Perl scripts in an existing solution. Due to some changes when upgrading the (Windows) server I’ve switched them from running ISAPI to CGI. This means I now have to send Content-Type manually or it will fail.
So I need to enable output buffering (print statements, so STDOUT), send Content-Type: text/html, but in the cases where it is a redirect I need to clear output buffer and send new header.
How do I do that?
Or is there another way? Note that the script is already using print for outputting HTML, and I can’t change that. (It was written in the early 90’s.)
select(STDOUT);
$| = 0;
print "Content-Type: text/html\n\n";
# somehow clear output
print "Location: login.pl\n\n";
You can’t “undo” a
printto STDOUT. You need to decide whether you’re generating HTML output or a redirect before you send anything to STDOUT.One way of doing that would be to
selectan in-memory buffer instead of STDOUT:As soon as you’re sure you won’t be generating a redirect, you can re-
selectSTDOUT and output the buffer: