CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Re: [agentbb007] Catch Error Message: Edit Log



Zhris
Enthusiast

Jun 20, 2018, 6:24 PM


Views: 472
Re: [agentbb007] Catch Error Message

No problem,

In case its useful to you, here is an example more along the lines of Laurent's suggestion of trapping fatal warnings. It simply slurps the entire file ( easily modified to buffer if too large ), then pushes good and bad filepaths into individual arrays:


Code
use strict; 
use warnings;
use Try::Tiny;
use Data::Dumper;

$| = 1;
#binmode STDOUT, ':utf8';

my $filepaths_check =
[ qw/
one.txt
two.txt
three.txt
/ ];

my $filepaths_ok = [ ];
my $filepaths_nok = [ ];

{
use warnings FATAL => qw/utf8/;
local $/ = undef;

for my $filepath ( @$filepaths_check )
{
open my $filehandle, '<:encoding(UTF-8)', $filepath or die "cannot open '$filepath': $!";

try
{
<$filehandle>;
}
catch
{
die unless /does not map to unicode/i;
}
finally
{
my $ref = @_ ? $filepaths_nok : $filepaths_ok ;
push @$ref, $filepath;

close $filehandle;
}
}
}

print Dumper $filepaths_ok, $filepaths_nok;


Another option is to use Encode to try to decode utf8 sequences, errors can again be trapped and managed.

Chris


(This post was edited by Zhris on Jun 21, 2018, 5:03 AM)


Edit Log:
Post edited by Zhris (Enthusiast) on Jun 21, 2018, 5:03 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives