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

Greetings!

I am writing an interface for a search engine that will pull its results from a database. I have, more or less, mastered the entire code in English, and am now trying to remaster it in CJK-compatible fonts. I would prefer to use uft8, and ideally the solution should be compatible with standard Latin characters as well. I am using Perl 5.8.7.

At the core of the problem is that I am unable to get a pattern match for words in a unicode-containing string. Even if I place spaces between the words, the regex will not recognize the space. I have tried \s \p{IsSpace} \P{IsWord} \b \X \p{IsZ} \p{IsZc} and others, with or without "use Encode;" or "use utf8;" in various forms, all to no avail. Here is an example of one line of my code:

Now, that line will function perfectly on an English input string, but as soon as I enter Chinese, it won't match anything. All that line needs to do is to insert an " AND " between any two spaced words, after verifying that those words are not search operator terms themselves. So, XXX YYY OR ZZZ should become XXX AND YYY OR ZZZ.

Any ideas for why this is not performing correctly in utf8? (I'm open to a total rewrite of the line, as it's obvious I haven't found the best solution yet.)

"use utf8;" is needed if your source code is actually in UTF8 encoding. It does not affect regex matches.

Perl does not have UTF8 semantics, but instead Unicode semantics. The difference is that you work on *encodingless* strings in Perl, and that you use *normal* operators instead of separate ones. The important things are done internally.

Thank you for the helpful responses. I learned several new things. However, I am still unable to get my program functioning correctly.

For starters, I am not opening a file. I am receiving the utf8 input from an HTML form, and, quite frankly, I have no way of knowing whether user input will be received in the Perl script as utf8, unicode, big5, gb2312, etc., although I am trying to work solely with utf8. The database being searched is in utf8, but Perl is doing next to nothing with it once received with the DBI commands, other than outputting it back to HTML.

On your advice, I tried learning more about utf8, and have tried using the -COE command in the shebang line (this rendered the output unreadable), and I tried using the Encode qw(encode decode), to no avail.

Here are my current uses:

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use strict;
use DBI;
use Encode;
use Encode qw(_utf8_on);
use Encode::HanConvert; #Module for dealing with CJK conversions
use Encode qw(encode decode);
require Encode::CN;
require Encode::TW;

So I am still left wondering how to make perl see the string as utf8. Am I missing something? Is Perl 5.8.7 recent enough? How can I flag utf8 on input from a form rather than a file?

The real puzzle, to me, is that split// seems to work, but the s/// won't work. Unfortunately, I have no way of knowing how many terms the user will be entering, so using split would require some significant recoding.
Thank you!

This is late, but I do have the script working well now, and I wanted to say a big thank you to those of you who offered your support. Juerd was probably closest to the solution that seemed to fit best in my situation.

For those who may be seeking the same wisdom, I would like to post the full solution in my case.

My entire "use" section:

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use strict;
use DBI;
use Encode;
use Encode::HanConvert; #Module for dealing with CJK conversions
use Encode qw(encode decode);
use POSIX qw(locale_h);
require Encode::CN;
require Encode::TW;
require 5.004;