# perldoc > Encode::PerlIO

---
type: CommandReference
command: Encode::PerlIO
mode: perldoc
section: ""
source: perldoc
---

## Quick Reference
- `open($fh, '<:encoding(iso-8859-7)', $file)` — read file with encoding layer
- `open($fh, '>:utf8', $file)` — write file as UTF-8
- `open($fh, '<:raw', $file)` — no encoding layer
- `binmode($fh, ':encoding(cp1252)')` — alter encoding layer on open handle
- `use open ':encoding(utf8)'` — set default encoding for lexical scope
- `my $use_perlio = perlio_ok($enc);` — check if encoding supports PerlIO
- `print G <F>` — convert between encodings without loading entire file
- `while (<F>) { print G }` — convert line-by-line

## Name
Encode::PerlIO — a detailed document on Encode and PerlIO

## Synopsis
perl
use Encode;
open(my $in, '<:encoding(ENCODING)', 'file');
open(my $out, '>:utf8', 'file');
binmode($fh, ':encoding(ENCODING)');
use open ':encoding(utf8)';
## Options (Layers)
- `:encoding(ENCODING)` — read/write converting to/from specified encoding
- `:utf8` — efficient UTF-8 layer (equivalent to `:encoding(utf-8)`)
- `:raw` — no encoding transformation; handle bytes as-is
- `binmode(FILEHANDLE, LAYER)` — change layer on an open handle
- `use open ':encoding(ENCODING)'` — set default encoding for all I/O in scope
- `perlio_ok(ENCODING)` — returns true if encoding supports PerlIO layer

## Examples

### Reading with encoding
perl
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($iliad);
### Writing UTF-8
perl
open(my $fh, '>:utf8', 'anything');
print $fh "Any \x{0021} string \N{SMILEY FACE}\n";
### Converting between encodings line-by-line
perl
open(F, '<:encoding(iso-8859-1)', 'data.txt') or die $!;
open(G, '>:utf8', 'data.utf') or die $!;
while (<F>) { print G }
### Checking encoding support for PerlIO
perl
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;
  # ...
}
## How It Works
PerlIO buffers data (typically 1024 bytes) and passes it to Encode with CHECK set to 1. Encode converts valid portion, leaving partial characters in the buffer. This works for fixed-width encodings but fails for escape-based encodings like ISO-2022-JP. For those, implement `->needs_lines` method in encoding class to enable line buffering. Encodings that fully support PerlIO include those in `Encode::XS` and `Encode::Unicode`.

## See Also
- [Encode::Encoding](https://metacpan.org/pod/Encode::Encoding)
- [Encode::Supported](https://metacpan.org/pod/Encode::Supported)
- [Encode::PerlIO](https://metacpan.org/pod/Encode::PerlIO)
- [encoding](https://metacpan.org/pod/encoding)
- [perlebcdic](https://metacpan.org/pod/perlebcdic)
- [open in perlfunc](https://perldoc.perl.org/functions/open)
- [perlunicode](https://metacpan.org/pod/perlunicode)
- [utf8](https://metacpan.org/pod/utf8)
- The Perl Unicode Mailing List: perl-unicode@perl.org