4 unless (find PerlIO::Layer 'perlio') {
5 print "1..0 # Skip: not perlio\n";
8 unless (eval { require Encode } ) {
9 print "1..0 # Skip: not Encode\n";
20 my $russki = "koi8r$$";
21 my $threebyte = "3byte$$";
23 if (open(GRK, ">$grk")) {
24 binmode(GRK, ":bytes");
25 # alpha beta gamma in ISO 8859-7
26 print GRK "\xe1\xe2\xe3";
27 close GRK or die "Could not close: $!";
31 open(my $i,'<:encoding(iso-8859-7)',$grk);
33 open(my $o,'>:utf8',$utf);
35 print $o readline($i);
37 close($o) or die "Could not close: $!";
41 if (open(UTF, "<$utf")) {
42 binmode(UTF, ":bytes");
43 if (ord('A') == 193) { # EBCDIC
44 # alpha beta gamma in UTF-EBCDIC Unicode (0x3b1 0x3b2 0x3b3)
45 print "not " unless <UTF> eq "\xb4\x58\xb4\x59\xb4\x62";
47 # alpha beta gamma in UTF-8 Unicode (0x3b1 0x3b2 0x3b3)
48 print "not " unless <UTF> eq "\xce\xb1\xce\xb2\xce\xb3";
56 open(my $i,'<:utf8',$utf);
58 open(my $o,'>:encoding(iso-8859-7)',$grk);
60 print $o readline($i);
62 close($o) or die "Could not close: $!";
66 if (open(GRK, "<$grk")) {
67 binmode(GRK, ":bytes");
68 print "not " unless <GRK> eq "\xe1\xe2\xe3";
73 $SIG{__WARN__} = sub {$warn .= $_[0]};
75 if (open(FAIL, ">:encoding(NoneSuch)", $fail1)) {
76 print "not ok 9 # Open should fail\n";
81 print "not ok 10 # warning is undef\n";
82 } elsif ($warn =~ /^Cannot find encoding "NoneSuch" at/) {
85 print "not ok 10 # warning is '$warn'";
88 if (open(RUSSKI, ">$russki")) {
89 print RUSSKI "\x3c\x3f\x78";
90 close RUSSKI or die "Could not close: $!";
91 open(RUSSKI, "$russki");
92 binmode(RUSSKI, ":raw");
94 read(RUSSKI, $buf1, 1);
96 binmode(RUSSKI, ":encoding(koi8-r)");
98 read(RUSSKI, $buf2, 1);
99 my $offset = tell(RUSSKI);
100 if (ord($buf1) == 0x3c &&
101 ord($buf2) == (ord('A') == 193) ? 0x6f : 0x3f &&
105 printf "not ok 11 # [%s] [%s] %d\n",
106 join(" ", unpack("H*", $buf1)),
107 join(" ", unpack("H*", $buf2)), $offset;
111 print "not ok 11 # open failed: $!\n";
116 # Check there is no Use of uninitialized value in concatenation (.) warning
117 # due to the way @latin2iso_num was used to make aliases.
118 if (open(FAIL, ">:encoding(latin42)", $fail2)) {
119 print "not ok 12 # Open should fail\n";
123 if (!defined $warn) {
124 print "not ok 13 # warning is undef\n";
125 } elsif ($warn =~ /^Cannot find encoding "latin42" at.*line \d+\.$/) {
128 print "not ok 13 # warning is: \n";
133 # Create a string of chars that are 3 bytes in UTF-8
134 my $str = "\x{1f80}" x 2048;
136 # Write them to a file
137 open(F,'>:utf8',$threebyte) || die "Cannot open $threebyte:$!";
141 # Read file back as UTF-8
142 open(F,'<:encoding(utf-8)',$threebyte) || die "Cannot open $threebyte:$!";
145 print "not " unless ($dstr eq $str);
148 # Try decoding some bad stuff
149 open(F,'>:raw',$threebyte) || die "Cannot open $threebyte:$!";
150 if (ord('A') == 193) { # EBCDIC
151 print F "foo\x8c\x80\x80\x80bar\n\x80foo\n";
153 print F "foo\xF0\x80\x80\x80bar\n\x80foo\n";
157 open(F,'<:encoding(utf-8)',$threebyte) || die "Cannot open $threebyte:$!";
158 $dstr = join(":", <F>);
160 if (ord('A') == 193) { # EBCDIC
161 print "not " unless $dstr eq "foo\\x8C\\x80\\x80\\x80bar\n:\\x80foo\n";
163 print "not " unless $dstr eq "foo\\xF0\\x80\\x80\\x80bar\n:\\x80foo\n";
168 1 while unlink($grk, $utf, $fail1, $fail2, $russki, $threebyte);