Hello Monks,
I have a script that uses the
CGI::Application framework and is used to serve utf-8 webpages. Today I added a functionality which reads a binary image file from disk and streams it out to the web user. While incorporating
CGI::Application::Plugin::Stream for that I found out that my image was broken unless I switched STDOUT into binary mode just before print()-ing/outputting the raw image data. But why?
I checked every bit of the output-process and finally reached my switches from the perl shebang
#!/usr/bin/perl -COE
So far this set of switches in combination with decoding_utf8() on user inputted data served me quite well. Until today when removing the switches magically solved my problems with the raw data streamout. As it seems, -COE breaks the binary data output with print().
Now, should I keep it this way, without -COE? As said, I generally serve utf-8 webpages with this script and it handles binary file prints as well.
I am aware of the fact that I need to decide wheater I use -COE on the perl command-line and use a
binmode, ':raw' on the output or the other way round: I encode all non-raw outputs to :utf8 before output except the raw data. Is there a best practice? The fact that
CGI::Application::Plugin::Stream
uses no fancy STDOUT commands seems to indicate that it's better to encode all text outputs before print, or it may be because the module is not utf-8 aware. Any suggestions?
Would be great if some knowing monk could outline what combination (-switches, encode, decode) is best to use 1. on the shebang, 2. form-data and 3. on output.
BTW: in CGI::Fast mode: should it switch STDOUT back into what it was before, after my print, or will CGI::Application magically clear the binmode on the next cycle? See:
...read $fh in binmode etc.
binmode STDOUT, ':raw';
print $buffer;
print '';
close ( $fh );
binmode STDOUT, ':utf8'; <-- :utf8 the equivalent of -CO right?
return;