03534b84651ce7a8635643c10fcca0503e8787ab
[urisagit/Perl-Docs.git] / t / binmode.t
1 #!/usr/local/bin/perl -w
2
3 use strict ;
4 use Test::More ;
5 use Carp ;
6 use File::Slurp ;
7
8 BEGIN {
9         plan skip_all => 'Older Perl lacking unicode support'
10                 if $] < 5.008001 ;
11 }
12
13 plan tests => 2 ;
14
15 my $suf = 'utf8' ;
16 my $mode = ":$suf" ;
17
18 my $is_win32 = $^O =~ /win32/i ;
19
20 my $orig_text = "\x{20ac}\n" ;
21 ( my $win32_text = $orig_text ) =~ s/\n/\015\012/ ;
22 my $unicode_length = length $orig_text ;
23
24 my $control_file = "control.$suf" ;
25 my $slurp_file = "slurp.$suf" ;
26
27 open( my $fh, ">$mode", $control_file ) or
28         die "cannot create control unicode file '$control_file' $!" ;
29 print $fh $orig_text ;
30 close $fh ;
31
32 my $slurp_utf = read_file( $control_file, binmode => $mode ) ;
33 my $written_text = $is_win32 ? $win32_text : $orig_text ;
34 is( $slurp_utf, $written_text, "read_file of $mode file" ) ;
35
36 # my $slurp_utf_length = length $slurp_utf ;
37 # my $slurp_text = read_file( $control_file ) ;
38 # my $slurp_text_length = length $slurp_text ;
39 # print "LEN UTF $slurp_utf_length TXT $slurp_text_length\n" ;
40
41 write_file( $slurp_file, {binmode => $mode}, $orig_text ) ;
42
43 open( $fh, "<$mode", $slurp_file ) or
44         die "cannot open slurp test file '$slurp_file' $!" ;
45 my $read_length = read( $fh, my $utf_text, $unicode_length ) ;
46 close $fh ;
47
48 is( $utf_text, $orig_text, "write_file of $mode file" ) ;
49
50 unlink( $control_file, $slurp_file ) ;