9 skip_all_without_perlio();
10 # FIXME - more of these could be tested without Encode or full perl
11 skip_all_without_dynamic_extension('Encode');
13 # Makes testing easier.
14 $ENV{PERLIO} = 'stdio' if exists $ENV{PERLIO} && $ENV{PERLIO} eq '';
15 skip_all("PERLIO='$ENV{PERLIO}' unknown")
16 if exists $ENV{PERLIO} && $ENV{PERLIO} !~ /^(stdio|perlio|mmap)$/;
17 $PERLIO = exists $ENV{PERLIO} ? $ENV{PERLIO} : "(undef)";
22 my $DOSISH = $^O =~ /^(?:MSWin32|os2|dos|NetWare)$/ ? 1 : 0;
23 $DOSISH = 1 if !$DOSISH and $^O =~ /^uwin/;
24 my $NONSTDIO = exists $ENV{PERLIO} && $ENV{PERLIO} ne 'stdio' ? 1 : 0;
25 my $FASTSTDIO = $Config{d_faststdio} && $Config{usefaststdio} ? 1 : 0;
27 if (${^UNICODE} & 1) {
28 if (${^UNICODE} & 64) {
29 # Conditional on the locale
30 $UTF8_STDIN = ${^UTF8LOCALE};
38 my $NTEST = 55 - (($DOSISH || !$FASTSTDIO) ? 7 : 0) - ($DOSISH ? 7 : 0)
41 sub PerlIO::F_UTF8 () { 0x00008000 } # from perliol.h
48 # NONSTDIO = $NONSTDIO
49 # FASTSTDIO = $FASTSTDIO
50 # UNICODE = ${^UNICODE}
51 # UTF8LOCALE = ${^UTF8LOCALE}
52 # UTF8_STDIN = $UTF8_STDIN
57 my ($result, $expected, $id) = @_;
58 # An interesting dance follows where we try to make the following
59 # IO layer stack setups to compare equal:
61 # PERLIO UNIX-like DOS-like
63 # unset / "" unix perlio / stdio [1] unix crlf
64 # stdio unix perlio / stdio [1] stdio
65 # perlio unix perlio unix perlio
66 # mmap unix mmap unix mmap
68 # [1] "stdio" if Configure found out how to do "fast stdio" (depends
69 # on the stdio implementation) and in Perl 5.8, otherwise "unix perlio"
73 shift @$result if $result->[0] eq "unix";
74 # Change expectations.
76 $expected->[0] = $ENV{PERLIO};
78 $expected->[0] = $ENV{PERLIO} if $expected->[0] eq "stdio";
80 } elsif (!$FASTSTDIO && !$DOSISH) {
81 splice(@$result, 0, 2, "stdio")
83 $result->[0] eq "unix" &&
84 $result->[1] eq "perlio";
86 splice(@$result, 0, 2, "stdio")
88 $result->[0] eq "unix" &&
89 $result->[1] eq "crlf";
91 if ($DOSISH && grep { $_ eq 'crlf' } @$expected) {
92 # 5 tests potentially skipped because
93 # DOSISH systems already have a CRLF layer
94 # which will make new ones not stick.
95 splice @$expected, 1, 1 if $expected->[1] eq 'crlf';
97 my $n = scalar @$expected;
98 is(scalar @$result, $n, "$id - layers == $n");
99 for (my $i = 0; $i < $n; $i++) {
100 my $j = $expected->[$i];
101 if (ref $j eq 'CODE') {
102 ok($j->($result->[$i]), "$id - $i is ok");
104 is($result->[$i], $j,
105 sprintf("$id - $i is %s",
106 defined $j ? $j : "undef"));
111 check([ PerlIO::get_layers(STDIN) ],
112 $UTF8_STDIN ? [ "stdio", "utf8" ] : [ "stdio" ],
115 my $afile = tempfile();
116 open(F, ">:crlf", $afile);
118 check([ PerlIO::get_layers(F) ],
124 check([ PerlIO::get_layers(F) ],
128 binmode(F, ":encoding(cp1047)");
130 check([ PerlIO::get_layers(F) ],
131 [ qw[stdio crlf encoding(cp1047) utf8] ],
132 ":encoding(cp1047)");
136 check([ PerlIO::get_layers(F) ],
137 [ qw[stdio crlf encoding(cp1047) utf8 crlf utf8] ],
138 ":encoding(cp1047):crlf");
140 binmode(F, ":pop:pop");
142 check([ PerlIO::get_layers(F) ],
148 check([ PerlIO::get_layers(F) ],
154 check([ PerlIO::get_layers(F) ],
158 binmode(F, ":bytes");
160 check([ PerlIO::get_layers(F) ],
164 binmode(F, ":encoding(utf8)");
166 check([ PerlIO::get_layers(F) ],
167 [ qw[stdio encoding(utf8) utf8] ],
170 binmode(F, ":raw :crlf");
172 check([ PerlIO::get_layers(F) ],
176 binmode(F, ":raw :encoding(latin1)"); # "latin1" will be canonized
178 # 7 tests potentially skipped.
179 unless ($DOSISH || !$FASTSTDIO) {
180 my @results = PerlIO::get_layers(F, details => 1);
182 # Get rid of the args and the flags.
183 splice(@results, 1, 2) if $NONSTDIO;
186 [ "stdio", undef, sub { $_[0] > 0 },
187 "encoding", "iso-8859-1", sub { $_[0] & PerlIO::F_UTF8() } ],
188 ":raw:encoding(latin1)");
193 check([ PerlIO::get_layers(F) ],
200 binmode(F, ":encoding(utf8)");
201 is( $@, "foo", '$@ not clobbered by binmode and :encoding');
207 use open(IN => ":crlf", OUT => ":encoding(cp1252)");
212 check([ PerlIO::get_layers(F, input => 1) ],
216 check([ PerlIO::get_layers(G, output => 1) ],
217 [ qw[stdio encoding(cp1252) utf8] ],
224 # Check that PL_sigwarn's reference count is correct, and that
225 # &PerlIO::Layer::NoWarnings isn't prematurely freed.
226 fresh_perl_like (<<"EOT", qr/^CODE/);
227 open(UTF, "<:raw:encoding(utf8)", '$afile') or die \$!;
228 print ref *PerlIO::Layer::NoWarnings{CODE};