Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Encode

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides character encoding/decoding functions compatible with Perl’s Encode module. Since PetaPerl does not support XS, this module is implemented directly in Rust.

Synopsis

use Encode qw(encode decode find_encoding);
my $bytes  = encode('UTF-8', $string);
my $string = decode('UTF-8', $bytes);
# Encoding object interface
my $enc = find_encoding('iso-8859-1');
my $bytes = $enc->encode($string);
# Shorthand for UTF-8
use Encode qw(encode_utf8 decode_utf8);
my $bytes  = encode_utf8($string);
my $string = decode_utf8($bytes);

Perl Equivalent

Encode

Functions

decode

Decodes a byte string from the specified encoding into a Unicode string.

use Encode 'decode';
my $string = decode('UTF-8', $bytes);

decode_utf8

Shorthand for decode("UTF-8", $bytes).

use Encode 'decode_utf8';
my $string = decode_utf8($bytes);

encode

Encodes a Unicode string into the specified encoding, returning a byte string.

use Encode 'encode';
my $bytes = encode('UTF-8', $string);
my $bytes = encode('iso-8859-1', $string);

encode_utf8

Shorthand for encode("UTF-8", $string).

use Encode 'encode_utf8';
my $bytes = encode_utf8($string);

encodings

Returns a list of available encoding names, optionally filtered by type.

find_encoding

Returns an encoding object for the given name, or undef if the encoding is not recognized. The object supports ->encode() and ->decode() methods.

use Encode 'find_encoding';
my $enc = find_encoding('UTF-8');
my $bytes = $enc->encode($string);

from_to

Converts a string in-place from one encoding to another. Returns the length of the converted string, or undef on failure.

use Encode 'from_to';
from_to($data, 'iso-8859-1', 'UTF-8');

is_utf8

Returns true if the string has the internal UTF-8 flag set. This is a Perl internals function, not a validity check.

resolve_alias

Resolves an encoding alias to its canonical name. Returns undef if the encoding is not recognized.

use Encode 'resolve_alias';
my $name = resolve_alias('latin1');   # 'iso-8859-1'