Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi again,
I'm having trouble when passing unicode strings via a html form text box. When I use the get method I can see that the unicode is being represented in utf-8 form, such that when I enter the word 'Bris' with a long r and a long s, the following is passed -

B%C9%BCi%C5%BF

the unicode for 'Bris' is - 'B' U+027c 'i' U+017f

'Bris' is entered into the text box called "Name". I now wish to see that the string 'Bris' was entered into the text box using the following code -
my $lr = chr(636); my $ls = chr(383); $str = 'B'.$lr.'i'.$ls, if ($str eq param('Name') { ...
but this comparison is always giving 0.
I also tried the following -
PrintUnicodeString($str),br; PrintUnicodeString(param('Name')); PrintUnicodeString { join("", map { printf("\\x{%04X}", $_) # \x{...} } unpack("U*", $_[0])); # unpack Unicode characters }
And this gives me the output -
0042 027C 0069 017F
0042 00C9 00BC 0069 00C5 00BF

What am I doing wrong?
Thanks.

Replies are listed 'Best First'.
Re: Passing unicode via html forms
by itub (Priest) on Sep 25, 2004 at 19:38 UTC
    use 5.008; use Encode qw(decode_utf8); my $s = decode_utf8(param('name'));
      #!/usr/bin/perl -w use CGI; use CGI::Carp "fatalsToBrowser"; use HTML::Template; use Encode; my $cgi = new CGI; my $sym = $cgi->param('cs') || ''; my $euro = "\x{20AC}"; my $charset = "utf-8"; Encode::_utf8_on($sym); print $cgi->header(-charset => $charset), $cgi->start_html ( -encoding => $charset, -head => $cgi->meta({-http_equiv => "Content-Type", -content => "text/html; charset=".$charset}), -title => "Euro", ), $sym, $cgi->br, UnicodeString(($sym)), $cgi->br, UnicodeString($euro),$cgi->br; if($euro eq $sym) { print "match"; } print $cgi->end_html; sub UnicodeString{ my $str; join("", map { $str .= sprintf("0x%04X ", $_) # \x{...} } unpack("U*", $_[0])); # unpack Unicode characters return $str; }
      Call as: http://baz.perlmonk.org/euro.cgi?cs=%E2%82%AC
Re: Passing unicode via html forms
by Anonymous Monk on Sep 25, 2004 at 17:32 UTC
    Oh, and my perl version is v5.8.4