.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Encoding::FixLatin 3pm" .TH Encoding::FixLatin 3pm 2024-03-05 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Encoding::FixLatin \- takes mixed encoding input and produces UTF\-8 output .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Encoding::FixLatin qw(fix_latin); \& \& my $utf8_string = fix_latin($mixed_encoding_string); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Most encoding conversion tools take input in one encoding and produce output in another encoding. This module takes input which may contain characters in more than one encoding and makes a best effort to convert them all to UTF\-8 output. .SH EXPORTS .IX Header "EXPORTS" Nothing is exported by default. The only public function is \f(CW\*(C`fix_latin\*(C'\fR which will be exported on request (as per SYNOPSIS). .SH FUNCTIONS .IX Header "FUNCTIONS" .SS "fix_latin( string, options ... )" .IX Subsection "fix_latin( string, options ... )" Decodes the supplied 'string' and returns a UTF\-8 version of the string. The following rules are used: .IP \(bu 4 ASCII characters (single bytes in the range 0x00 \- 0x7F) are passed through unchanged. .IP \(bu 4 Well-formed UTF\-8 multi-byte characters are also passed through unchanged. .IP \(bu 4 UTF\-8 multi-byte character which are over-long but otherwise well-formed are converted to the shortest UTF\-8 normal form. .IP \(bu 4 Bytes in the range 0xA0 \- 0xFF are assumed to be Latin\-1 characters (ISO8859\-1 encoded) and are converted to UTF\-8. .IP \(bu 4 Bytes in the range 0x80 \- 0x9F are assumed to be Win\-Latin\-1 characters (CP1252 encoded) and are converted to UTF\-8. Except for the five bytes in this range which are not defined in CP1252 (see the \f(CW\*(C`ascii_hex\*(C'\fR option below). .PP The achilles heel of these rules is that it's possible for certain combinations of two consecutive Latin\-1 characters to be misinterpreted as a single UTF\-8 character \- ie: there is some risk of data corruption. See the 'LIMITATIONS' section below to quantify this risk for the type of data you're working with. .PP If you pass in a string that is already a UTF\-8 character string (the utf8 flag is set on the Perl scalar) then the string will simply be returned unchanged. However if the 'bytes_only' option is specified (see below), the returned string will be a byte string rather than a character string. The rules described above will not be applied in either case. .PP The \f(CW\*(C`fix_latin\*(C'\fR function accepts options as name => value pairs. Recognised options are: .IP "bytes_only => 1/0" 4 .IX Item "bytes_only => 1/0" The value returned by fix_latin is normally a Perl character string and will have the utf8 flag set if it contains non-ASCII characters. If you set the \&\f(CW\*(C`bytes_only\*(C'\fR option to a true value, the returned string will be a binary string of UTF\-8 bytes. The utf8 flag will not be set. This is useful if you're going to immediately use the string in an IO operation and wish to avoid the overhead of converting to and from Perl's internal representation. .IP "ascii_hex => 1/0" 4 .IX Item "ascii_hex => 1/0" Bytes in the range 0x80\-0x9F are assumed to be CP1252, however CP1252 does not define a mapping for 5 of these bytes (0x81, 0x8D, 0x8F, 0x90 and 0x9D). Use this option to specify how they should be handled: .RS 4 .IP \(bu 4 If the ascii_hex option is set to true (the default), these bytes will be converted to 3 character ASCII hex strings of the form \f(CW%XX\fR. For example the byte 0x81 will become \f(CW%81\fR. .IP \(bu 4 If the ascii_hex option is set to false, these bytes will be treated as Latin\-1 control characters and converted to the equivalent UTF\-8 multi-byte sequences. .RE .RS 4 .Sp When processing text strings you will almost certainly never encounter these bytes at all. The most likely reason you would see them is if a malicious attacker was feeding random bytes to your application. It is difficult to conceive of a scenario in which it makes sense to change this option from its default setting. .RE .IP "overlong_fatal => 1/0" 4 .IX Item "overlong_fatal => 1/0" An over-long UTF\-8 byte sequence is one which uses more than the minimum number of bytes required to represent the character. Use this option to specify how overlong sequences should be handled. .RS 4 .IP \(bu 4 If the overlong_fatal option is set to false (the default) over-long sequences will be converted to the shortest normal UTF\-8 sequence. For example the input byte string "\exC0\exBCscript>" would be converted to "