9 unless (find PerlIO::Layer 'perlio') {
10 print "1..0 # Skip: not perlio\n";
14 if ($@ =~ /dynamic loading not available/) {
15 print "1..0 # miniperl cannot load Encode\n";
18 # Makes testing easier.
19 $ENV{PERLIO} = 'stdio' if exists $ENV{PERLIO} && $ENV{PERLIO} eq '';
20 if (exists $ENV{PERLIO} && $ENV{PERLIO} !~ /^(stdio|perlio|mmap)$/) {
21 # We are not prepared for anything else.
22 print "1..0 # PERLIO='$ENV{PERLIO}' unknown\n";
25 $PERLIO = exists $ENV{PERLIO} ? $ENV{PERLIO} : "(undef)";
30 my $DOSISH = $^O =~ /^(?:MSWin32|os2|dos|NetWare)$/ ? 1 : 0;
31 $DOSISH = 1 if !$DOSISH and $^O =~ /^uwin/;
32 my $NONSTDIO = exists $ENV{PERLIO} && $ENV{PERLIO} ne 'stdio' ? 1 : 0;
33 my $FASTSTDIO = $Config{d_faststdio} && $Config{usefaststdio} ? 1 : 0;
35 if (${^UNICODE} & 1) {
36 if (${^UNICODE} & 64) {
37 # Conditional on the locale
38 $UTF8_STDIN = ${^UTF8LOCALE};
46 my $NTEST = 44 - (($DOSISH || !$FASTSTDIO) ? 7 : 0) - ($DOSISH ? 5 : 0)
49 sub PerlIO::F_UTF8 () { 0x00008000 } # from perliol.h
56 # NONSTDIO = $NONSTDIO
57 # FASTSTDIO = $FASTSTDIO
58 # UNICODE = ${^UNICODE}
59 # UTF8LOCALE = ${^UTF8LOCALE}
60 # UTF8_STDIN = $UTF8_STDIN
64 # FIXME - more of these could be tested without Encode or full perl
65 skip("This perl does not have Encode", $NTEST)
66 unless " $Config{extensions} " =~ / Encode /;
67 skip("miniperl does not have Encode", $NTEST) if $ENV{PERL_CORE_MINITEST};
70 my ($result, $expected, $id) = @_;
71 # An interesting dance follows where we try to make the following
72 # IO layer stack setups to compare equal:
74 # PERLIO UNIX-like DOS-like
76 # unset / "" unix perlio / stdio [1] unix crlf
77 # stdio unix perlio / stdio [1] stdio
78 # perlio unix perlio unix perlio
79 # mmap unix mmap unix mmap
81 # [1] "stdio" if Configure found out how to do "fast stdio" (depends
82 # on the stdio implementation) and in Perl 5.8, otherwise "unix perlio"
86 shift @$result if $result->[0] eq "unix";
87 # Change expectations.
89 $expected->[0] = $ENV{PERLIO};
91 $expected->[0] = $ENV{PERLIO} if $expected->[0] eq "stdio";
93 } elsif (!$FASTSTDIO && !$DOSISH) {
94 splice(@$result, 0, 2, "stdio")
96 $result->[0] eq "unix" &&
97 $result->[1] eq "perlio";
99 splice(@$result, 0, 2, "stdio")
101 $result->[0] eq "unix" &&
102 $result->[1] eq "crlf";
104 if ($DOSISH && grep { $_ eq 'crlf' } @$expected) {
105 # 5 tests potentially skipped because
106 # DOSISH systems already have a CRLF layer
107 # which will make new ones not stick.
108 @$expected = grep { $_ ne 'crlf' } @$expected;
110 my $n = scalar @$expected;
111 is(scalar @$result, $n, "$id - layers == $n");
112 for (my $i = 0; $i < $n; $i++) {
113 my $j = $expected->[$i];
114 if (ref $j eq 'CODE') {
115 ok($j->($result->[$i]), "$id - $i is ok");
117 is($result->[$i], $j,
118 sprintf("$id - $i is %s",
119 defined $j ? $j : "undef"));
124 check([ PerlIO::get_layers(STDIN) ],
125 $UTF8_STDIN ? [ "stdio", "utf8" ] : [ "stdio" ],
128 my $afile = tempfile();
129 open(F, ">:crlf", $afile);
131 check([ PerlIO::get_layers(F) ],
135 binmode(F, ":encoding(cp1047)");
137 check([ PerlIO::get_layers(F) ],
138 [ qw[stdio crlf encoding(cp1047) utf8] ],
139 ":encoding(cp1047)");
143 check([ PerlIO::get_layers(F) ],
149 check([ PerlIO::get_layers(F) ],
155 check([ PerlIO::get_layers(F) ],
159 binmode(F, ":bytes");
161 check([ PerlIO::get_layers(F) ],
165 binmode(F, ":encoding(utf8)");
167 check([ PerlIO::get_layers(F) ],
168 [ qw[stdio encoding(utf8) utf8] ],
171 binmode(F, ":raw :crlf");
173 check([ PerlIO::get_layers(F) ],
177 binmode(F, ":raw :encoding(latin1)"); # "latin1" will be canonized
179 # 7 tests potentially skipped.
180 unless ($DOSISH || !$FASTSTDIO) {
181 my @results = PerlIO::get_layers(F, details => 1);
183 # Get rid of the args and the flags.
184 splice(@results, 1, 2) if $NONSTDIO;
187 [ "stdio", undef, sub { $_[0] > 0 },
188 "encoding", "iso-8859-1", sub { $_[0] & PerlIO::F_UTF8() } ],
189 ":raw:encoding(latin1)");
194 check([ PerlIO::get_layers(F) ],
201 use open(IN => ":crlf", OUT => ":encoding(cp1252)");
206 check([ PerlIO::get_layers(F, input => 1) ],
210 check([ PerlIO::get_layers(G, output => 1) ],
211 [ qw[stdio encoding(cp1252) utf8] ],
218 # Check that PL_sigwarn's reference count is correct, and that
219 # &PerlIO::Layer::NoWarnings isn't prematurely freed.
220 fresh_perl_like (<<"EOT", qr/^CODE/);
221 open(UTF, "<:raw:encoding(utf8)", '$afile') or die \$!;
222 print ref *PerlIO::Layer::NoWarnings{CODE};