Hi Troy,
here is a untested example. Copy it to your cgi-bin/troy.pl. Without parameters it presenrs the submit form otherwise it shows the data everytime in utf8.
The error in your script is that you confuse utf8 and unicode somewhere.
Also the meta tags looks unnecessarily to me.
PS: Consider to add readmore tags around your code on pm.
#!/usr/bin/perl
use strict;
use Encode;
use CGI;
$|++;
my $c = CGI->new;
binmode( STDOUT, ":utf8" );
unless ( () = $c->param ) {
print $c->header( -charset => 'utf-8' ),
$c->start_html('Character conversion test'), qq{
<form action="/cgi-bin/troy.pl" method="post" enctype="multipart/f
+orm-data">
<input type="hidden" name="enc_sniffer" value="\x{df}\x{20ac}">
<textarea name="textInput" rows="25" cols="72"></textarea><p>
<input type="submit">
</form>}, $c->end_html;
exit;
}
else {
my $enc;
my $hidden = $c->param('enc_sniffer');
{
use bytes;
$enc =
( $hidden eq "\xc3\x9f\xe2\x82\xac" ) ? 'utf8'
: ( $hidden eq "\xdf\x80" ) ? 'cp1252'
: 'iso-8859-1';
}
# decode all param fields here
my $qtext = decode( $enc, $c->param('textInput') );
print $c->header( -charset => 'utf-8' ), $c->start_html("Encoding is
+ $enc"),
$enc, " ", $qtext, $c->end_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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.