native_from_to#

Reencode a byte string in place from one encoding to another.

Synopsis#

from_to($octets, $from, $to);
from_to($octets, $from, $to, $check);

What you get back#

The number of characters in the reencoded result, or undef if the conversion could not complete. $octets itself is modified in place — after the call it holds raw bytes in the $to encoding and has no SVf_UTF8 flag.

Think of from_to as a bundled decode / encode pair where the intermediate character string is never exposed to Perl. That is sometimes what you want (less allocation, no flag juggling) and sometimes a trap (no way to intervene between the two halves).

Examples#

Convert a Latin-1 HTTP body to UTF-8 before writing:

my $body = read_file($path);             # raw bytes
from_to($body, 'iso-8859-1', 'UTF-8');
write_file($out, $body);

Unknown encodings croak — same as encode / decode.

Edge cases#

  • undef input returns undef and leaves $octets untouched.

  • $check is forwarded to both the decode and encode halves.

Differences from upstream#

Fully compatible with upstream for the registered encodings.