Skip to content

Instantly share code, notes, and snippets.

@munepom
Created October 11, 2018 08:28
Show Gist options
  • Save munepom/de77693623a13e240de4f03019dd0d3c to your computer and use it in GitHub Desktop.
Save munepom/de77693623a13e240de4f03019dd0d3c to your computer and use it in GitHub Desktop.
Perl の Encode::Guess を利用して、ファイルの文字エンコーディングを確認してくれるくん
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Encode::Guess qw/cp932 euc-jp 7bit-jis utf8/; #文字コードの候補を指定して use する
my $file = shift;
my $fh;
unless (open ($fh, '<:raw', $file)){ #文字コード指定なしで1回ファイルを開く
die "OPEN FAILED: $file, $!
";
}
my ($temp, $i);
while ($temp .= <$fh> and ++$i < 50){ #最大50行(実は49行)まで変数に保存
eof and last;
}
close ($fh);
my $genc = guess_encoding($temp);
printf 'Encoding of file %s is %s', $file, $genc->name;
print "
";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment