What is the simplest way to make modules warnings fatal?
By means of "mastering perl" I've overwritten the "encode"-function of the "Encode"-module. Is there a shorter way to make the encode-utf8-warnings fatal?
#!/usr/bin/env perl
use warnings;
use 5.012;
binmode STDOUT, ':encoding(utf-8)';
BEGIN {
use Encod开发者_JAVA技巧e;
no warnings 'redefine';
*Encode::encode = sub ($$;$) {
my ( $name, $string, $check ) = @_;
return undef unless defined $string;
$string .= '' if ref $string;
$check ||= 0;
unless ( defined $name ) {
require Carp;
Carp::croak("Encoding name should not be undef");
}
my $enc = find_encoding($name);
unless ( defined $enc ) {
require Carp;
Carp::croak("Unknown encoding '$name'");
}
use warnings FATAL => 'utf8'; ###
my $octets = $enc->encode( $string, $check );
$_[1] = $string if $check and !ref $check and !( $check & LEAVE_SRC() );
return $octets;
}
}
use Encode qw(encode);
use warnings FATAL => 'utf8';
my $character;
{
no warnings 'utf8';
$character = "\x{ffff}";
# $character = "\x{263a}";
}
my $utf32;
eval { $utf32 = encode( 'utf-32', $character ) };
if ( $@ ) {
( my $error_message = $@ ) =~ s/\K\sin\ssubroutine.*$//;
chomp $error_message; # where does the newline come from?
say $error_message;
}
else {
my @a = unpack( '(B8)*', $utf32 );
printf "utf-32 encoded:\t%8s %8s %8s %8s %8s %8s %8s %8s\n", @a;
}
subquestion: where does the newline in $error_message after the s/// come from?
I'm not sure I follow your main question... use warnings FATAL => 'utf8';
is pretty short already; I don't think you're likely to find anything shorter.
As for the subquestion, .
in a regex will, by default, match any character except a newline, so that substitution doesn't remove the final newline:
$ perl -e '$foo = "foo bar baz\n"; $foo =~ s/bar.*$//; print $foo . "---\n";'
prints
foo
---
To get .
to match newlines, add the /s
modifier to your regex:
perl -e '$foo = "foo bar baz\n"; $foo =~ s/bar.*$//s; print $foo . "---\n";'
prints
foo ---
精彩评论