Skip to content

Commit 756c4a0

Browse files
committed
Module-Metadata: Fix UTF-8 BOM detection for non-ASCII
1 parent 367a64e commit 756c4a0

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

lib/Module/Metadata.pm

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,17 @@ sub _parse_version_expression {
498498
sub _handle_bom {
499499
my ($self, $fh, $filename) = @_;
500500

501+
my $UTF8_BOM;
502+
if ( "$]" >= 5.008 ) {
503+
504+
# Works on EBCDIC too
505+
$UTF8_BOM = "\x{FEFF}";
506+
utf8::encode($UTF8_BOM);
507+
}
508+
else {
509+
$UTF8_BOM = "\x{EF}\x{BB}\x{BF}";
510+
}
511+
501512
my $pos = tell $fh;
502513
return unless defined $pos;
503514

@@ -512,10 +523,10 @@ sub _handle_bom {
512523
elsif ( $buf eq "\x{FF}\x{FE}" ) {
513524
$encoding = 'UTF-16LE';
514525
}
515-
elsif ( $buf eq "\x{EF}\x{BB}" ) {
516-
$buf = ' ';
526+
elsif ( $buf eq substr($UTF8_BOM, 0, 2) ) {
527+
$buf = ' ' x (length($UTF8_BOM) - 2);
517528
$count = read $fh, $buf, length $buf;
518-
if ( defined $count and $count >= 1 and $buf eq "\x{BF}" ) {
529+
if ( defined $count and $count >= 1 and $buf eq substr($UTF8_BOM, 2) ) {
519530
$encoding = 'UTF-8';
520531
}
521532
}

0 commit comments

Comments
 (0)