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, 4:21 PM


Views: 492
Re: [agentbb007] Catch Error Message

I like Laurent's idea, but I would prefer to locally modify the SIG hash to use a custom warnings handler. This way I can build a hash of bad files perhaps listing the bad line numbers, without effecting other warnings. These can be handled later, or a short circuiting system could be implemented via the likes of Try::Tiny. Along the lines of:


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

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

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

my $warnings = { };

for my $filepath ( @$filepaths )
{
local $SIG{__WARN__} = sub { utf8_warn( $filepath, $warnings, @_ ) };

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

while ( my $line = <$filehandle> )
{
# ...
}

close $filehandle;
}

print Dumper $warnings;

sub utf8_warn
{
my ( $filepath, $warnings, $message ) = @_;

if ( $message =~ /does not map to unicode/i )
{
my ( $line ) = $message =~ m/>\s*line\s*(\d+)\.$/; # your match may differ.
$line++;

$warnings->{$filepath}->{$line}++;
}
else
{
warn $message;
}

return 1;
}


Chris


(This post was edited by Zhris on Jun 20, 2018, 4:49 PM)


Edit Log:
Post edited by Zhris (Enthusiast) on Jun 20, 2018, 4:29 PM
Post edited by Zhris (Enthusiast) on Jun 20, 2018, 4:31 PM
Post edited by Zhris (Enthusiast) on Jun 20, 2018, 4:41 PM
Post edited by Zhris (Enthusiast) on Jun 20, 2018, 4:42 PM
Post edited by Zhris (Enthusiast) on Jun 20, 2018, 4:45 PM
Post edited by Zhris (Enthusiast) on Jun 20, 2018, 4:46 PM
Post edited by Zhris (Enthusiast) on Jun 20, 2018, 4:49 PM


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

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