use File::BOM qw( :all ); open $fh, '<', $filename; my ($encoding) = get_encoding_from_filehandle($fh);