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", 1) unless (find PerlIO::Layer 'perlio');
66 skip("no Encode", 1) unless $Config{extensions} =~ m{\bEncode\b};
67 skip("EBCDIC platform doesnt have 'use encoding' used by open ':locale'", 1)
70 eval q[use Encode::Alias;use open ":std", ":locale"];
71 is($@, '', 'can use :std and :locale');
75 local $ENV{PERL_UNICODE};
76 delete $ENV{PERL_UNICODE};
78 $TODO = "Encode not working on EBCDIC" if $::IS_EBCDIC;
81 'use open q\:encoding(UTF-8)\, q-:std-;',
82 'use open q\:encoding(UTF-8)\;',
83 'if(($_ = <STDIN>) eq qq-\x{100}\n-) { print qq-stdin ok\n- }',
84 'else { print qq-got -, join(q q q, map ord, split//), "\n" }',
85 'print STDOUT qq-\x{fe}\n-;',
86 'print STDERR qq-\x{fe}\n-;',
88 stdin => byte_utf8a_to_utf8n("\xc4\x80") . "\n",
92 . byte_utf8a_to_utf8n("\xc3\xbe")
94 . byte_utf8a_to_utf8n("\xc3\xbe")
96 "use open without :std does not affect standard handles",
101 1 while unlink "utf8";
106 # the test cases beyond __DATA__ need to be executed separately
109 $ENV{LC_ALL} = 'nonexistent.euc';
110 eval { open::_get_locale_encoding() };
111 like( $@, qr/too ambiguous/, 'should die with ambiguous locale encoding' );
113 # the special :locale layer
114 $ENV{LC_ALL} = $ENV{LANG} = 'ru_RU.KOI8-R';
115 # the :locale will probe the locale environment variables like LANG
116 use open OUT => ':locale';
118 print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1
121 printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1