| 1 | #!./perl
|
|---|
| 2 |
|
|---|
| 3 | BEGIN {
|
|---|
| 4 | chdir 't' if -d 't';
|
|---|
| 5 | @INC = '../lib';
|
|---|
| 6 | push @INC, "::lib:$MacPerl::Architecture:" if $^O eq 'MacOS';
|
|---|
| 7 | require "../t/test.pl";
|
|---|
| 8 | skip_all("No perlio") unless (find PerlIO::Layer 'perlio');
|
|---|
| 9 | if (ord("A") == 193) {
|
|---|
| 10 | print "1..0 # Skip: EBCDIC\n";
|
|---|
| 11 | exit 0;
|
|---|
| 12 | }
|
|---|
| 13 | unless( eval { require Encode } ) {
|
|---|
| 14 | print "1..0 # Skip: No Encode\n";
|
|---|
| 15 | exit 0;
|
|---|
| 16 | }
|
|---|
| 17 | plan (9);
|
|---|
| 18 | import Encode qw(:fallback_all);
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | # $PerlIO::encoding = 0; # WARN_ON_ERR|PERLQQ;
|
|---|
| 22 |
|
|---|
| 23 | my $file = "fallback$$.txt";
|
|---|
| 24 |
|
|---|
| 25 | {
|
|---|
| 26 | my $message = '';
|
|---|
| 27 | local $SIG{__WARN__} = sub { $message = $_[0] };
|
|---|
| 28 | $PerlIO::encoding::fallback = Encode::PERLQQ;
|
|---|
| 29 | ok(open(my $fh,">encoding(iso-8859-1)",$file),"opened iso-8859-1 file");
|
|---|
| 30 | my $str = "\x{20AC}";
|
|---|
| 31 | print $fh $str,"0.02\n";
|
|---|
| 32 | close($fh);
|
|---|
| 33 | like($message, qr/does not map to iso-8859-1/o, "FB_WARN message");
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | open($fh,$file) || die "File cannot be re-opened";
|
|---|
| 37 | my $line = <$fh>;
|
|---|
| 38 | is($line,"\\x{20ac}0.02\n","perlqq escapes");
|
|---|
| 39 | close($fh);
|
|---|
| 40 |
|
|---|
| 41 | $PerlIO::encoding::fallback = Encode::HTMLCREF;
|
|---|
| 42 |
|
|---|
| 43 | ok(open(my $fh,">encoding(iso-8859-1)",$file),"opened iso-8859-1 file");
|
|---|
| 44 | my $str = "\x{20AC}";
|
|---|
| 45 | print $fh $str,"0.02\n";
|
|---|
| 46 | close($fh);
|
|---|
| 47 |
|
|---|
| 48 | open($fh,$file) || die "File cannot be re-opened";
|
|---|
| 49 | my $line = <$fh>;
|
|---|
| 50 | is($line,"€0.02\n","HTML escapes");
|
|---|
| 51 | close($fh);
|
|---|
| 52 |
|
|---|
| 53 | {
|
|---|
| 54 | no utf8;
|
|---|
| 55 | open($fh,">$file") || die "File cannot be re-opened";
|
|---|
| 56 | binmode($fh);
|
|---|
| 57 | print $fh "\xA30.02\n";
|
|---|
| 58 | close($fh);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | ok(open($fh,"<encoding(US-ASCII)",$file),"Opened as ASCII");
|
|---|
| 62 | my $line = <$fh>;
|
|---|
| 63 | printf "# %x\n",ord($line);
|
|---|
| 64 | is($line,"\\xA30.02\n","Escaped non-mapped char");
|
|---|
| 65 | close($fh);
|
|---|
| 66 |
|
|---|
| 67 | $PerlIO::encoding::fallback = Encode::WARN_ON_ERROR;
|
|---|
| 68 |
|
|---|
| 69 | ok(open($fh,"<encoding(US-ASCII)",$file),"Opened as ASCII");
|
|---|
| 70 | my $line = <$fh>;
|
|---|
| 71 | printf "# %x\n",ord($line);
|
|---|
| 72 | is($line,"\x{FFFD}0.02\n","Unicode replacement char");
|
|---|
| 73 | close($fh);
|
|---|
| 74 |
|
|---|
| 75 | END {
|
|---|
| 76 | 1 while unlink($file);
|
|---|
| 77 | }
|
|---|