I thought my knowledge of unicode handling in perth had evolved to the point where I could include a unicode character in my template files but I thought very wrong. I've been finding the message "Use of wide characters in FCGI::Stream::PRINT is deprecated and will stop working in a future version of FCGI" in my server logs and no amount of setting STDOUT to utf8 encoding through various pragmas or binmode invocations has been able to remove it. Extensive googling turned up the possibility that the FCGI has no concept of Unicode handling and the most thorough explanation I could find was this rant at https://www.apachelounge.com/viewtopic.php?p=37483 which posits that the use of tied handles makes changing STDOUT ineffective and the only solution was to roll your own print routine like..
sub my_print($) { my($string) = @_; my $is_utf8 = is_utf8(${$string}); _utf8_off(${$string}) if($is_utf8); print ${$string}; _utf8_on(${$string}) if($is_utf8); }
I could already hear the lynch mobs baying about the scandalous use of _utf8_off and while at this point worrying about making unicode handling even more broken seemed like an academic distinction even I was troubled about turning off the utf8 flag on a string that I knew actually was utf8 but luckily I only had to get halfway through that doomed solution as it turns out my template module will take a file handle to use instead of STDOUT and I could print to the string to get it flagged as UTF like so...
my $string; open(my $out, "+<", \$string); binmode $out, ':encoding(UTF-8)'; $page->output($out); close($out); print $string;
Without the binmode on the strings file handle I was getting the generic wide characters warnings from the perl interpreter and nothing from FCGI but with it everything is now working. I came here to ask WTF is going on but in the course of writing out the question I think I actually figured it out but I'm going to post anyway in case it helps the next person trying to find an answer on google. In fact I'm now thinking that use v5.14; in my template module instead of just the main script would also have solved this problem although it feels counter-intuitive that unicode handling in my module isn't inherited from the main script. If I have to find a question I guess maybe it's turned into should I just use feature 'unicode_strings' (or use v5.14) in all libraries going forward or does this just cause the same problem for future users calling the modules from a script that doesn't have that? Ideally I would set a configuration switch to turn it on or off but I don't think use pragmas can be set like that.

In reply to FCGI, tied handles and wide characters by Maelstrom

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.