I prefer the method Charles showed. But nevertheless your method is
interesting as well. 

Thanks for sharing, 
Manfred

On Sat, 14 Mar 2015 14:24:48 -0400
Brandon McCaig <bamcc...@gmail.com> wrote:

> On Sat, Mar 14, 2015 at 03:20:06AM -0700, Charles DeRykus wrote:
> > open my $fh, '<:encoding.... ) or die ...
> > 
> > {  open( local *STDERR,'>',\my $err);
> >    my $string = <$fh>;
> >     if ($err =~ /does not map to Unicode/) {
> >             # take action.....
> >      }
> > }
> 
> Here is an alternative approach..
> 
> use strict;
> use warnings;
> 
> use Encode qw/decode encode/;
> 
> my $file_name = './somefile';
> 
> sub create_file {
>     open my $fh, '>', $file_name or die "open: $!";
>     print $fh "ABC\n", chr(0x90), "\nDEF\n";
>     close $fh or warn "close: $!";
> }
> 
> sub read_or_die {
>     my ($fh) = @_;
>     my $bytes = <$fh>;
> 
>     # Emphasis on the Encode::FB_CROAK.
>     my $string = decode('UTF-8', $bytes, Encode::FB_CROAK());
> 
>     return $string;
> }
> 
> sub write_or_die {
>     my ($fh, $string) = @_;
> 
>     # Emphasis on the Encode::FB_CROAK.
>     my $bytes = encode('UTF-8', $string, Encode::FB_CROAK());
> 
>     print $fh $bytes;
> }
> 
> create_file();
> 
> print STDERR "Created file. Attempting to open it...\n";
> 
> open my $fh, '<', $file_name or die "open: $!";
> 
> print STDERR "Attempting to read it...\n";
> 
> eval {
>     while(my $line = read_or_die($fh)) {
>         write_or_die(\*STDOUT, $line);
>     }
> };
> 
> if ($@) {
>     print "CAUGHT ERROR: $@";
> }
> 
> close $fh or warn "close: $!";
> 
> __END__
> 
> Output:
> 
> Created file. Attempting to open it...
> Attempting to read it...
> ABC
> CAUGHT ERROR: utf8 "\x90" does not map to Unicode
> at /home/bambams/perl5/perlbrew/perls/stable/lib/5.18.0/x86_64-linux/Encode.pm
> line 176, <$fh> line 2.
> 
> It is a bit more painful having to call decode and encode
> ourselves, but I couldn't figure out any way to control the CHECK
> parameter used by PerlIO. The weird thing is that the
> documentation appears to assert that PerlIO is supposed to be
> using Encode::FB_CROAK() (1) too, but alas I guess that isn't the
> case if it doesn't croak().. That or the PerlIO layers are
> catching and handling the error differently...
> 





-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to