Hi

I run CGI::Application::Plugin::TT under mod_perl and need to send either UTF-8 or ISO-8859-1 encoded xml (generated by templates) back to the client.

I have asked about this before but it seems that changing the encoding of STDOUT via binmode does not work under mod_perl.

I have now found a solution that works for me, but it is not entirely clear to me..

What I do is let TT generate the xml in utf8 and then change the encoding in a cgiapp_postrun method like this:

use Encode; sub cgiapp_postrun { my($this, $response_ref)=@_; my $encoding = ... #figure out which encoding to use if($encoding ne "UTF-8") { my $response_utf8 = decode('UTF-8', $$response_ref); $$response_ref = encode('ISO-8859-1', $response_utf8); }
This works but what I don't understand is why it is neccessary to first call decode (it does not work without it).

I always thought that by default a Perl-string would be utf8, so I had assumed that I can call encode straight away, but this evidently is wrong...

So can someone please explain what is going on?

Many thanks!


In reply to decode/encode - can someone explain this please by morgon

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.