This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Inching towards Module::Build-ability on VMS.
[perl5.git] / lib / locale.t
1 #!./perl -wT
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     unshift @INC, '.';
7     require Config; import Config;
8     if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
9         print "1..0\n";
10         exit;
11     }
12     $| = 1;
13 }
14
15 use strict;
16
17 my $debug = 1;
18
19 use Dumpvalue;
20
21 my $dumper = Dumpvalue->new(
22                             tick => qq{"},
23                             quoteHighBit => 0,
24                             unctrl => "quote"
25                            );
26 sub debug {
27   return unless $debug;
28   my($mess) = join "", @_;
29   chop $mess;
30   print $dumper->stringify($mess,1), "\n";
31 }
32
33 sub debugf {
34     printf @_ if $debug;
35 }
36
37 my $have_setlocale = 0;
38 eval {
39     require POSIX;
40     import POSIX ':locale_h';
41     $have_setlocale++;
42 };
43
44 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
45 # and mingw32 uses said silly CRT
46 # This doesn't seem to be an issue any more, at least on Windows XP,
47 # so re-enable the tests for Windows XP onwards.
48 my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
49                 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
50 $have_setlocale = 0 if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') &&
51                 $Config{cc} =~ /^(cl|gcc)/i);
52
53 # UWIN seems to loop after test 98, just skip for now
54 $have_setlocale = 0 if ($^O =~ /^uwin/);
55
56 my $last = $have_setlocale ? &last : &last_without_setlocale;
57
58 print "1..$last\n";
59
60 sub LC_ALL ();
61
62 $a = 'abc %';
63
64 sub ok {
65     my ($n, $result) = @_;
66
67     print 'not ' unless ($result);
68     print "ok $n\n";
69 }
70
71 # First we'll do a lot of taint checking for locales.
72 # This is the easiest to test, actually, as any locale,
73 # even the default locale will taint under 'use locale'.
74
75 sub is_tainted { # hello, camel two.
76     no warnings 'uninitialized' ;
77     my $dummy;
78     not eval { $dummy = join("", @_), kill 0; 1 }
79 }
80
81 sub check_taint ($$) {
82     ok $_[0], is_tainted($_[1]);
83 }
84
85 sub check_taint_not ($$) {
86     ok $_[0], not is_tainted($_[1]);
87 }
88
89 use locale;     # engage locale and therefore locale taint.
90
91 check_taint_not   1, $a;
92
93 check_taint       2, uc($a);
94 check_taint       3, "\U$a";
95 check_taint       4, ucfirst($a);
96 check_taint       5, "\u$a";
97 check_taint       6, lc($a);
98 check_taint       7, "\L$a";
99 check_taint       8, lcfirst($a);
100 check_taint       9, "\l$a";
101
102 check_taint_not  10, sprintf('%e', 123.456);
103 check_taint_not  11, sprintf('%f', 123.456);
104 check_taint_not  12, sprintf('%g', 123.456);
105 check_taint_not  13, sprintf('%d', 123.456);
106 check_taint_not  14, sprintf('%x', 123.456);
107
108 $_ = $a;        # untaint $_
109
110 $_ = uc($a);    # taint $_
111
112 check_taint      15, $_;
113
114 /(\w)/; # taint $&, $`, $', $+, $1.
115 check_taint      16, $&;
116 check_taint      17, $`;
117 check_taint      18, $';
118 check_taint      19, $+;
119 check_taint      20, $1;
120 check_taint_not  21, $2;
121
122 /(.)/;  # untaint $&, $`, $', $+, $1.
123 check_taint_not  22, $&;
124 check_taint_not  23, $`;
125 check_taint_not  24, $';
126 check_taint_not  25, $+;
127 check_taint_not  26, $1;
128 check_taint_not  27, $2;
129
130 /(\W)/; # taint $&, $`, $', $+, $1.
131 check_taint      28, $&;
132 check_taint      29, $`;
133 check_taint      30, $';
134 check_taint      31, $+;
135 check_taint      32, $1;
136 check_taint_not  33, $2;
137
138 /(\s)/; # taint $&, $`, $', $+, $1.
139 check_taint      34, $&;
140 check_taint      35, $`;
141 check_taint      36, $';
142 check_taint      37, $+;
143 check_taint      38, $1;
144 check_taint_not  39, $2;
145
146 /(\S)/; # taint $&, $`, $', $+, $1.
147 check_taint      40, $&;
148 check_taint      41, $`;
149 check_taint      42, $';
150 check_taint      43, $+;
151 check_taint      44, $1;
152 check_taint_not  45, $2;
153
154 $_ = $a;        # untaint $_
155
156 check_taint_not  46, $_;
157
158 /(b)/;          # this must not taint
159 check_taint_not  47, $&;
160 check_taint_not  48, $`;
161 check_taint_not  49, $';
162 check_taint_not  50, $+;
163 check_taint_not  51, $1;
164 check_taint_not  52, $2;
165
166 $_ = $a;        # untaint $_
167
168 check_taint_not  53, $_;
169
170 $b = uc($a);    # taint $b
171 s/(.+)/$b/;     # this must taint only the $_
172
173 check_taint      54, $_;
174 check_taint_not  55, $&;
175 check_taint_not  56, $`;
176 check_taint_not  57, $';
177 check_taint_not  58, $+;
178 check_taint_not  59, $1;
179 check_taint_not  60, $2;
180
181 $_ = $a;        # untaint $_
182
183 s/(.+)/b/;      # this must not taint
184 check_taint_not  61, $_;
185 check_taint_not  62, $&;
186 check_taint_not  63, $`;
187 check_taint_not  64, $';
188 check_taint_not  65, $+;
189 check_taint_not  66, $1;
190 check_taint_not  67, $2;
191
192 $b = $a;        # untaint $b
193
194 ($b = $a) =~ s/\w/$&/;
195 check_taint      68, $b;        # $b should be tainted.
196 check_taint_not  69, $a;        # $a should be not.
197
198 $_ = $a;        # untaint $_
199
200 s/(\w)/\l$1/;   # this must taint
201 check_taint      70, $_;
202 check_taint      71, $&;
203 check_taint      72, $`;
204 check_taint      73, $';
205 check_taint      74, $+;
206 check_taint      75, $1;
207 check_taint_not  76, $2;
208
209 $_ = $a;        # untaint $_
210
211 s/(\w)/\L$1/;   # this must taint
212 check_taint      77, $_;
213 check_taint      78, $&;
214 check_taint      79, $`;
215 check_taint      80, $';
216 check_taint      81, $+;
217 check_taint      82, $1;
218 check_taint_not  83, $2;
219
220 $_ = $a;        # untaint $_
221
222 s/(\w)/\u$1/;   # this must taint
223 check_taint      84, $_;
224 check_taint      85, $&;
225 check_taint      86, $`;
226 check_taint      87, $';
227 check_taint      88, $+;
228 check_taint      89, $1;
229 check_taint_not  90, $2;
230
231 $_ = $a;        # untaint $_
232
233 s/(\w)/\U$1/;   # this must taint
234 check_taint      91, $_;
235 check_taint      92, $&;
236 check_taint      93, $`;
237 check_taint      94, $';
238 check_taint      95, $+;
239 check_taint      96, $1;
240 check_taint_not  97, $2;
241
242 # After all this tainting $a should be cool.
243
244 check_taint_not  98, $a;
245
246 sub last_without_setlocale { 98 }
247
248 # I think we've seen quite enough of taint.
249 # Let us do some *real* locale work now,
250 # unless setlocale() is missing (i.e. minitest).
251
252 exit unless $have_setlocale;
253
254 # Find locales.
255
256 debug "# Scanning for locales...\n";
257
258 # Note that it's okay that some languages have their native names
259 # capitalized here even though that's not "right".  They are lowercased
260 # anyway later during the scanning process (and besides, some clueless
261 # vendor might have them capitalized errorneously anyway).
262
263 my $locales = <<EOF;
264 Afrikaans:af:za:1 15
265 Arabic:ar:dz eg sa:6 arabic8
266 Brezhoneg Breton:br:fr:1 15
267 Bulgarski Bulgarian:bg:bg:5
268 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
269 Hrvatski Croatian:hr:hr:2
270 Cymraeg Welsh:cy:cy:1 14 15
271 Czech:cs:cz:2
272 Dansk Danish:dk:da:1 15
273 Nederlands Dutch:nl:be nl:1 15
274 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
275 Esperanto:eo:eo:3
276 Eesti Estonian:et:ee:4 6 13
277 Suomi Finnish:fi:fi:1 15
278 Flamish::fl:1 15
279 Deutsch German:de:at be ch de lu:1 15
280 Euskaraz Basque:eu:es fr:1 15
281 Galego Galician:gl:es:1 15
282 Ellada Greek:el:gr:7 g8
283 Frysk:fy:nl:1 15
284 Greenlandic:kl:gl:4 6
285 Hebrew:iw:il:8 hebrew8
286 Hungarian:hu:hu:2
287 Indonesian:in:id:1 15
288 Gaeilge Irish:ga:IE:1 14 15
289 Italiano Italian:it:ch it:1 15
290 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
291 Korean:ko:kr:
292 Latine Latin:la:va:1 15
293 Latvian:lv:lv:4 6 13
294 Lithuanian:lt:lt:4 6 13
295 Macedonian:mk:mk:1 15
296 Maltese:mt:mt:3
297 Moldovan:mo:mo:2
298 Norsk Norwegian:no no\@nynorsk:no:1 15
299 Occitan:oc:es:1 15
300 Polski Polish:pl:pl:2
301 Rumanian:ro:ro:2
302 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
303 Serbski Serbian:sr:yu:5
304 Slovak:sk:sk:2
305 Slovene Slovenian:sl:si:2
306 Sqhip Albanian:sq:sq:1 15
307 Svenska Swedish:sv:fi se:1 15
308 Thai:th:th:11 tis620
309 Turkish:tr:tr:9 turkish8
310 Yiddish:yi::1 15
311 EOF
312
313 if ($^O eq 'os390') {
314     # These cause heartburn.  Broken locales?
315     $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
316     $locales =~ s/Thai:th:th:11 tis620\n//;
317 }
318
319 sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ }
320
321 if (in_utf8) {
322     require "lib/locale/utf8";
323 } else {
324     require "lib/locale/latin1";
325 }
326
327 my @Locale;
328 my $Locale;
329 my @Alnum_;
330
331 my @utf8locale;
332 my %utf8skip;
333
334 sub getalnum_ {
335     sort grep /\w/, map { chr } 0..255
336 }
337
338 sub trylocale {
339     my $locale = shift;
340     if (setlocale(LC_ALL, $locale)) {
341         push @Locale, $locale;
342     }
343 }
344
345 sub decode_encodings {
346     my @enc;
347
348     foreach (split(/ /, shift)) {
349         if (/^(\d+)$/) {
350             push @enc, "ISO8859-$1";
351             push @enc, "iso8859$1";     # HP
352             if ($1 eq '1') {
353                  push @enc, "roman8";   # HP
354             }
355         } else {
356             push @enc, $_;
357             push @enc, "$_.UTF-8";
358         }
359     }
360     if ($^O eq 'os390') {
361         push @enc, qw(IBM-037 IBM-819 IBM-1047);
362     }
363
364     return @enc;
365 }
366
367 trylocale("C");
368 trylocale("POSIX");
369 foreach (0..15) {
370     trylocale("ISO8859-$_");
371     trylocale("iso8859$_");
372     trylocale("iso8859-$_");
373     trylocale("iso_8859_$_");
374     trylocale("isolatin$_");
375     trylocale("isolatin-$_");
376     trylocale("iso_latin_$_");
377 }
378
379 # Sanitize the environment so that we can run the external 'locale'
380 # program without the taint mode getting grumpy.
381
382 # $ENV{PATH} is special in VMS.
383 delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
384
385 # Other subversive stuff.
386 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
387
388 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
389     while (<LOCALES>) {
390         # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
391         # ain't great when we're running this testPERL_UNICODE= so that utf8
392         # locales will cause all IO hadles to default to (assume) utf8
393         next unless utf8::valid($_);
394         chomp;
395         trylocale($_);
396     }
397     close(LOCALES);
398 } elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
399 # The SYS$I18N_LOCALE logical name search list was not present on 
400 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
401     opendir(LOCALES, "SYS\$I18N_LOCALE:");
402     while ($_ = readdir(LOCALES)) {
403         chomp;
404         trylocale($_);
405     }
406     close(LOCALES);
407 } elsif ($^O eq 'openbsd' && -e '/usr/share/locale') {
408
409    # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
410    # is much easier and faster than the last resort method.
411
412     opendir(LOCALES, '/usr/share/locale');
413     while ($_ = readdir(LOCALES)) {
414         chomp;
415         trylocale($_);
416     }
417     close(LOCALES);
418 } else {
419
420     # This is going to be slow.
421
422     foreach my $locale (split(/\n/, $locales)) {
423         my ($locale_name, $language_codes, $country_codes, $encodings) =
424             split(/:/, $locale);
425         my @enc = decode_encodings($encodings);
426         foreach my $loc (split(/ /, $locale_name)) {
427             trylocale($loc);
428             foreach my $enc (@enc) {
429                 trylocale("$loc.$enc");
430             }
431             $loc = lc $loc;
432             foreach my $enc (@enc) {
433                 trylocale("$loc.$enc");
434             }
435         }
436         foreach my $lang (split(/ /, $language_codes)) {
437             trylocale($lang);
438             foreach my $country (split(/ /, $country_codes)) {
439                 my $lc = "${lang}_${country}";
440                 trylocale($lc);
441                 foreach my $enc (@enc) {
442                     trylocale("$lc.$enc");
443                 }
444                 my $lC = "${lang}_\U${country}";
445                 trylocale($lC);
446                 foreach my $enc (@enc) {
447                     trylocale("$lC.$enc");
448                 }
449             }
450         }
451     }
452 }
453
454 setlocale(LC_ALL, "C");
455
456 if ($^O eq 'darwin') {
457     # Darwin 8/Mac OS X 10.4 has bad Basque locales: perl bug #35895,
458     # Apple bug ID# 4139653. It also has a problem in Byelorussian.
459     if ($Config{osvers} ge '8' and $Config{osvers} lt '9') {
460         debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n";
461         @Locale = grep ! m/^(eu_ES|be_BY.CP1131$)/, @Locale;
462     }
463 }
464
465 @Locale = sort @Locale;
466
467 debug "# Locales =\n";
468 for ( @Locale ) {
469     debug "# $_\n";
470 }
471
472 my %Problem;
473 my %Okay;
474 my %Testing;
475 my @Neoalpha;
476 my %Neoalpha;
477
478 sub tryneoalpha {
479     my ($Locale, $i, $test) = @_;
480     unless ($test) {
481         $Problem{$i}{$Locale} = 1;
482         debug "# failed $i with locale '$Locale'\n";
483     } else {
484         push @{$Okay{$i}}, $Locale;
485     }
486 }
487
488 foreach $Locale (@Locale) {
489     debug "# Locale = $Locale\n";
490     @Alnum_ = getalnum_();
491     debug "# w = ", join("",@Alnum_), "\n";
492
493     unless (setlocale(LC_ALL, $Locale)) {
494         foreach (99..103) {
495             $Problem{$_}{$Locale} = -1;
496         }
497         next;
498     }
499
500     # Sieve the uppercase and the lowercase.
501     
502     my %UPPER = ();
503     my %lower = ();
504     my %BoThCaSe = ();
505     for (@Alnum_) {
506         if (/[^\d_]/) { # skip digits and the _
507             if (uc($_) eq $_) {
508                 $UPPER{$_} = $_;
509             }
510             if (lc($_) eq $_) {
511                 $lower{$_} = $_;
512             }
513         }
514     }
515     foreach (keys %UPPER) {
516         $BoThCaSe{$_}++ if exists $lower{$_};
517     }
518     foreach (keys %lower) {
519         $BoThCaSe{$_}++ if exists $UPPER{$_};
520     }
521     foreach (keys %BoThCaSe) {
522         delete $UPPER{$_};
523         delete $lower{$_};
524     }
525
526     debug "# UPPER    = ", join("", sort keys %UPPER   ), "\n";
527     debug "# lower    = ", join("", sort keys %lower   ), "\n";
528     debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
529
530     # Find the alphabets that are not alphabets in the default locale.
531
532     {
533         no locale;
534     
535         @Neoalpha = ();
536         for (keys %UPPER, keys %lower) {
537             push(@Neoalpha, $_) if (/\W/);
538             $Neoalpha{$_} = $_;
539         }
540     }
541
542     @Neoalpha = sort @Neoalpha;
543
544     debug "# Neoalpha = ", join("",@Neoalpha), "\n";
545
546     if (@Neoalpha == 0) {
547         # If we have no Neoalphas the remaining tests are no-ops.
548         debug "# no Neoalpha, skipping tests 99..102 for locale '$Locale'\n";
549         foreach (99..102) {
550             push @{$Okay{$_}}, $Locale;
551         }
552     } else {
553
554         # Test \w.
555     
556         my $word = join('', @Neoalpha);
557
558         my $badutf8;
559         {
560             local $SIG{__WARN__} = sub {
561                 $badutf8 = $_[0] =~ /Malformed UTF-8/;
562             };
563             $Locale =~ /utf-?8/i;
564         }
565
566         if ($badutf8) {
567             debug "# Locale name contains bad UTF-8, skipping test 99 for locale '$Locale'\n";
568         } elsif ($Locale =~ /utf-?8/i) {
569             debug "# unknown whether locale and Unicode have the same \\w, skipping test 99 for locale '$Locale'\n";
570             push @{$Okay{99}}, $Locale;
571         } else {
572             if ($word =~ /^(\w+)$/) {
573                 tryneoalpha($Locale, 99, 1);
574             } else {
575                 tryneoalpha($Locale, 99, 0);
576             }
577         }
578
579         # Cross-check the whole 8-bit character set.
580
581         for (map { chr } 0..255) {
582             tryneoalpha($Locale, 100,
583                         (/\w/ xor /\W/) ||
584                         (/\d/ xor /\D/) ||
585                         (/\s/ xor /\S/));
586         }
587
588         # Test for read-only scalars' locale vs non-locale comparisons.
589
590         {
591             no locale;
592             $a = "qwerty";
593             {
594                 use locale;
595                 tryneoalpha($Locale, 101, ($a cmp "qwerty") == 0);
596             }
597         }
598
599         {
600             my ($from, $to, $lesser, $greater,
601                 @test, %test, $test, $yes, $no, $sign);
602
603             for (0..9) {
604                 # Select a slice.
605                 $from = int(($_*@Alnum_)/10);
606                 $to = $from + int(@Alnum_/10);
607                 $to = $#Alnum_ if ($to > $#Alnum_);
608                 $lesser  = join('', @Alnum_[$from..$to]);
609                 # Select a slice one character on.
610                 $from++; $to++;
611                 $to = $#Alnum_ if ($to > $#Alnum_);
612                 $greater = join('', @Alnum_[$from..$to]);
613                 ($yes, $no, $sign) = ($lesser lt $greater
614                                       ? ("    ", "not ", 1)
615                                       : ("not ", "    ", -1));
616                 # all these tests should FAIL (return 0).
617                 # Exact lt or gt cannot be tested because
618                 # in some locales, say, eacute and E may test equal.
619                 @test = 
620                     (
621                      $no.'    ($lesser  le $greater)',  # 1
622                      'not      ($lesser  ne $greater)', # 2
623                      '         ($lesser  eq $greater)', # 3
624                      $yes.'    ($lesser  ge $greater)', # 4
625                      $yes.'    ($lesser  ge $greater)', # 5
626                      $yes.'    ($greater le $lesser )', # 7
627                      'not      ($greater ne $lesser )', # 8
628                      '         ($greater eq $lesser )', # 9
629                      $no.'     ($greater ge $lesser )', # 10
630                      'not (($lesser cmp $greater) == -($sign))' # 11
631                      );
632                 @test{@test} = 0 x @test;
633                 $test = 0;
634                 for my $ti (@test) {
635                     $test{$ti} = eval $ti;
636                     $test ||= $test{$ti}
637                 }
638                 tryneoalpha($Locale, 102, $test == 0);
639                 if ($test) {
640                     debug "# lesser  = '$lesser'\n";
641                     debug "# greater = '$greater'\n";
642                     debug "# lesser cmp greater = ",
643                           $lesser cmp $greater, "\n";
644                     debug "# greater cmp lesser = ",
645                           $greater cmp $lesser, "\n";
646                     debug "# (greater) from = $from, to = $to\n";
647                     for my $ti (@test) {
648                         debugf("# %-40s %-4s", $ti,
649                                $test{$ti} ? 'FAIL' : 'ok');
650                         if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
651                             debugf("(%s == %4d)", $1, eval $1);
652                         }
653                         debug "\n#";
654                     }
655
656                     last;
657                 }
658             }
659         }
660     }
661
662     use locale;
663
664     my ($x, $y) = (1.23, 1.23);
665
666     $a = "$x";
667     printf ''; # printf used to reset locale to "C"
668     $b = "$y";
669
670     debug "# 103..107: a = $a, b = $b, Locale = $Locale\n";
671
672     tryneoalpha($Locale, 103, $a eq $b);
673
674     my $c = "$x";
675     my $z = sprintf ''; # sprintf used to reset locale to "C"
676     my $d = "$y";
677
678     debug "# 104..107: c = $c, d = $d, Locale = $Locale\n";
679
680     tryneoalpha($Locale, 104, $c eq $d); 
681
682     {
683         use warnings;
684         my $w = 0;
685         local $SIG{__WARN__} =
686             sub {
687                 print "# @_\n";
688                 $w++;
689             };
690
691         # The == (among other ops) used to warn for locales
692         # that had something else than "." as the radix character.
693
694         tryneoalpha($Locale, 105, $c == 1.23);
695
696         tryneoalpha($Locale, 106, $c == $x);
697
698         tryneoalpha($Locale, 107, $c == $d);
699
700         {
701 #           no locale; # XXX did this ever work correctly?
702         
703             my $e = "$x";
704
705             debug "# 108..110: e = $e, Locale = $Locale\n";
706
707             tryneoalpha($Locale, 108, $e == 1.23);
708
709             tryneoalpha($Locale, 109, $e == $x);
710             
711             tryneoalpha($Locale, 110, $e == $c);
712         }
713         
714         my $f = "1.23";
715         my $g = 2.34;
716
717         debug "# 111..115: f = $f, g = $g, locale = $Locale\n";
718
719         tryneoalpha($Locale, 111, $f == 1.23);
720
721         tryneoalpha($Locale, 112, $f == $x);
722         
723         tryneoalpha($Locale, 113, $f == $c);
724
725         tryneoalpha($Locale, 114, abs(($f + $g) - 3.57) < 0.01);
726
727         tryneoalpha($Locale, 115, $w == 0);
728     }
729
730     # Does taking lc separately differ from taking
731     # the lc "in-line"?  (This was the bug 19990704.002, change #3568.)
732     # The bug was in the caching of the 'o'-magic.
733     {
734         use locale;
735
736         sub lcA {
737             my $lc0 = lc $_[0];
738             my $lc1 = lc $_[1];
739             return $lc0 cmp $lc1;
740         }
741
742         sub lcB {
743             return lc($_[0]) cmp lc($_[1]);
744         }
745
746         my $x = "ab";
747         my $y = "aa";
748         my $z = "AB";
749
750         tryneoalpha($Locale, 116,
751                     lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
752                     lcA($x, $z) == 0 && lcB($x, $z) == 0);
753     }
754
755     # Does lc of an UPPER (if different from the UPPER) match
756     # case-insensitively the UPPER, and does the UPPER match
757     # case-insensitively the lc of the UPPER.  And vice versa.
758     {
759         use locale;
760         no utf8;
761         my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
762
763         my @f = ();
764         foreach my $x (keys %UPPER) {
765             my $y = lc $x;
766             next unless uc $y eq $x;
767             print "# UPPER $x lc $y ",
768             $x =~ /$y/i ? 1 : 0, " ",
769             $y =~ /$x/i ? 1 : 0, "\n" if 0;
770             #
771             # If $x and $y contain regular expression characters
772             # AND THEY lowercase (/i) to regular expression characters,
773             # regcomp() will be mightily confused.  No, the \Q doesn't
774             # help here (maybe regex engine internal lowercasing
775             # is done after the \Q?)  An example of this happening is
776             # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
777             # the chr(173) (the "[") is the lowercase of the chr(235).
778             #
779             # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
780             # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
781             # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
782             # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
783             # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
784             # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
785             #
786             # Similar things can happen even under (bastardised)
787             # non-EBCDIC locales: in many European countries before the
788             # advent of ISO 8859-x nationally customised versions of
789             # ISO 646 were devised, reusing certain punctuation
790             # characters for modified characters needed by the
791             # country/language.  For example, the "|" might have
792             # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
793             #
794             if ($x =~ $re || $y =~ $re) {
795                 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
796                 next;
797             }
798             # With utf8 both will fail since the locale concept
799             # of upper/lower does not work well in Unicode.
800             push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
801
802             foreach my $x (keys %lower) {
803                 my $y = uc $x;
804                 next unless lc $y eq $x;
805                 print "# lower $x uc $y ",
806                 $x =~ /$y/i ? 1 : 0, " ",
807                 $y =~ /$x/i ? 1 : 0, "\n" if 0;
808                 if ($x =~ $re || $y =~ $re) { # See above.
809                     print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
810                     next;
811                 }
812                 # With utf8 both will fail since the locale concept
813                 # of upper/lower does not work well in Unicode.
814                 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
815             }
816             tryneoalpha($Locale, 117, @f == 0);
817             if (@f) {
818                 print "# failed 117 locale '$Locale' characters @f\n"
819             }
820         }
821     }
822 }
823
824 # Recount the errors.
825
826 foreach (&last_without_setlocale()+1..$last) {
827     if ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
828         if ($_ == 102) {
829             print "# The failure of test 102 is not necessarily fatal.\n";
830             print "# It usually indicates a problem in the environment,\n";
831             print "# not in Perl itself.\n";
832         }
833         print "not ";
834     }
835     print "ok $_\n";
836 }
837
838 # Give final advice.
839
840 my $didwarn = 0;
841
842 foreach (99..$last) {
843     if ($Problem{$_}) {
844         my @f = sort keys %{ $Problem{$_} };
845         my $f = join(" ", @f);
846         $f =~ s/(.{50,60}) /$1\n#\t/g;
847         print
848             "#\n",
849             "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
850             "#\t", $f, "\n#\n",
851             "# on your system may have errors because the locale test $_\n",
852             "# failed in ", (@f == 1 ? "that locale" : "those locales"),
853             ".\n";
854         print <<EOW;
855 #
856 # If your users are not using these locales you are safe for the moment,
857 # but please report this failure first to perlbug\@perl.com using the
858 # perlbug script (as described in the INSTALL file) so that the exact
859 # details of the failures can be sorted out first and then your operating
860 # system supplier can be alerted about these anomalies.
861 #
862 EOW
863         $didwarn = 1;
864     }
865 }
866
867 # Tell which locales were okay and which were not.
868
869 if ($didwarn) {
870     my (@s, @F);
871     
872     foreach my $l (@Locale) {
873         my $p = 0;
874         foreach my $t (102..$last) {
875             $p++ if $Problem{$t}{$l};
876         }
877         push @s, $l if $p == 0;
878       push @F, $l unless $p == 0;
879     }
880     
881     if (@s) {
882         my $s = join(" ", @s);
883         $s =~ s/(.{50,60}) /$1\n#\t/g;
884
885         warn
886             "# The following locales\n#\n",
887             "#\t", $s, "\n#\n",
888             "# tested okay.\n#\n",
889     } else {
890         warn "# None of your locales were fully okay.\n";
891     }
892
893     if (@F) {
894         my $F = join(" ", @F);
895         $F =~ s/(.{50,60}) /$1\n#\t/g;
896
897         warn
898           "# The following locales\n#\n",
899           "#\t", $F, "\n#\n",
900           "# had problems.\n#\n",
901     } else {
902         warn "# None of your locales were broken.\n";
903     }
904
905     if (@utf8locale) {
906         my $S = join(" ", @utf8locale);
907         $S =~ s/(.{50,60}) /$1\n#\t/g;
908     
909         warn "#\n# The following locales\n#\n",
910              "#\t", $S, "\n#\n",
911              "# were skipped for the tests ",
912              join(" ", sort {$a<=>$b} keys %utf8skip), "\n",
913             "# because UTF-8 and locales do not work together in Perl.\n#\n";
914     }
915 }
916
917 sub last { 117 }
918
919 # eof