if (substr($text, 0, 4) eq "\x00\x00\xFE\xFF") { utf32be } elsif (substr($text, 0, 4) eq "\xFF\xFE\x00\x00") { utf32le } elsif (substr($text, 0, 2) eq "\xFE\xFF" ) { utf16be } elsif (substr($text, 0, 2) eq "\xFF\xFE" ) { utf16le } elsif (substr($text, 0, 3) eq "\xEF\xBB\xBF" ) { utf8 } else { No BOM found. Might not be UTF. Use another method to guess the encoding. }