Here's the situation:

I have an HTML form. When its contents are submitted to a perl script, any non-ASCII characters come out wrongly encoded, for instance if I type 'I like Beyoncé' it comes out in my script as 'I like Beyoncé'

The script does some other stuff, AJAX was involved at one point, but I have now reduced it to a simple (POST or GET) request to this script:

use CGI::Simple; my $q = CGI::Simple->new(); print "Content-type: text/plain\n\n"; print $q->param('text');

And if the 'text' parameter contains anything double-byte/UTF-8, I get that output.

Things I have tried:

But still the problem persists. What else can I do?

Confusingly, this page: https://www.i18nqa.com/debug/utf8-debug.html seems to suggest my errant character, é turning into é is caused by Windows-1252 encoding, a.k.a. ISO 8859-1, but this is nothing to do with Windows. It's happening on OS X for a start.

TIA, fellow monks

SOLVED: This is what worked:

use Encode; print Encode::decode_utf8($q->param('text'))

I guess I just have to accept that when the text leaves URL A it might be UTF-8 but there's no way to tell page B that, and I will always have to decode it.


In reply to [SOLVED] How do I convince my Perl script that UTF-8 from an HTML form really is UTF-8? by Cody Fendant

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.