MIME::Charset(3)
π NAME π
Bestie, meet MIME::Charset β the Perl module from the MIME::Charset(3) manual. Its whole thing is Charset Information for MIME. That's the vibe. π
β¨ SYNOPSIS β¨
Okay so first you load it up:
use MIME::Charset:
$charset = MIME::Charset->new("euc-jp");
πΈ Getting charset information
$benc = $charset->body_encoding; # e.g. "Q"
$cset = $charset->as_string; # e.g. "US-ASCII"
$henc = $charset->header_encoding; # e.g. "S"
$cset = $charset->output_charset; # e.g. "ISO-2022-JP"
π¦ Translating text data
($text, $charset, $encoding) =
$charset->header_encode(
"\xc9\xc2\xc5\xaa\xc0\xde\xc3\xef\xc5\xaa".
"\xc7\xd1\xca\xaa\xbd\xd0\xce\xcf\xb4\xef",
Charset => 'euc-jp');
# ...returns e.g. (<converted>, "ISO-2022-JP", "B").
($text, $charset, $encoding) =
$charset->body_encode(
"Collectioneur path\xe9tiquement ".
"\xe9clectique de d\xe9chets",
Charset => 'latin1');
# ...returns e.g. (<original>, "ISO-8859-1", "QUOTED-PRINTABLE").
$len = $charset->encoded_header_len(
"Perl\xe8\xa8\x80\xe8\xaa\x9e",
Charset => 'utf-8',
Encoding => "b");
# ...returns e.g. 28.
π Manipulating module defaults
MIME::Charset::alias("csEUCKR", "euc-kr");
MIME::Charset::default("iso-8859-1");
MIME::Charset::fallback("us-ascii");
β Non-OO functions (may be deprecated in near future)
use MIME::Charset qw(:info);
$benc = body_encoding("iso-8859-2"); # "Q"
$cset = canonical_charset("ANSI X3.4-1968"); # "US-ASCII"
$henc = header_encoding("utf-8"); # "S"
$cset = output_charset("shift_jis"); # "ISO-2022-JP"
use MIME::Charset qw(:trans);
($text, $charset, $encoding) =
header_encode(
"\xc9\xc2\xc5\xaa\xc0\xde\xc3\xef\xc5\xaa".
"\xc7\xd1\xca\xaa\xbd\xd0\xce\xcf\xb4\xef",
"euc-jp");
# ...returns (<converted>, "ISO-2022-JP", "B");
($text, $charset, $encoding) =
body_encode(
"Collectioneur path\xe9tiquement ".
"\xe9clectique de d\xe9chets",
"latin1");
# ...returns (<original>, "ISO-8859-1", "QUOTED-PRINTABLE");
$len = encoded_header_len(
"Perl\xe8\xa8\x80\xe8\xaa\x9e", "b", "utf-8"); # 28
π₯ DESCRIPTION π₯
MIME::Charset gives you info about character sets used for MIME messages on the Internet. Iconic, honestly. π
π§ Definitions
The charset is βcharacter setβ as MIME uses it: a method of converting a sequence of octets into a sequence of characters. It includes both the βcoded character setβ (CCS) and the βcharacter encoding schemeβ (CES) concepts from ISO/IEC.
The encoding is what MIME means when it talks about a method of representing a body part or a header body as sequence(s) of printable US-ASCII characters. Slay. π«
π Constructor
$charset = MIME::Charset->new([CHARSET [, OPTS]]) β Create a charset object. π«Ά
OPTS may accept the following key-value pair. NOTE: when Unicode/multibyte support is disabled (see "USE_ENCODE"), conversion will not be performed, so this option doesn't have any effects.
Mapping => MAPTYPEβ Whether to extend mappings actually used for charset names or not."EXTENDED"uses extended mappings."STANDARD"uses standardized strict mappings. Default is"EXTENDED".
π Getting Information of Charsets
$charset->body_encoding/body_encoding CHARSETβ Get the recommended transfer-encoding of CHARSET for the message body. The returned value will be one of"B"(BASE64),"Q"(QUOTED-PRINTABLE),"S"(shorter one of either), or"undef"(might not be transfer-encoded; either 7BIT or 8BIT). This may not be the same as encoding for the message header. π$charset->as_string/canonical_charset CHARSETβ Get the canonical name for charset. π$charset->decoderβ Get anEncode::Encodingobject to decode strings to Unicode by charset. If charset is not specified or not known by this module,undefwill be returned.$charset->dupβ Get a copy of the charset object. Duplicate era. β¨$charset->encoder([CHARSET])β Get anEncode::Encodingobject to encode a Unicode string using a compatible charset recommended to be used for messages on the Internet. If optional CHARSET is specified, replace the encoder (and output charset name) of the$charsetobject with those of CHARSET, therefore the$charsetobject will be a converter between the original charset and the new CHARSET.$charset->header_encoding/header_encoding CHARSETβ Get the recommended encoding scheme of CHARSET for the message header. The returned value will be one of"B","Q","S"(shorter one of either), or"undef"(might not be encoded). This may not be the same as encoding for the message body.$charset->output_charset/output_charset CHARSETβ Get a charset that is compatible with the given CHARSET and is recommended to be used for MIME messages on the Internet (if it is known by this module). When Unicode/multibyte support is disabled (see"USE_ENCODE"), this function will simply return the result of"canonical_charset".
π¦ Translating Text Data
$charset->body_encode(STRING [, OPTS])/body_encode STRING, CHARSET [, OPTS]β Get converted (if needed) data of STRING and the recommended transfer-encoding of that data for the message body. CHARSET is the charset by which STRING is encoded.
OPTS may accept the following key-value pairs. NOTE: when Unicode/multibyte support is disabled (see "USE_ENCODE"), conversion will not be performed, so these options do not have any effects.
Detect7bit => YESNOβ Try auto-detecting a 7-bit charset when CHARSET is not given. Default is"YES".Replacement => REPLACEMENTβ Specifies the error handling scheme. See"Error Handling".
A 3-item list of (converted string, charset for output, transfer-encoding) will be returned. Transfer-encoding will be either "BASE64", "QUOTED-PRINTABLE", "7BIT", or "8BIT". If the charset for output could not be determined and the converted string contains non-ASCII byte(s), the charset for output will be "undef" and transfer-encoding will be "BASE64". The charset for output will be "US-ASCII" if and only if the string does not contain any non-ASCII bytes. π
$charset->decode(STRING [,CHECK])β Decode STRING to Unicode. Note: when Unicode/multibyte support is disabled (see"USE_ENCODE"), this function will die. πdetect_7bit_charset STRINGβ Guess the 7-bit charset that may encode a string STRING. If STRING contains any 8-bit bytes,"undef"will be returned. Otherwise, Default Charset will be returned for an unknown charset.$charset->encode(STRING [, CHECK])β Encode STRING (Unicode or non-Unicode) using a compatible charset recommended to be used for messages on the Internet (if this module knows it). Note that the string will be decoded to Unicode then encoded even if the compatible charset was equal to the original charset. Note: when Unicode/multibyte support is disabled (see"USE_ENCODE"), this function will die. πΈ$charset->encoded_header_len(STRING [, ENCODING])/encoded_header_len STRING, ENCODING, CHARSETβ Get the length of encoded STRING for the message header (without folding). ENCODING may be one of"B","Q", or"S"(shorter one of either"B"or"Q").$charset->header_encode(STRING [, OPTS])/header_encode STRING, CHARSET [, OPTS]β Get converted (if needed) data of STRING and the recommended encoding scheme of that data for message headers. CHARSET is the charset by which STRING is encoded.
OPTS may accept the following key-value pairs. NOTE: when Unicode/multibyte support is disabled (see "USE_ENCODE"), conversion will not be performed, so these options do not have any effects.
Detect7bit => YESNOβ Try auto-detecting a 7-bit charset when CHARSET is not given. Default is"YES".Replacement => REPLACEMENTβ Specifies the error handling scheme. See"Error Handling".
A 3-item list of (converted string, charset for output, encoding scheme) will be returned. Encoding scheme will be either "B", "Q", or "undef" (might not be encoded). If the charset for output could not be determined and the converted string contains non-ASCII byte(s), the charset for output will be "8BIT" (this is not a charset name but a special value to represent unencodable data) and the encoding scheme will be "undef" (should not be encoded). The charset for output will be "US-ASCII" if and only if the string does not contain any non-ASCII bytes.
$charset->undecode(STRING [,CHECK])β Encode the Unicode string STRING to a byte string by the input charset of$charset. This is equivalent to$charset->decoder->encode(). Note: when Unicode/multibyte support is disabled (see"USE_ENCODE"), this function will die. π«Ά
π Manipulating Module Defaults
alias ALIAS [, CHARSET]β Get/set a charset alias for canonical names determined by"canonical_charset". If CHARSET is given and isn't false, ALIAS will be assigned as an alias of CHARSET. Otherwise, the alias won't be changed. In both cases, the current charset name that ALIAS is assigned will be returned.default [CHARSET]β Get/set the default charset. The default charset is used by this module when the charset context is unknown. Modules using this module are recommended to use this charset when the charset context is unknown or an implicit default is expected. By default, it is"US-ASCII". If CHARSET is given and isn't false, it will be set to the default charset. Otherwise, the default charset won't be changed. In both cases, the current default charset will be returned. NOTE: default charset should not be changed.fallback [CHARSET]β Get/set the fallback charset. The fallback charset is used by this module when conversion by the given charset is failed and the"FALLBACK"error handling scheme is specified. Modules using this module may use this charset as a last resort of charset for conversion. By default, it is"UTF-8". If CHARSET is given and isn't false, it will be set to the fallback charset. If CHARSET is"NONE", the fallback charset will be undefined. Otherwise, the fallback charset won't be changed. In any cases, the current fallback charset will be returned. NOTE: it is useful that"US-ASCII"is specified as fallback charset, since the result of conversion will be readable without charset information. πrecommended CHARSET [, HEADERENC, BODYENC [, ENCCHARSET]]β Get/set charset profiles."DEFAULT"β drops a substitution character right where the malformed character was. For UCM-based encodings,<subchar>is what gets used. β¨"FALLBACK"β tries the"DEFAULT"scheme but using the fallback charset (peep"fallback"for that). But likeβ¦ if the fallback charset is undefined and conversion errors out, the code will die on error with an error message. So, heads up. π"CROAK"β code dies on error immediately, with an error message. So you should trap that fatal error witheval{}unless you genuinely want it to die on error. Synonym is"STRICT". π"PERLQQ","HTMLCREF","XMLCREF"β use the"FB_PERLQQ","FB_HTMLCREF"or"FB_XMLCREF"scheme defined by the Encode module. π- numeric values β yes bestie, numbers are allowed too. For more deets see
"Handling Malformed Data"in Encode. π₯
If the error handling scheme isn't specified, or you specify some unknown scheme, then "DEFAULT" gets assumed. No cap. π«Ά
π οΈ Configuration File
Built-in defaults for the option parameters can be overridden by a configuration file: MIME/Charset/Defaults.pm. For more details, go read MIME/Charset/Defaults.pm.sample. It's giving "read the docs, bestie". π
π·οΈ VERSION
Consult the $VERSION variable. That's the vibe check. β
Development versions of this module may be found at <http://hatuka.nezumi.nu/repos/MIME-Charset/>.
π§ Incompatible Changes
Release 1.001
new()method returns an object when the CHARSET argument is not specified. Iconic behaviour shift, honestly. π
Release 1.005
- Restrict characters in encoded-word according to RFC 2047 section 5 (3). This also affects the return value of the
encoded_header_len()method. Lowkey a big deal if you were relying on the old thing. π
Release 1.008.2
body_encoding()method may also return"S". Slay. β¨- Return value of
body_encode()method for UTF-8 may include a"QUOTED-PRINTABLE"encoding item that in earlier versions was fixed to"BASE64". So likeβ¦ it's not always BASE64 anymore, periodt. π
π SEE ALSO
Multipurpose Internet Mail Extensions (MIME).
βοΈ AUTHOR
Hatuka*nezumi - IKEDA Soji <hatuka(at)nezumi.nu>
Β©οΈ COPYRIGHT
Copyright (C) 2006-2017 Hatuka*nezumi - IKEDA Soji. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
That's the whole thing, bestie β go encode some headers responsibly. π This page came from perl v5.42.0, dated 2022-08-13, and it's the MIME::Charset(3) manpage, so now you know exactly where to point the blame (affectionately) when you need to look something up again. π¦
π¦ where this page comes from
- opensuse-leap 16.0: perl-MIME-Charset 1.013.1-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms