6 require Config; import Config;
8 require './charset_tools.pl';
13 # open::import expects 'open' as its first argument, but it clashes with open()
15 open::import( 'open', @_ );
18 # can't use require_ok() here, with a name like 'open'
19 ok( require 'open.pm', 'requiring open' );
23 like( $@, qr/needs explicit list of PerlIO layers/,
24 'import should fail without args' );
26 # prevent it from loading I18N::Langinfo, so we can test encoding failures
28 local $SIG{__WARN__} = sub {
32 # and it shouldn't be able to find this layer
34 eval q{ no warnings 'layer'; use open IN => ':macguffin' ; };
36 'should not warn about unknown layer with bad layer provided' );
39 eval q{ use warnings 'layer'; use open IN => ':macguffin' ; };
40 like( $warn, qr/Unknown PerlIO layer/,
41 'should warn about unknown layer with bad layer provided' );
43 # open :locale logic changed since open 1.04, new logic
44 # difficult to test portably.
46 # see if it sets the magic variables appropriately
47 import( 'IN', ':crlf' );
48 is( $^H{'open_IN'}, 'crlf', 'should have set crlf layer' );
50 # it should reset them appropriately, too
51 import( 'IN', ':raw' );
52 is( $^H{'open_IN'}, 'raw', 'should have reset to raw layer' );
54 # it dies if you don't set IN, OUT, or IO
55 eval { import( 'sideways', ':raw' ) };
56 like( $@, qr/Unknown PerlIO layer class/, 'should croak with unknown class' );
58 # but it handles them all so well together
59 import( 'IO', ':raw :crlf' );
60 is( ${^OPEN}, ":raw :crlf\0:raw :crlf",
61 'should set multi types, multi layer' );
62 is( $^H{'open_IO'}, 'crlf', 'should record last layer set in %^H' );
65 skip("no perlio, no :utf8", 12) unless (find PerlIO::Layer 'perlio');
73 is(ord(<I>), 0x100, ":utf8 single wide character round-trip");
78 @a = map { chr(1 << ($_ << 2)) } 0..5; # 0x1, 0x10, .., 0x100000
79 unshift @a, chr(0); # ... and a null byte in front just for fun
85 sysseek($_[0], 0, SEEK_CUR);
88 require bytes; # not use
92 open F, "<:utf8", "a";
96 ($c = sysread(F, $b, 1)) == 1 &&
99 systell(F) == ($a += bytes::length($b))
101 print '# ord($_) == ', ord($_), "\n";
102 print '# ord($b) == ', ord($b), "\n";
103 print '# length($b) == ', length($b), "\n";
104 print '# bytes::length($b) == ', bytes::length($b), "\n";
105 print '# systell(F) == ', systell(F), "\n";
106 print '# $a == ', $a, "\n";
107 print '# $c == ', $c, "\n";
114 "on :utf8 streams sysread() should work on characters, not bytes");
117 print '# ord($_) == ', ord($_), "\n";
118 print '# bytes::length($_) == ', bytes::length($_), "\n";
119 print '# systell(G) == ', systell(G), "\n";
120 print '# $a == ', $a, "\n";
121 print '# $c == ', $c, "\n";
126 syswrite => sub { syswrite G, shift; },
127 'syswrite len' => sub { syswrite G, shift, 1; },
128 'syswrite len pad' => sub {
129 my $temp = shift() . "\243";
130 syswrite G, $temp, 1; },
131 'syswrite off' => sub {
132 my $temp = "\351" . shift();
133 syswrite G, $temp, 1, 1; },
134 'syswrite off pad' => sub {
135 my $temp = "\351" . shift() . "\243";
136 syswrite G, $temp, 1, 1; },
139 foreach my $key (sort keys %actions) {
140 # syswrite() on should work on characters, not bytes
141 open G, ">:utf8", "b";
147 ($c = $actions{$key}($_)) == 1 &&
148 systell(G) == ($a += bytes::length($_))
157 "on :utf8 streams syswrite() should work on characters, not bytes");
159 open G, "<:utf8", "b";
163 ($c = sysread(G, $b, 1)) == 1 &&
165 ord($b) == ord($_) &&
166 systell(G) == ($a += bytes::length($_))
168 print '# ord($_) == ', ord($_), "\n";
169 print '# ord($b) == ', ord($b), "\n";
170 print '# length($b) == ', length($b), "\n";
171 print '# bytes::length($b) == ', bytes::length($b), "\n";
172 print '# systell(G) == ', systell(G), "\n";
173 print '# $a == ', $a, "\n";
174 print '# $c == ', $c, "\n";
181 "checking syswrite() output on :utf8 streams by reading it back in");
185 skip("no perlio", 1) unless (find PerlIO::Layer 'perlio');
186 skip("no Encode", 1) unless $Config{extensions} =~ m{\bEncode\b};
187 skip("EBCDIC platform doesnt have 'use encoding' used by open ':locale'", 1)
190 eval q[use Encode::Alias;use open ":std", ":locale"];
191 is($@, '', 'can use :std and :locale');
195 local $ENV{PERL_UNICODE};
196 delete $ENV{PERL_UNICODE};
198 $TODO = "Encode not working on EBCDIC" if $::IS_EBCDIC;
201 'use open q\:encoding(UTF-8)\, q-:std-;',
202 'use open q\:encoding(UTF-8)\;',
203 'if(($_ = <STDIN>) eq qq-\x{100}\n-) { print qq-stdin ok\n- }',
204 'else { print qq-got -, join(q q q, map ord, split//), "\n" }',
205 'print STDOUT qq-\x{fe}\n-;',
206 'print STDERR qq-\x{fe}\n-;',
208 stdin => byte_utf8a_to_utf8n("\xc4\x80") . "\n",
212 . byte_utf8a_to_utf8n("\xc3\xbe")
214 . byte_utf8a_to_utf8n("\xc3\xbe")
216 "use open without :std does not affect standard handles",
221 1 while unlink "utf8";
226 # the test cases beyond __DATA__ need to be executed separately
229 $ENV{LC_ALL} = 'nonexistent.euc';
230 eval { open::_get_locale_encoding() };
231 like( $@, qr/too ambiguous/, 'should die with ambiguous locale encoding' );
233 # the special :locale layer
234 $ENV{LC_ALL} = $ENV{LANG} = 'ru_RU.KOI8-R';
235 # the :locale will probe the locale environment variables like LANG
236 use open OUT => ':locale';
238 print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1
241 printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1