Search code examples
perlencodingcharacter-encodingcgiparam

How to pass a HTTP parameter from a CP1251 page to a UTF-8 handler?


When I type a Russian word in the input of the below Perl program and click Submit, I see a nonsense instead of Russian letters.

How to pass data from a page encoded as CP1251 to a handler script which needs UTF-8 strings? (The script below is a simple example of this situation.)

#!/usr/bin/perl

use strict;
use warnings;

use CGI qw/param/;

if (param('x')) {
  print "Content-Type: text/plain; charset=utf-8\n\n";
  print "[[".param('x')."]]";
} else {
  print "Content-Type: text/html; charset=windows-1251\n\n";
  print '<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1251"></head>';
  print "<form><input name='x'/><input type='submit'/></form>";
}

Solution

  • I can ether convert the value of param() from CP1251 to UTF-8 or add accept-charset='utf-8' attribute to <form> element.