Last active
December 11, 2015 07:19
-
-
Save oliveiraev/4565572 to your computer and use it in GitHub Desktop.
Check and remove Byte-order-mark.
Original here: http://rishida.net/blog/?p=102
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# program to remove a leading UTF-8 BOM from a file | |
# works both STDIN -> STDOUT and on the spot (with filename as argument) | |
if ($#ARGV > 0) { | |
print STDERR "Too many arguments!\n"; | |
exit; | |
} | |
my @file; # file content | |
my $lineno = 0; | |
my $filename = @ARGV[0]; | |
if ($filename) { | |
open( BOMFILE, $filename ) || die "Could not open source file for reading."; | |
while (<BOMFILE>) { | |
if ($lineno++ == 0) { | |
if ( index( $_, '' ) == 0 ) { | |
s/^\xEF\xBB\xBF//; | |
print "BOM found and removed.\n"; | |
} | |
else { print "No BOM found.\n"; } | |
} | |
push @file, $_ ; | |
} | |
close (BOMFILE) || die "Can't close source file after reading."; | |
open (NOBOMFILE, ">$filename") || die "Could not open source file for writing."; | |
foreach $line (@file) { | |
print NOBOMFILE $line; | |
} | |
close (NOBOMFILE) || die "Can't close source file after writing."; | |
} | |
else { # STDIN -> STDOUT | |
while (<>) { | |
if (!$lineno++) { | |
s/^\xEF\xBB\xBF//; | |
} | |
push @file, $_ ; | |
} | |
foreach $line (@file) { | |
print $line; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment