Perl - Validate Chinese character input from web p

2019-08-16 07:29发布

问题:

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.

回答1:

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

  • why-does-modern-perl-avoid-utf-8-by-default
  • perluniintro


回答2:

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.



标签: perl utf-8