8

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";
    }
}
John Montgomery
  • 314
  • 5
  • 13

1 Answers1

18

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.

tchrist
  • 78,834
  • 30
  • 123
  • 180
  • 2
    The other issue is what encoding the script is in, because it uses ó as a literal character. – cjm Nov 19 '10 at 02:06
  • @cjm: Ug, you’re right. I just have been burned by `use encoding` before. I now have program text either in pure 7-bit ASCII or else in UTF-8 with a `use utf8` pragma, because I know *that* works. – tchrist Nov 19 '10 at 02:09
  • The first solution didn't work, and the second didn't, but if I do the second and the third, it works. I guess I'll just have to be satisfied with that for now. – John Montgomery Nov 19 '10 at 02:09
  • 1
    @John: I wouldn’t write `/ó/` unless your source is in utf8 and you’ve said so. I would write `/\xF3/`. There is still a problem with the accent: are you expecting it to match unaccented or differently accented characters? If so you’ll need to NFD and deMark it. – tchrist Nov 19 '10 at 02:12