On Sat, 14 Mar 2015 03:20:06 -0700 Charles DeRykus <dery...@gmail.com> wrote:
> On Sat, Mar 14, 2015 at 2:38 AM, Manfred Lotz <manfred.l...@arcor.de> > wrote: > > Hi all, > > I wanted to test what happens if Perl encounters an error when > > reading a utf8 encoded file. > > > > Here a minimal example: > > > > #! /usr/bin/perl > > > > use strict; > > use warnings; > > > > my $fname = $ARGV[0]; > > > > open my $fh, '<:encoding(UTF-8)', $fname > > or die "Couldn't open file: $fname"; > > > > my $string = <$fh>; > > close $fh; > > > > print "Reaching the end\n"; > > > > > > Running it on a file where I had inserted a hex \x90 gives the > > following error message which is fine. > > > > utf8 "\x90" does not map to Unicode at ./read_utf8.pl line 11. > > Reaching the end > > > > > > Question: The error above goes to stderr which basically is ok. > > Hovever, I want to take action in my script if such an error > > occurs. How do I notice it programmatically? > > > > > > open my $fh, '<:encoding.... ) or die ... > > { open( local *STDERR,'>',\my $err); > my $string = <$fh>; > if ($err =~ /does not map to Unicode/) { > # take action..... > } > } > > It seems I have to do it this way. Not what I wanted but it works nicely. Thanks a lot. -- Manfred -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/