1#!./perl -w 2 3BEGIN { 4 chdir 't' if -d 't'; 5 require "./test.pl"; 6 set_up_inc('../lib'); 7 require "./charset_tools.pl"; 8 skip_all_without_perlio(); 9} 10 11use Config; 12 13 14my $file = tempfile(); 15my $crlf = uni_to_native("\015\012"); 16my $crcr = uni_to_native("\x0d\x0d"); 17 18my $ungetc_count = 8200; # Somewhat over the likely buffer size 19 20{ 21 plan(tests => 21 + 2 * $ungetc_count); 22 ok(open(FOO,">:crlf",$file)); 23 ok(print FOO 'a'.((('a' x 14).qq{\n}) x 2000) || close(FOO)); 24 ok(open(FOO,"<:crlf",$file)); 25 26 my $text; 27 { local $/; $text = <FOO> } 28 is(count_chars($text, $crlf), 0); 29 is(count_chars($text, "\n"), 2000); 30 31 binmode(FOO); 32 seek(FOO,0,0); 33 { local $/; $text = <FOO> } 34 is(count_chars($text, $crlf), 2000); 35 36 SKIP: 37 { 38 skip_if_miniperl("miniperl can't rely on loading IO::Handle", 39 2 * $ungetc_count + 1); 40 my $fcontents = join "", map {"$_$crlf"} "a".."zzz"; 41 open my $fh, "<:crlf", \$fcontents; 42 local $/ = "xxx"; 43 local $_ = <$fh>; 44 my $pos = tell $fh; # pos must be behind "xxx", before "\nxxy\n" 45 seek $fh, $pos, 0; 46 $/ = "\n"; 47 $s = <$fh>.<$fh>; 48 is($s, "\nxxy\n"); 49 50 for my $i (0 .. $ungetc_count - 1) { 51 my $j = $i % 256; 52 is($fh->ungetc($j), $j, "ungetc of $j returns itself"); 53 } 54 55 for (my $i = $ungetc_count - 1; $i >= 0; $i--) { 56 my $j = $i % 256; 57 is(ord($fh->getc()), $j, "getc gets back $j"); 58 } 59 } 60 61 ok(close(FOO)); 62 63 # binmode :crlf should not cumulate. 64 # Try it first once and then twice so that even UNIXy boxes 65 # get to exercise this, for DOSish boxes even once is enough. 66 # Try also pushing :utf8 first so that there are other layers 67 # in between (this should not matter: CRLF layers still should 68 # not accumulate). 69 for my $utf8 ('', ':utf8') { 70 for my $binmode (1..2) { 71 open(FOO, ">$file"); 72 # require PerlIO; print PerlIO::get_layers(FOO), "\n"; 73 binmode(FOO, "$utf8:crlf") for 1..$binmode; 74 # require PerlIO; print PerlIO::get_layers(FOO), "\n"; 75 print FOO "Hello\n"; 76 close FOO; 77 open(FOO, "<$file"); 78 binmode(FOO); 79 my $foo = scalar <FOO>; 80 close FOO; 81 print join(" ", "#", map { sprintf("%02x", $_) } unpack("C*", $foo)), 82 "\n"; 83 like($foo, qr/$crlf$/); 84 unlike($foo, qr/$crcr/); 85 } 86 } 87 88 { 89 # check binmode removes :utf8 90 # 133604 - on Win32 :crlf is the base buffer layer, so 91 # binmode doesn't remove it, but the binmode handler didn't 92 # remove :utf8 either 93 ok(open(my $fh, ">", $file), "open a file"); 94 ok(binmode($fh, ":utf8"), "add :utf8"); 95 ok((() = grep($_ eq "utf8", PerlIO::get_layers($fh))), 96 "check :utf8 set"); 97 ok(binmode($fh), "remove :utf8"); 98 ok(!(() = grep($_ eq "utf8", PerlIO::get_layers($fh))), 99 "check :utf8 removed"); 100 close $fh; 101 } 102} 103 104sub count_chars { 105 my($text, $chars) = @_; 106 my $seen = 0; 107 $seen++ while $text =~ /$chars/g; 108 return $seen; 109} 110