#!/usr/local/bin/perl use 5.006002; use strict; use warnings; use Encode; use PerlIO::encoding; my $encoding = $ARGV[0] || 'utf8'; printf "Initial \$PerlIO::encoding::fallback is 0x%x\n", $PerlIO::encoding::fallback; $PerlIO::encoding::fallback |= Encode::FB_WARN(); my $data = "Feliz nuevo a\xF1o"; # 'Happy new year' in Spanish, ISO-8859-1 my $size = length $data; open my $fh, "<:encoding($encoding)", \$data or die "Failed to open data: $!"; binmode STDOUT, 'encoding(utf8)' or die "Failed to set outptu to utf8: $!"; while ( <$fh> ) { print; } my $at = $@; my $bang = $!; my $query = $?; print <<"EOD"; size = $size tell = @{[ tell $fh ]} eof is @{[ eof $fh ? 'true' : 'false' ]} \$@ = '$at' \$! = '$bang' \$? = $query EOD __END__ =head1 TITLE encoding - Investigate behavior of PerlIO with malformed characters =head1 SYNOPSIS encoding encoding latin1 =head1 OPTIONS None =head1 DETAILS This script is my attempt to investigate the behavior of the PerlIO encoding functionality in the presence of malformed characters. It attempts to read a piece of ISO-8859-1 encoded data using (by default) C<< '<:encoding(utf8)' >>, and reports the characters actually read, and various data about the state of the file after the read is completed. The actual encoding used can be specified on the command line. =head1 AUTHOR Thomas R. Wyant, III F =head1 COPYRIGHT AND LICENSE Copyright (C) 2010, Thomas R. Wyant, III This program is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For more details, see the full text of the licenses in the directory LICENSES. This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. =cut # ex: set textwidth=72 :