Why is the Perl coding level unaffected?

I need to read a file encoded in iso-8859-1.

For some reason, I cannot get the encoding layer (as described in PerlIO::encoding) to work. Here is a minimal example of what I'm doing.

test.txt contains one pound sign encoded in iso-8859-1.

% iconv -f iso-8859-1 test.txt
£

% hexdump -C test.txt
00000000  a3 0a                                             |..|
00000002

My Perl script:

#!/bin/perl

use warnings;
use strict;

open my $f, "<:encoding(iso-8859-1)", $ARGV[0] or die qq{Could not open $ARGV[0]: $!};

while (<$f>) {
  print;
}

Result:

% ./script.pl test.txt | hexdump -C
00000000  a3 0a                                             |..|
00000002

Thus, the script prints the exact sequence of bytes that it reads, without conversion performed.

+4
source share
2 answers

A string is a sequence of (32-bit or 64-bit) numbers.

, , Unicode. A3 Unicode U+00A3 iso-8859-1, decode("iso-8859-1", "\xA3") "\xA3".

, print("\xA3") A3 ( ).


, , , , iso-8859-1 UTF-8. ,

use open ':std', ':encoding(locale)';

use open ':std', ':encoding(UTF-8)';

STDIN, STDOUT STDERR ( binmode), open .

+4

, , , utf-8, , -, .

binmode(STDOUT, ":utf8");

.

+4

Source: https://habr.com/ru/post/1694426/


All Articles