Encode::PerlIO (3)
Leading comments
Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) Standard preamble: ========================================================================
NAME
Encode::PerlIO -- a detailed document on Encode and PerlIOOverview
It is very common to want to do encoding transformations when reading or writing files, network connections, pipes etc. If Perl is configured to use the new 'perlio'Here is how the blind poet would modernise the encoding:
use Encode; open(my $iliad,'<:encoding(iso-8859-7)','iliad.greek'); open(my $utf8,'>:utf8','iliad.utf8'); my @epic = <$iliad>; print $utf8 @epic; close($utf8); close($illiad);
In addition, the new
open(my $fh,'>:utf8','anything'); print $fh "Any \x{0021} string \N{SMILEY FACE}\n";
Either of the above forms of ``layer'' specifications can be made the default for a lexical scope with the "use open ..." pragma. See open.
Once a handle is open, its layers can be altered using "binmode".
Without any such configuration, or if Perl itself is built using the system's own
In other cases, it is the program's responsibility to transform characters into bytes using the
You can also use PerlIO to convert larger amounts of data you don't want to bring into memory. For example, to convert between
open(F, "<:encoding(iso-8859-1)", "data.txt") or die $!; open(G, ">:utf8", "data.utf") or die $!; while (<F>) { print G } # Could also do "print G <F>" but that would pull # the whole file into memory just to write it out again.
More examples:
open(my $f, "<:encoding(cp1252)") open(my $g, ">:encoding(iso-8859-2)") open(my $h, ">:encoding(latin9)") # iso-8859-15
See also encoding for how to change the default encoding of the data in your script.
How does it work?
Here is a crude diagram of how filehandle, PerlIO, and Encode interact.
filehandle <-> PerlIO PerlIO <-> scalar (read/printed) \ / Encode
When PerlIO receives data from either direction, it fills a buffer (currently with 1024 bytes) and passes the buffer to Encode. Encode tries to convert the valid part and passes it back to PerlIO, leaving invalid parts (usually a partial character) in the buffer. PerlIO then appends more data to the buffer, calls Encode again, and so on until the data stream ends.
To do so, PerlIO always calls (de|en)code methods with
A B C .... ~ \x{3000} .... 41 42 43 .... 7E e3 80 80 .... <- buffer ---------------> << encoded >>>>>>>>>> <- next buffer ------
Encode converts from the beginning to \x7E, leaving \xe3 in the buffer because it is invalid (partial character).
Unfortunately, this scheme does not work well with escape-based encodings such as
Line Buffering
Now let's see what happens when you try to decode from
JIS208-ESC \x{5f3e} A B C .... ~ \e $ B |DAN | .... 41 42 43 .... 7E 1b 24 41 43 46 .... <- buffer ---------------------------> << encoded >>>>>>>>>>>>>>>>>>>>>>>
As you see, the next buffer begins with \x43. But \x43 is 'C' in
Fortunately PerlIO also supports line buffer if you tell PerlIO to use one instead of fixed buffer. Since
To tell PerlIO to use line buffer, implement ->needs_lines method for your encoding object. See Encode::Encoding for details.
Thanks to these efforts most encodings that come with Encode support PerlIO but that still leaves following encodings.
iso-2022-kr MIME-B MIME-Header MIME-Q
Fortunately iso-2022-kr is hardly used (according to Jungshik) and MIME-* are very unlikely to be fed to PerlIO because they are for mail headers. See Encode::MIME::Header for details.
How can I tell whether my encoding fully supports PerlIO ?
As of this writing, any encoding whose class belongs to Encode::XS and Encode::Unicode works. The Encode module has a "perlio_ok" method which you can use before applying PerlIO encoding to the filehandle. Here is an example:
my $use_perlio = perlio_ok($enc); my $layer = $use_perlio ? "<:raw" : "<:encoding($enc)"; open my $fh, $layer, $file or die "$file : $!"; while(<$fh>){ $_ = decode($enc, $_) unless $use_perlio; # .... }