Search code examples
perlencodinginputlatin1

How to read in ISO 8859-1 (Latin-1) encoded text in Perl


So I'm trying to write a perl script to read in a file encoded in Latin-1. For some reason, this just isn't working out. When I try to do a simple search for a character that I know is in the file (it's in the first line), nothing shows up. I'm using use encoding "iso 8859-1"; below, but I've also tried binmode(STDIN, ":utf8");. Any suggestions on what I might be doing wrong, and how to make it right?

use encoding "iso 8859-1";

while(<>)
{
    if(/ó/gi)
    {
    print "Found one!\n";
    }
}

Solution

  • Don’t use the use encoding pragma: it’s broken.

    Either specify the encoding here:

    use open ":encoding(Latin1)";
    

    or put it in the open itself:

    open(FH, "< :encoding(Latin1)", $pathname)
       || die "can't open $pathname: $!";
    

    or binmode it after opening:

    binmode(FH, ":encoding(Latin1)")
       || die "can't binmode to encoding Latin1";
    

    If you’re using <ARGV>, then use open is probably easiest.

    Don’t forget to set the encoding on your output streams, too.