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;

In reply to perl -COE and, for example, CGI::Application by isync

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.