--- name: Encode runtime: pp source: src/native/Encode/pp.rs --- ```{index} single: Encode; Perl module (pp runtime) ``` # Encode Native implementation of Encode 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 ## encode Encodes a Unicode string into the specified encoding, returning a byte string. ```perl use Encode 'encode'; my $bytes = encode('UTF-8', $string); my $bytes = encode('iso-8859-1', $string); ``` ## decode Decodes a byte string from the specified encoding into a Unicode string. ```perl use Encode 'decode'; my $string = decode('UTF-8', $bytes); ``` ## encode_utf8 Shorthand for `encode("UTF-8", $string)`. ```perl use Encode 'encode_utf8'; my $bytes = encode_utf8($string); ``` ## decode_utf8 Shorthand for `decode("UTF-8", $bytes)`. ```perl use Encode 'decode_utf8'; my $string = decode_utf8($bytes); ``` ## 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. ```perl use Encode 'find_encoding'; my $enc = find_encoding('UTF-8'); my $bytes = $enc->encode($string); ``` ## resolve_alias Resolves an encoding alias to its canonical name. Returns `undef` if the encoding is not recognized. ```perl use Encode 'resolve_alias'; my $name = resolve_alias('latin1'); # 'iso-8859-1' ``` ## is_utf8 Returns true if the string has the internal UTF-8 flag set. This is a Perl internals function, not a validity check. ## from_to Converts a string in-place from one encoding to another. Returns the length of the converted string, or `undef` on failure. ```perl use Encode 'from_to'; from_to($data, 'iso-8859-1', 'UTF-8'); ``` ## encodings Returns a list of available encoding names, optionally filtered by type.