0

My Perl script accepts and processes input from a text field in a form on a web page. It was written for the English version of the web page and works just fine.

There is also a Chinese version of the page (a separate page, not both languages on the same page), and now I need my script to work with that. The user input on this page is expected to be in Chinese.

Expecting to need to work in UTF-8, I added

use utf8;

This continues to function just fine on the English page.

But in order to, for example, define a string variable for comparison that uses Chinese characters, I have to save the Perl script itself with utf-8 encoding. As soon as I do that, I get the dreaded 500 server error.

Clearly I'm going about this wrong and any helpful direction will be greatly appreciated/

Thanks.

EDIT - please see my clarification post below.

Jim_1234
  • 25
  • 4
  • 1
    `use utf8` will only tell Perl that the text inside the script is in UTF-8 encoding. In order to help you, you need to show some of your code. What is the encoding of the Chinese page? How do you get the form input from the form to the script? What is your server software? How does the script interface with it? – Javier Elices Mar 15 '18 at 21:39
  • You need to show your 500 error detail – asthman Mar 16 '18 at 01:39
  • Thanks for your feedback. I think I misdirected people. Please see my clarification below. – Jim_1234 Mar 16 '18 at 17:02

2 Answers2

0

To handle utf8 properly :

use strict; use warnings;

use utf8;
use open(IO => ':encoding(utf8)');
binmode $_, ":utf8" for qw/STDOUT STDIN STDERR/;

open(my $fh, '<:utf8', '/file/path'); # if you need a file-handle

# code.....

Check

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • Appreciate the reply, but I think I misdirected the question. Could you please see my clarification below? Thanks. – Jim_1234 Mar 16 '18 at 17:03
  • You reply got me thinking in approximately the right direction and doing some more research. Thanks. Not completely solved yet, but I'll start a new Q since this one is not really on the mark. – Jim_1234 Mar 16 '18 at 21:46
0

I'm sorry - I think I poorly expressed my question by including too much information.

The issue is - if I save my script in ANSI format and upload it to the server, it works just fine for the English page. Expecting to want to use Chinese characters in the script, I saved it in UTF-8 format and re-uploaded, and suddenly it throws 500 for the English page.

I tested with a Hello World script:

#!/usr/bin/perl -T
use strict;
use warnings;

print "Content-type: text/html\n\n";

print "Hello, world!\n";

Works fine when saved as ANSI - fails 500 when saved as UTF8.

Jim_1234
  • 25
  • 4