CINXE.COM
Encode - character encodings in Perl - Perldoc Browser
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Encode - character encodings in Perl - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/Encode"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.24.3</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/Encode">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/Encode">5.40.1</a> <a class="dropdown-item" href="/5.40.0/Encode">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/Encode">5.38.3</a> <a class="dropdown-item" href="/5.38.2/Encode">5.38.2</a> <a class="dropdown-item" href="/5.38.1/Encode">5.38.1</a> <a class="dropdown-item" href="/5.38.0/Encode">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/Encode">5.36.3</a> <a class="dropdown-item" href="/5.36.2/Encode">5.36.2</a> <a class="dropdown-item" href="/5.36.1/Encode">5.36.1</a> <a class="dropdown-item" href="/5.36.0/Encode">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/Encode">5.34.3</a> <a class="dropdown-item" href="/5.34.2/Encode">5.34.2</a> <a class="dropdown-item" href="/5.34.1/Encode">5.34.1</a> <a class="dropdown-item" href="/5.34.0/Encode">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/Encode">5.32.1</a> <a class="dropdown-item" href="/5.32.0/Encode">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/Encode">5.30.3</a> <a class="dropdown-item" href="/5.30.2/Encode">5.30.2</a> <a class="dropdown-item" href="/5.30.1/Encode">5.30.1</a> <a class="dropdown-item" href="/5.30.0/Encode">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/Encode">5.28.3</a> <a class="dropdown-item" href="/5.28.2/Encode">5.28.2</a> <a class="dropdown-item" href="/5.28.1/Encode">5.28.1</a> <a class="dropdown-item" href="/5.28.0/Encode">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/Encode">5.26.3</a> <a class="dropdown-item" href="/5.26.2/Encode">5.26.2</a> <a class="dropdown-item" href="/5.26.1/Encode">5.26.1</a> <a class="dropdown-item" href="/5.26.0/Encode">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/Encode">5.24.4</a> <a class="dropdown-item active" href="/5.24.3/Encode">5.24.3</a> <a class="dropdown-item" href="/5.24.2/Encode">5.24.2</a> <a class="dropdown-item" href="/5.24.1/Encode">5.24.1</a> <a class="dropdown-item" href="/5.24.0/Encode">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/Encode">5.22.4</a> <a class="dropdown-item" href="/5.22.3/Encode">5.22.3</a> <a class="dropdown-item" href="/5.22.2/Encode">5.22.2</a> <a class="dropdown-item" href="/5.22.1/Encode">5.22.1</a> <a class="dropdown-item" href="/5.22.0/Encode">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/Encode">5.20.3</a> <a class="dropdown-item" href="/5.20.2/Encode">5.20.2</a> <a class="dropdown-item" href="/5.20.1/Encode">5.20.1</a> <a class="dropdown-item" href="/5.20.0/Encode">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/Encode">5.18.4</a> <a class="dropdown-item" href="/5.18.3/Encode">5.18.3</a> <a class="dropdown-item" href="/5.18.2/Encode">5.18.2</a> <a class="dropdown-item" href="/5.18.1/Encode">5.18.1</a> <a class="dropdown-item" href="/5.18.0/Encode">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/Encode">5.16.3</a> <a class="dropdown-item" href="/5.16.2/Encode">5.16.2</a> <a class="dropdown-item" href="/5.16.1/Encode">5.16.1</a> <a class="dropdown-item" href="/5.16.0/Encode">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/Encode">5.14.4</a> <a class="dropdown-item" href="/5.14.3/Encode">5.14.3</a> <a class="dropdown-item" href="/5.14.2/Encode">5.14.2</a> <a class="dropdown-item" href="/5.14.1/Encode">5.14.1</a> <a class="dropdown-item" href="/5.14.0/Encode">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/Encode">5.12.5</a> <a class="dropdown-item" href="/5.12.4/Encode">5.12.4</a> <a class="dropdown-item" href="/5.12.3/Encode">5.12.3</a> <a class="dropdown-item" href="/5.12.2/Encode">5.12.2</a> <a class="dropdown-item" href="/5.12.1/Encode">5.12.1</a> <a class="dropdown-item" href="/5.12.0/Encode">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/Encode">5.10.1</a> <a class="dropdown-item" href="/5.10.0/Encode">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/Encode">5.8.9</a> <a class="dropdown-item" href="/5.8.8/Encode">5.8.8</a> <a class="dropdown-item" href="/5.8.7/Encode">5.8.7</a> <a class="dropdown-item" href="/5.8.6/Encode">5.8.6</a> <a class="dropdown-item" href="/5.8.5/Encode">5.8.5</a> <a class="dropdown-item" href="/5.8.4/Encode">5.8.4</a> <a class="dropdown-item" href="/5.8.3/Encode">5.8.3</a> <a class="dropdown-item" href="/5.8.2/Encode">5.8.2</a> <a class="dropdown-item" href="/5.8.1/Encode">5.8.1</a> <a class="dropdown-item" href="/5.8.0/Encode">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/Encode">5.6.2</a> <a class="dropdown-item" href="/5.6.1/Encode">5.6.1</a> <a class="dropdown-item" href="/5.6.0/Encode">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/Encode">5.005_04</a> <a class="dropdown-item" href="/5.005_03/Encode">5.005_03</a> <a class="dropdown-item" href="/5.005_02/Encode">5.005_02</a> <a class="dropdown-item" href="/5.005_01/Encode">5.005_01</a> <a class="dropdown-item" href="/5.005/Encode">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/Encode">blead</a> <a class="dropdown-item" href="/5.41.9/Encode">5.41.9</a> <a class="dropdown-item" href="/5.41.8/Encode">5.41.8</a> <a class="dropdown-item" href="/5.41.7/Encode">5.41.7</a> <a class="dropdown-item" href="/5.41.6/Encode">5.41.6</a> <a class="dropdown-item" href="/5.41.5/Encode">5.41.5</a> <a class="dropdown-item" href="/5.41.4/Encode">5.41.4</a> <a class="dropdown-item" href="/5.41.3/Encode">5.41.3</a> <a class="dropdown-item" href="/5.41.2/Encode">5.41.2</a> <a class="dropdown-item" href="/5.41.1/Encode">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/Encode">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/Encode">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/Encode">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/Encode">5.39.10</a> <a class="dropdown-item" href="/5.39.9/Encode">5.39.9</a> <a class="dropdown-item" href="/5.39.8/Encode">5.39.8</a> <a class="dropdown-item" href="/5.39.7/Encode">5.39.7</a> <a class="dropdown-item" href="/5.39.6/Encode">5.39.6</a> <a class="dropdown-item" href="/5.39.5/Encode">5.39.5</a> <a class="dropdown-item" href="/5.39.4/Encode">5.39.4</a> <a class="dropdown-item" href="/5.39.3/Encode">5.39.3</a> <a class="dropdown-item" href="/5.39.2/Encode">5.39.2</a> <a class="dropdown-item" href="/5.39.1/Encode">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/Encode">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.24.3/perl">Perl</a> <a class="dropdown-item" href="/5.24.3/perlintro">Intro</a> <a class="dropdown-item" href="/5.24.3/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.24.3/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.24.3/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.3/perlop">Operators</a> <a class="dropdown-item" href="/5.24.3/functions">Functions</a> <a class="dropdown-item" href="/5.24.3/variables">Variables</a> <a class="dropdown-item" href="/5.24.3/modules">Modules</a> <a class="dropdown-item" href="/5.24.3/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.3/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.24.3/perlcommunity">Community</a> <a class="dropdown-item" href="/5.24.3/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.24.3/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.24.3/Encode">Encode</a> <div id="more"> (<a href="/5.24.3/Encode.txt">source</a>, <a href="https://metacpan.org/pod/Encode">CPAN</a>) </div> <div id="moduleversion">version 2.80_01</div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.24.3. <a href="/Encode">View the latest version</a> </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#SYNOPSIS">SYNOPSIS</a> <ul> <li> <a class="text-decoration-none" href="#Table-of-Contents">Table of Contents</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> <ul> <li> <a class="text-decoration-none" href="#TERMINOLOGY">TERMINOLOGY</a> <ul> <li> <a class="text-decoration-none" href="#character">character</a> </li> <li> <a class="text-decoration-none" href="#byte">byte</a> </li> <li> <a class="text-decoration-none" href="#octet">octet</a> </li> </ul> </li> </ul> </li> <li> <a class="text-decoration-none" href="#THE-PERL-ENCODING-API">THE PERL ENCODING API</a> <ul> <li> <a class="text-decoration-none" href="#Basic-methods">Basic methods</a> <ul> <li> <a class="text-decoration-none" href="#encode">encode</a> </li> <li> <a class="text-decoration-none" href="#decode">decode</a> </li> <li> <a class="text-decoration-none" href="#find_encoding">find_encoding</a> </li> <li> <a class="text-decoration-none" href="#from_to">from_to</a> </li> <li> <a class="text-decoration-none" href="#encode_utf8">encode_utf8</a> </li> <li> <a class="text-decoration-none" href="#decode_utf8">decode_utf8</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#Listing-available-encodings">Listing available encodings</a> </li> <li> <a class="text-decoration-none" href="#Defining-Aliases">Defining Aliases</a> </li> <li> <a class="text-decoration-none" href="#Finding-IANA-Character-Set-Registry-names">Finding IANA Character Set Registry names</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#Encoding-via-PerlIO">Encoding via PerlIO</a> </li> <li> <a class="text-decoration-none" href="#Handling-Malformed-Data">Handling Malformed Data</a> <ul> <li> <a class="text-decoration-none" href="#List-of-CHECK-values">List of CHECK values</a> <ul> <li> <a class="text-decoration-none" href="#FB_DEFAULT">FB_DEFAULT</a> </li> <li> <a class="text-decoration-none" href="#FB_CROAK">FB_CROAK</a> </li> <li> <a class="text-decoration-none" href="#FB_QUIET">FB_QUIET</a> </li> <li> <a class="text-decoration-none" href="#FB_WARN">FB_WARN</a> </li> <li> <a class="text-decoration-none" href="#FB_PERLQQ-FB_HTMLCREF-FB_XMLCREF">FB_PERLQQ FB_HTMLCREF FB_XMLCREF</a> </li> <li> <a class="text-decoration-none" href="#The-bitmask">The bitmask</a> </li> <li> <a class="text-decoration-none" href="#LEAVE_SRC">LEAVE_SRC</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#coderef-for-CHECK">coderef for CHECK</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#Defining-Encodings">Defining Encodings</a> </li> <li> <a class="text-decoration-none" href="#The-UTF8-flag">The UTF8 flag</a> <ul> <li> <a class="text-decoration-none" href="#Messing-with-Perl's-Internals">Messing with Perl's Internals</a> <ul> <li> <a class="text-decoration-none" href="#is_utf8">is_utf8</a> </li> <li> <a class="text-decoration-none" href="#_utf8_on">_utf8_on</a> </li> <li> <a class="text-decoration-none" href="#_utf8_off">_utf8_off</a> </li> </ul> </li> </ul> </li> <li> <a class="text-decoration-none" href="#UTF-8-vs.-utf8-vs.-UTF8">UTF-8 vs. utf8 vs. UTF8</a> </li> <li> <a class="text-decoration-none" href="#SEE-ALSO">SEE ALSO</a> </li> <li> <a class="text-decoration-none" href="#MAINTAINER">MAINTAINER</a> </li> <li> <a class="text-decoration-none" href="#COPYRIGHT">COPYRIGHT</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>Encode - character encodings in Perl</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code>use Encode qw(decode encode); $characters = decode('UTF-8', $octets, Encode::FB_CROAK); $octets = encode('UTF-8', $characters, Encode::FB_CROAK);</code></pre> <h2 id="Table-of-Contents"><a class="permalink" href="#Table-of-Contents">#</a><a id="Table"></a>Table of Contents</h2> <p>Encode consists of a collection of modules whose details are too extensive to fit in one document. This one itself explains the top-level APIs and general topics at a glance. For other topics and more details, see the documentation for these modules:</p> <dl> <dt id="Encode::Alias-Alias-definitions-to-encodings"><a class="permalink" href="#Encode::Alias-Alias-definitions-to-encodings">#</a><a id="Encode"></a><a id="Encode::Alias---Alias-definitions-to-encodings"></a><a href="/5.24.3/Encode::Alias">Encode::Alias</a> - Alias definitions to encodings</dt> <dd> </dd> <dt id="Encode::Encoding-Encode-Implementation-Base-Class"><a class="permalink" href="#Encode::Encoding-Encode-Implementation-Base-Class">#</a><a id="Encode1"></a><a id="Encode::Encoding---Encode-Implementation-Base-Class"></a><a href="/5.24.3/Encode::Encoding">Encode::Encoding</a> - Encode Implementation Base Class</dt> <dd> </dd> <dt id="Encode::Supported-List-of-Supported-Encodings"><a class="permalink" href="#Encode::Supported-List-of-Supported-Encodings">#</a><a id="Encode2"></a><a id="Encode::Supported---List-of-Supported-Encodings"></a><a href="/5.24.3/Encode::Supported">Encode::Supported</a> - List of Supported Encodings</dt> <dd> </dd> <dt id="Encode::CN-Simplified-Chinese-Encodings"><a class="permalink" href="#Encode::CN-Simplified-Chinese-Encodings">#</a><a id="Encode3"></a><a id="Encode::CN---Simplified-Chinese-Encodings"></a><a href="/5.24.3/Encode::CN">Encode::CN</a> - Simplified Chinese Encodings</dt> <dd> </dd> <dt id="Encode::JP-Japanese-Encodings"><a class="permalink" href="#Encode::JP-Japanese-Encodings">#</a><a id="Encode4"></a><a id="Encode::JP---Japanese-Encodings"></a><a href="/5.24.3/Encode::JP">Encode::JP</a> - Japanese Encodings</dt> <dd> </dd> <dt id="Encode::KR-Korean-Encodings"><a class="permalink" href="#Encode::KR-Korean-Encodings">#</a><a id="Encode5"></a><a id="Encode::KR---Korean-Encodings"></a><a href="/5.24.3/Encode::KR">Encode::KR</a> - Korean Encodings</dt> <dd> </dd> <dt id="Encode::TW-Traditional-Chinese-Encodings"><a class="permalink" href="#Encode::TW-Traditional-Chinese-Encodings">#</a><a id="Encode6"></a><a id="Encode::TW---Traditional-Chinese-Encodings"></a><a href="/5.24.3/Encode::TW">Encode::TW</a> - Traditional Chinese Encodings</dt> <dd> </dd> </dl> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p>The <code>Encode</code> module provides the interface between Perl strings and the rest of the system. Perl strings are sequences of <i>characters</i>.</p> <p>The repertoire of characters that Perl can represent is a superset of those defined by the Unicode Consortium. On most platforms the ordinal values of a character as returned by <code>ord(<i>S</i>)</code> is the <i>Unicode codepoint</i> for that character. The exceptions are platforms where the legacy encoding is some variant of EBCDIC rather than a superset of ASCII; see <a href="/5.24.3/perlebcdic">perlebcdic</a>.</p> <p>During recent history, data is moved around a computer in 8-bit chunks, often called "bytes" but also known as "octets" in standards documents. Perl is widely used to manipulate data of many types: not only strings of characters representing human or computer languages, but also "binary" data, being the machine's representation of numbers, pixels in an image, or just about anything.</p> <p>When Perl is processing "binary data", the programmer wants Perl to process "sequences of bytes". This is not a problem for Perl: because a byte has 256 possible values, it easily fits in Perl's much larger "logical character".</p> <p>This document mostly explains the <i>how</i>. <a href="/5.24.3/perlunitut">perlunitut</a> and <a href="/5.24.3/perlunifaq">perlunifaq</a> explain the <i>why</i>.</p> <h2 id="TERMINOLOGY"><a class="permalink" href="#TERMINOLOGY">#</a>TERMINOLOGY</h2> <h3 id="character"><a class="permalink" href="#character">#</a>character</h3> <p>A character in the range 0 .. 2**32-1 (or more); what Perl's strings are made of.</p> <h3 id="byte"><a class="permalink" href="#byte">#</a>byte</h3> <p>A character in the range 0..255; a special case of a Perl character.</p> <h3 id="octet"><a class="permalink" href="#octet">#</a>octet</h3> <p>8 bits of data, with ordinal values 0..255; term for bytes passed to or from a non-Perl context, such as a disk file, standard I/O stream, database, command-line argument, environment variable, socket etc.</p> <h1 id="THE-PERL-ENCODING-API"><a class="permalink" href="#THE-PERL-ENCODING-API">#</a><a id="THE"></a>THE PERL ENCODING API</h1> <h2 id="Basic-methods"><a class="permalink" href="#Basic-methods">#</a><a id="Basic"></a>Basic methods</h2> <h3 id="encode"><a class="permalink" href="#encode">#</a>encode</h3> <pre><code>$octets = encode(ENCODING, STRING[, CHECK])</code></pre> <p>Encodes the scalar value <i>STRING</i> from Perl's internal form into <i>ENCODING</i> and returns a sequence of octets. <i>ENCODING</i> can be either a canonical name or an alias. For encoding names and aliases, see <a href="#Defining-Aliases">"Defining Aliases"</a>. For CHECK, see <a href="#Handling-Malformed-Data">"Handling Malformed Data"</a>.</p> <p>For example, to convert a string from Perl's internal format into ISO-8859-1, also known as Latin1:</p> <pre><code>$octets = encode("iso-8859-1", $string);</code></pre> <p><b>CAVEAT</b>: When you run <code>$octets = encode("utf8", $string)</code>, then $octets <i>might not be equal to</i> $string. Though both contain the same data, the UTF8 flag for $octets is <i>always</i> off. When you encode anything, the UTF8 flag on the result is always off, even when it contains a completely valid utf8 string. See <a href="#The-UTF8-flag">"The UTF8 flag"</a> below.</p> <p>If the $string is <code>undef</code>, then <code>undef</code> is returned.</p> <h3 id="decode"><a class="permalink" href="#decode">#</a>decode</h3> <pre><code>$string = decode(ENCODING, OCTETS[, CHECK])</code></pre> <p>This function returns the string that results from decoding the scalar value <i>OCTETS</i>, assumed to be a sequence of octets in <i>ENCODING</i>, into Perl's internal form. As with encode(), <i>ENCODING</i> can be either a canonical name or an alias. For encoding names and aliases, see <a href="#Defining-Aliases">"Defining Aliases"</a>; for <i>CHECK</i>, see <a href="#Handling-Malformed-Data">"Handling Malformed Data"</a>.</p> <p>For example, to convert ISO-8859-1 data into a string in Perl's internal format:</p> <pre><code>$string = decode("iso-8859-1", $octets);</code></pre> <p><b>CAVEAT</b>: When you run <code>$string = decode("utf8", $octets)</code>, then $string <i>might not be equal to</i> $octets. Though both contain the same data, the UTF8 flag for $string is on. See <a href="#The-UTF8-flag">"The UTF8 flag"</a> below.</p> <p>If the $string is <code>undef</code>, then <code>undef</code> is returned.</p> <h3 id="find_encoding"><a class="permalink" href="#find_encoding">#</a>find_encoding</h3> <pre><code>[$obj =] find_encoding(ENCODING)</code></pre> <p>Returns the <i>encoding object</i> corresponding to <i>ENCODING</i>. Returns <code>undef</code> if no matching <i>ENCODING</i> is find. The returned object is what does the actual encoding or decoding.</p> <pre><code>$utf8 = decode($name, $bytes);</code></pre> <p>is in fact</p> <pre><code>$utf8 = do { $obj = find_encoding($name); croak qq(encoding "$name" not found) unless ref $obj; $obj->decode($bytes); };</code></pre> <p>with more error checking.</p> <p>You can therefore save time by reusing this object as follows;</p> <pre><code>my $enc = find_encoding("iso-8859-1"); while(<>) { my $utf8 = $enc->decode($_); ... # now do something with $utf8; }</code></pre> <p>Besides <a href="#decode">"decode"</a> and <a href="#encode">"encode"</a>, other methods are available as well. For instance, <code>name()</code> returns the canonical name of the encoding object.</p> <pre><code>find_encoding("latin1")->name; # iso-8859-1</code></pre> <p>See <a href="/5.24.3/Encode::Encoding">Encode::Encoding</a> for details.</p> <h3 id="from_to"><a class="permalink" href="#from_to">#</a>from_to</h3> <pre><code>[$length =] from_to($octets, FROM_ENC, TO_ENC [, CHECK])</code></pre> <p>Converts <i>in-place</i> data between two encodings. The data in $octets must be encoded as octets and <i>not</i> as characters in Perl's internal format. For example, to convert ISO-8859-1 data into Microsoft's CP1250 encoding:</p> <pre><code>from_to($octets, "iso-8859-1", "cp1250");</code></pre> <p>and to convert it back:</p> <pre><code>from_to($octets, "cp1250", "iso-8859-1");</code></pre> <p>Because the conversion happens in place, the data to be converted cannot be a string constant: it must be a scalar variable.</p> <p><code>from_to()</code> returns the length of the converted string in octets on success, and <code>undef</code> on error.</p> <p><b>CAVEAT</b>: The following operations may look the same, but are not:</p> <pre><code>from_to($data, "iso-8859-1", "utf8"); #1 $data = decode("iso-8859-1", $data); #2</code></pre> <p>Both #1 and #2 make $data consist of a completely valid UTF-8 string, but only #2 turns the UTF8 flag on. #1 is equivalent to:</p> <pre><code>$data = encode("utf8", decode("iso-8859-1", $data));</code></pre> <p>See <a href="#The-UTF8-flag">"The UTF8 flag"</a> below.</p> <p>Also note that:</p> <pre><code>from_to($octets, $from, $to, $check);</code></pre> <p>is equivalent to:</p> <pre><code>$octets = encode($to, decode($from, $octets), $check);</code></pre> <p>Yes, it does <i>not</i> respect the $check during decoding. It is deliberately done that way. If you need minute control, use <code>decode</code> followed by <code>encode</code> as follows:</p> <pre><code>$octets = encode($to, decode($from, $octets, $check_from), $check_to);</code></pre> <h3 id="encode_utf8"><a class="permalink" href="#encode_utf8">#</a>encode_utf8</h3> <pre><code>$octets = encode_utf8($string);</code></pre> <p>Equivalent to <code>$octets = encode("utf8", $string)</code>. The characters in $string are encoded in Perl's internal format, and the result is returned as a sequence of octets. Because all possible characters in Perl have a (loose, not strict) UTF-8 representation, this function cannot fail.</p> <h3 id="decode_utf8"><a class="permalink" href="#decode_utf8">#</a>decode_utf8</h3> <pre><code>$string = decode_utf8($octets [, CHECK]);</code></pre> <p>Equivalent to <code>$string = decode("utf8", $octets [, CHECK])</code>. The sequence of octets represented by $octets is decoded from UTF-8 into a sequence of logical characters. Because not all sequences of octets are valid UTF-8, it is quite possible for this function to fail. For CHECK, see <a href="#Handling-Malformed-Data">"Handling Malformed Data"</a>.</p> <h2 id="Listing-available-encodings"><a class="permalink" href="#Listing-available-encodings">#</a><a id="Listing"></a>Listing available encodings</h2> <pre><code>use Encode; @list = Encode->encodings();</code></pre> <p>Returns a list of canonical names of available encodings that have already been loaded. To get a list of all available encodings including those that have not yet been loaded, say:</p> <pre><code>@all_encodings = Encode->encodings(":all");</code></pre> <p>Or you can give the name of a specific module:</p> <pre><code>@with_jp = Encode->encodings("Encode::JP");</code></pre> <p>When "<code>::</code>" is not in the name, "<code>Encode::</code>" is assumed.</p> <pre><code>@ebcdic = Encode->encodings("EBCDIC");</code></pre> <p>To find out in detail which encodings are supported by this package, see <a href="/5.24.3/Encode::Supported">Encode::Supported</a>.</p> <h2 id="Defining-Aliases"><a class="permalink" href="#Defining-Aliases">#</a><a id="Defining"></a>Defining Aliases</h2> <p>To add a new alias to a given encoding, use:</p> <pre><code>use Encode; use Encode::Alias; define_alias(NEWNAME => ENCODING);</code></pre> <p>After that, <i>NEWNAME</i> can be used as an alias for <i>ENCODING</i>. <i>ENCODING</i> may be either the name of an encoding or an <i>encoding object</i>.</p> <p>Before you do that, first make sure the alias is nonexistent using <code>resolve_alias()</code>, which returns the canonical name thereof. For example:</p> <pre><code>Encode::resolve_alias("latin1") eq "iso-8859-1" # true Encode::resolve_alias("iso-8859-12") # false; nonexistent Encode::resolve_alias($name) eq $name # true if $name is canonical</code></pre> <p><code>resolve_alias()</code> does not need <code>use Encode::Alias</code>; it can be imported via <code>use Encode qw(resolve_alias)</code>.</p> <p>See <a href="/5.24.3/Encode::Alias">Encode::Alias</a> for details.</p> <h2 id="Finding-IANA-Character-Set-Registry-names"><a class="permalink" href="#Finding-IANA-Character-Set-Registry-names">#</a><a id="Finding"></a>Finding IANA Character Set Registry names</h2> <p>The canonical name of a given encoding does not necessarily agree with IANA Character Set Registry, commonly seen as <code>Content-Type: text/plain; charset=<i>WHATEVER</i></code>. For most cases, the canonical name works, but sometimes it does not, most notably with "utf-8-strict".</p> <p>As of <code>Encode</code> version 2.21, a new method <code>mime_name()</code> is therefore added.</p> <pre><code>use Encode; my $enc = find_encoding("UTF-8"); warn $enc->name; # utf-8-strict warn $enc->mime_name; # UTF-8</code></pre> <p>See also: <a href="/5.24.3/Encode::Encoding">Encode::Encoding</a></p> <h1 id="Encoding-via-PerlIO"><a class="permalink" href="#Encoding-via-PerlIO">#</a><a id="Encoding"></a>Encoding via PerlIO</h1> <p>If your perl supports <code>PerlIO</code> (which is the default), you can use a <code>PerlIO</code> layer to decode and encode directly via a filehandle. The following two examples are fully identical in functionality:</p> <pre><code>### Version 1 via PerlIO open(INPUT, "< :encoding(shiftjis)", $infile) || die "Can't open < $infile for reading: $!"; open(OUTPUT, "> :encoding(euc-jp)", $outfile) || die "Can't open > $output for writing: $!"; while (<INPUT>) { # auto decodes $_ print OUTPUT; # auto encodes $_ } close(INPUT) || die "can't close $infile: $!"; close(OUTPUT) || die "can't close $outfile: $!"; ### Version 2 via from_to() open(INPUT, "< :raw", $infile) || die "Can't open < $infile for reading: $!"; open(OUTPUT, "> :raw", $outfile) || die "Can't open > $output for writing: $!"; while (<INPUT>) { from_to($_, "shiftjis", "euc-jp", 1); # switch encoding print OUTPUT; # emit raw (but properly encoded) data } close(INPUT) || die "can't close $infile: $!"; close(OUTPUT) || die "can't close $outfile: $!";</code></pre> <p>In the first version above, you let the appropriate encoding layer handle the conversion. In the second, you explicitly translate from one encoding to the other.</p> <p>Unfortunately, it may be that encodings are not <code>PerlIO</code>-savvy. You can check to see whether your encoding is supported by <code>PerlIO</code> by invoking the <code>perlio_ok</code> method on it:</p> <pre><code>Encode::perlio_ok("hz"); # false find_encoding("euc-cn")->perlio_ok; # true wherever PerlIO is available use Encode qw(perlio_ok); # imported upon request perlio_ok("euc-jp")</code></pre> <p>Fortunately, all encodings that come with <code>Encode</code> core are <code>PerlIO</code>-savvy except for <code>hz</code> and <code>ISO-2022-kr</code>. For the gory details, see <a href="/5.24.3/Encode::Encoding">Encode::Encoding</a> and <a href="/5.24.3/Encode::PerlIO">Encode::PerlIO</a>.</p> <h1 id="Handling-Malformed-Data"><a class="permalink" href="#Handling-Malformed-Data">#</a><a id="Handling"></a>Handling Malformed Data</h1> <p>The optional <i>CHECK</i> argument tells <code>Encode</code> what to do when encountering malformed data. Without <i>CHECK</i>, <code>Encode::FB_DEFAULT</code> (== 0) is assumed.</p> <p>As of version 2.12, <code>Encode</code> supports coderef values for <code>CHECK</code>; see below.</p> <p><b>NOTE:</b> Not all encodings support this feature. Some encodings ignore the <i>CHECK</i> argument. For example, <a href="/5.24.3/Encode::Unicode">Encode::Unicode</a> ignores <i>CHECK</i> and it always croaks on error.</p> <h2 id="List-of-CHECK-values"><a class="permalink" href="#List-of-CHECK-values">#</a><a id="List"></a>List of <i>CHECK</i> values</h2> <h3 id="FB_DEFAULT"><a class="permalink" href="#FB_DEFAULT">#</a>FB_DEFAULT</h3> <pre><code class="plaintext">I<CHECK> = Encode::FB_DEFAULT ( == 0)</code></pre> <p>If <i>CHECK</i> is 0, encoding and decoding replace any malformed character with a <i>substitution character</i>. When you encode, <i>SUBCHAR</i> is used. When you decode, the Unicode REPLACEMENT CHARACTER, code point U+FFFD, is used. If the data is supposed to be UTF-8, an optional lexical warning of warning category <code>"utf8"</code> is given.</p> <h3 id="FB_CROAK"><a class="permalink" href="#FB_CROAK">#</a>FB_CROAK</h3> <pre><code class="plaintext">I<CHECK> = Encode::FB_CROAK ( == 1)</code></pre> <p>If <i>CHECK</i> is 1, methods immediately die with an error message. Therefore, when <i>CHECK</i> is 1, you should trap exceptions with <code>eval{}</code>, unless you really want to let it <code>die</code>.</p> <h3 id="FB_QUIET"><a class="permalink" href="#FB_QUIET">#</a>FB_QUIET</h3> <pre><code class="plaintext">I<CHECK> = Encode::FB_QUIET</code></pre> <p>If <i>CHECK</i> is set to <code>Encode::FB_QUIET</code>, encoding and decoding immediately return the portion of the data that has been processed so far when an error occurs. The data argument is overwritten with everything after that point; that is, the unprocessed portion of the data. This is handy when you have to call <code>decode</code> repeatedly in the case where your source data may contain partial multi-byte character sequences, (that is, you are reading with a fixed-width buffer). Here's some sample code to do exactly that:</p> <pre><code>my($buffer, $string) = ("", ""); while (read($fh, $buffer, 256, length($buffer))) { $string .= decode($encoding, $buffer, Encode::FB_QUIET); # $buffer now contains the unprocessed partial character }</code></pre> <h3 id="FB_WARN"><a class="permalink" href="#FB_WARN">#</a>FB_WARN</h3> <pre><code class="plaintext">I<CHECK> = Encode::FB_WARN</code></pre> <p>This is the same as <code>FB_QUIET</code> above, except that instead of being silent on errors, it issues a warning. This is handy for when you are debugging.</p> <h3 id="FB_PERLQQ-FB_HTMLCREF-FB_XMLCREF"><a class="permalink" href="#FB_PERLQQ-FB_HTMLCREF-FB_XMLCREF">#</a><a id="FB_PERLQQ"></a>FB_PERLQQ FB_HTMLCREF FB_XMLCREF</h3> <dl> <dt id="perlqq-mode-(CHECK-=-Encode::FB_PERLQQ)"><a class="permalink" href="#perlqq-mode-(CHECK-=-Encode::FB_PERLQQ)">#</a><a id="perlqq"></a><a id="perlqq-mode-CHECK-Encode::FB_PERLQQ"></a>perlqq mode (<i>CHECK</i> = Encode::FB_PERLQQ)</dt> <dd> </dd> <dt id="HTML-charref-mode-(CHECK-=-Encode::FB_HTMLCREF)"><a class="permalink" href="#HTML-charref-mode-(CHECK-=-Encode::FB_HTMLCREF)">#</a><a id="HTML"></a><a id="HTML-charref-mode-CHECK-Encode::FB_HTMLCREF"></a>HTML charref mode (<i>CHECK</i> = Encode::FB_HTMLCREF)</dt> <dd> </dd> <dt id="XML-charref-mode-(CHECK-=-Encode::FB_XMLCREF)"><a class="permalink" href="#XML-charref-mode-(CHECK-=-Encode::FB_XMLCREF)">#</a><a id="XML"></a><a id="XML-charref-mode-CHECK-Encode::FB_XMLCREF"></a>XML charref mode (<i>CHECK</i> = Encode::FB_XMLCREF)</dt> <dd> </dd> </dl> <p>For encodings that are implemented by the <code>Encode::XS</code> module, <code>CHECK</code> <code>==</code> <code>Encode::FB_PERLQQ</code> puts <code>encode</code> and <code>decode</code> into <code>perlqq</code> fallback mode.</p> <p>When you decode, <code>\x<i>HH</i></code> is inserted for a malformed character, where <i>HH</i> is the hex representation of the octet that could not be decoded to utf8. When you encode, <code>\x{<i>HHHH</i>}</code> will be inserted, where <i>HHHH</i> is the Unicode code point (in any number of hex digits) of the character that cannot be found in the character repertoire of the encoding.</p> <p>The HTML/XML character reference modes are about the same. In place of <code>\x{<i>HHHH</i>}</code>, HTML uses <code>&#<i>NNN</i>;</code> where <i>NNN</i> is a decimal number, and XML uses <code>&#x<i>HHHH</i>;</code> where <i>HHHH</i> is the hexadecimal number.</p> <p>In <code>Encode</code> 2.10 or later, <code>LEAVE_SRC</code> is also implied.</p> <h3 id="The-bitmask"><a class="permalink" href="#The-bitmask">#</a><a id="The"></a>The bitmask</h3> <p>These modes are all actually set via a bitmask. Here is how the <code>FB_<i>XXX</i></code> constants are laid out. You can import the <code>FB_<i>XXX</i></code> constants via <code>use Encode qw(:fallbacks)</code>, and you can import the generic bitmask constants via <code>use Encode qw(:fallback_all)</code>.</p> <pre><code class="plaintext"> FB_DEFAULT FB_CROAK FB_QUIET FB_WARN FB_PERLQQ DIE_ON_ERR 0x0001 X WARN_ON_ERR 0x0002 X RETURN_ON_ERR 0x0004 X X LEAVE_SRC 0x0008 X PERLQQ 0x0100 X HTMLCREF 0x0200 XMLCREF 0x0400</code></pre> <h3 id="LEAVE_SRC"><a class="permalink" href="#LEAVE_SRC">#</a>LEAVE_SRC</h3> <pre><code class="plaintext">Encode::LEAVE_SRC</code></pre> <p>If the <code>Encode::LEAVE_SRC</code> bit is <i>not</i> set but <i>CHECK</i> is set, then the source string to encode() or decode() will be overwritten in place. If you're not interested in this, then bitwise-OR it with the bitmask.</p> <h2 id="coderef-for-CHECK"><a class="permalink" href="#coderef-for-CHECK">#</a><a id="coderef"></a>coderef for CHECK</h2> <p>As of <code>Encode</code> 2.12, <code>CHECK</code> can also be a code reference which takes the ordinal value of the unmapped character as an argument and returns octets that represent the fallback character. For instance:</p> <pre><code>$ascii = encode("ascii", $utf8, sub{ sprintf "<U+%04X>", shift });</code></pre> <p>Acts like <code>FB_PERLQQ</code> but U+<i>XXXX</i> is used instead of <code>\x{<i>XXXX</i>}</code>.</p> <p>Even the fallback for <code>decode</code> must return octets, which are then decoded with the character encoding that <code>decode</code> accepts. So for example if you wish to decode octets as UTF-8, and use ISO-8859-15 as a fallback for bytes that are not valid UTF-8, you could write</p> <pre><code>$str = decode 'UTF-8', $octets, sub { my $tmp = chr shift; from_to $tmp, 'ISO-8859-15', 'UTF-8'; return $tmp; };</code></pre> <h1 id="Defining-Encodings"><a class="permalink" href="#Defining-Encodings">#</a><a id="Defining1"></a>Defining Encodings</h1> <p>To define a new encoding, use:</p> <pre><code>use Encode qw(define_encoding); define_encoding($object, CANONICAL_NAME [, alias...]);</code></pre> <p><i>CANONICAL_NAME</i> will be associated with <i>$object</i>. The object should provide the interface described in <a href="/5.24.3/Encode::Encoding">Encode::Encoding</a>. If more than two arguments are provided, additional arguments are considered aliases for <i>$object</i>.</p> <p>See <a href="/5.24.3/Encode::Encoding">Encode::Encoding</a> for details.</p> <h1 id="The-UTF8-flag"><a class="permalink" href="#The-UTF8-flag">#</a><a id="The1"></a>The UTF8 flag</h1> <p>Before the introduction of Unicode support in Perl, The <code>eq</code> operator just compared the strings represented by two scalars. Beginning with Perl 5.8, <code>eq</code> compares two strings with simultaneous consideration of <i>the UTF8 flag</i>. To explain why we made it so, I quote from page 402 of <i>Programming Perl, 3rd ed.</i></p> <dl> <dt id="Goal-#1:"><a class="permalink" href="#Goal-%231:">#</a><a id="Goal"></a><a id="Goal-1"></a>Goal #1:</dt> <dd> <p>Old byte-oriented programs should not spontaneously break on the old byte-oriented data they used to work on.</p> </dd> <dt id="Goal-#2:"><a class="permalink" href="#Goal-%232:">#</a><a id="Goal1"></a><a id="Goal-2"></a>Goal #2:</dt> <dd> <p>Old byte-oriented programs should magically start working on the new character-oriented data when appropriate.</p> </dd> <dt id="Goal-#3:"><a class="permalink" href="#Goal-%233:">#</a><a id="Goal2"></a><a id="Goal-3"></a>Goal #3:</dt> <dd> <p>Programs should run just as fast in the new character-oriented mode as in the old byte-oriented mode.</p> </dd> <dt id="Goal-#4:"><a class="permalink" href="#Goal-%234:">#</a><a id="Goal3"></a><a id="Goal-4"></a>Goal #4:</dt> <dd> <p>Perl should remain one language, rather than forking into a byte-oriented Perl and a character-oriented Perl.</p> </dd> </dl> <p>When <i>Programming Perl, 3rd ed.</i> was written, not even Perl 5.6.0 had been born yet, many features documented in the book remained unimplemented for a long time. Perl 5.8 corrected much of this, and the introduction of the UTF8 flag is one of them. You can think of there being two fundamentally different kinds of strings and string-operations in Perl: one a byte-oriented mode for when the internal UTF8 flag is off, and the other a character-oriented mode for when the internal UTF8 flag is on.</p> <p>Here is how <code>Encode</code> handles the UTF8 flag.</p> <ul> <li><p>When you <i>encode</i>, the resulting UTF8 flag is always <b>off</b>.</p> </li> <li><p>When you <i>decode</i>, the resulting UTF8 flag is <b>on</b>--<i>unless</i> you can unambiguously represent data. Here is what we mean by "unambiguously". After <code>$utf8 = decode("foo", $octet)</code>,</p> <pre><code>When $octet is... The UTF8 flag in $utf8 is --------------------------------------------- In ASCII only (or EBCDIC only) OFF In ISO-8859-1 ON In any other Encoding ON ---------------------------------------------</code></pre> <p>As you see, there is one exception: in ASCII. That way you can assume Goal #1. And with <code>Encode</code>, Goal #2 is assumed but you still have to be careful in the cases mentioned in the <b>CAVEAT</b> paragraphs above.</p> <p>This UTF8 flag is not visible in Perl scripts, exactly for the same reason you cannot (or rather, you <i>don't have to</i>) see whether a scalar contains a string, an integer, or a floating-point number. But you can still peek and poke these if you will. See the next section.</p> </li> </ul> <h2 id="Messing-with-Perl's-Internals"><a class="permalink" href="#Messing-with-Perl's-Internals">#</a><a id="Messing"></a><a id="Messing-with-Perls-Internals"></a>Messing with Perl's Internals</h2> <p>The following API uses parts of Perl's internals in the current implementation. As such, they are efficient but may change in a future release.</p> <h3 id="is_utf8"><a class="permalink" href="#is_utf8">#</a>is_utf8</h3> <pre><code class="plaintext">is_utf8(STRING [, CHECK])</code></pre> <p>[INTERNAL] Tests whether the UTF8 flag is turned on in the <i>STRING</i>. If <i>CHECK</i> is true, also checks whether <i>STRING</i> contains well-formed UTF-8. Returns true if successful, false otherwise.</p> <p>As of Perl 5.8.1, <a href="/5.24.3/utf8">utf8</a> also has the <code>utf8::is_utf8</code> function.</p> <h3 id="_utf8_on"><a class="permalink" href="#_utf8_on">#</a><a id="utf8_on"></a>_utf8_on</h3> <pre><code class="plaintext">_utf8_on(STRING)</code></pre> <p>[INTERNAL] Turns the <i>STRING</i>'s internal UTF8 flag <b>on</b>. The <i>STRING</i> is <i>not</i> checked for containing only well-formed UTF-8. Do not use this unless you <i>know with absolute certainty</i> that the STRING holds only well-formed UTF-8. Returns the previous state of the UTF8 flag (so please don't treat the return value as indicating success or failure), or <code>undef</code> if <i>STRING</i> is not a string.</p> <p><b>NOTE</b>: For security reasons, this function does not work on tainted values.</p> <h3 id="_utf8_off"><a class="permalink" href="#_utf8_off">#</a><a id="utf8_off"></a>_utf8_off</h3> <pre><code class="plaintext">_utf8_off(STRING)</code></pre> <p>[INTERNAL] Turns the <i>STRING</i>'s internal UTF8 flag <b>off</b>. Do not use frivolously. Returns the previous state of the UTF8 flag, or <code>undef</code> if <i>STRING</i> is not a string. Do not treat the return value as indicative of success or failure, because that isn't what it means: it is only the previous setting.</p> <p><b>NOTE</b>: For security reasons, this function does not work on tainted values.</p> <h1 id="UTF-8-vs.-utf8-vs.-UTF8"><a class="permalink" href="#UTF-8-vs.-utf8-vs.-UTF8">#</a><a id="UTF"></a>UTF-8 vs. utf8 vs. UTF8</h1> <pre><code class="plaintext">....We now view strings not as sequences of bytes, but as sequences of numbers in the range 0 .. 2**32-1 (or in the case of 64-bit computers, 0 .. 2**64-1) -- Programming Perl, 3rd ed.</code></pre> <p>That has historically been Perl's notion of UTF-8, as that is how UTF-8 was first conceived by Ken Thompson when he invented it. However, thanks to later revisions to the applicable standards, official UTF-8 is now rather stricter than that. For example, its range is much narrower (0 .. 0x10_FFFF to cover only 21 bits instead of 32 or 64 bits) and some sequences are not allowed, like those used in surrogate pairs, the 31 non-character code points 0xFDD0 .. 0xFDEF, the last two code points in <i>any</i> plane (0x<i>XX</i>_FFFE and 0x<i>XX</i>_FFFF), all non-shortest encodings, etc.</p> <p>The former default in which Perl would always use a loose interpretation of UTF-8 has now been overruled:</p> <pre><code>From: Larry Wall <larry@wall.org> Date: December 04, 2004 11:51:58 JST To: perl-unicode@perl.org Subject: Re: Make Encode.pm support the real UTF-8 Message-Id: <20041204025158.GA28754@wall.org> On Fri, Dec 03, 2004 at 10:12:12PM +0000, Tim Bunce wrote: : I've no problem with 'utf8' being perl's unrestricted uft8 encoding, : but "UTF-8" is the name of the standard and should give the : corresponding behaviour. For what it's worth, that's how I've always kept them straight in my head. Also for what it's worth, Perl 6 will mostly default to strict but make it easy to switch back to lax. Larry</code></pre> <p>Got that? As of Perl 5.8.7, <b>"UTF-8"</b> means UTF-8 in its current sense, which is conservative and strict and security-conscious, whereas <b>"utf8"</b> means UTF-8 in its former sense, which was liberal and loose and lax. <code>Encode</code> version 2.10 or later thus groks this subtle but critically important distinction between <code>"UTF-8"</code> and <code>"utf8"</code>.</p> <pre><code>encode("utf8", "\x{FFFF_FFFF}", 1); # okay encode("UTF-8", "\x{FFFF_FFFF}", 1); # croaks</code></pre> <p>In the <code>Encode</code> module, <code>"UTF-8"</code> is actually a canonical name for <code>"utf-8-strict"</code>. That hyphen between the <code>"UTF"</code> and the <code>"8"</code> is critical; without it, <code>Encode</code> goes "liberal" and (perhaps overly-)permissive:</p> <pre><code>find_encoding("UTF-8")->name # is 'utf-8-strict' find_encoding("utf-8")->name # ditto. names are case insensitive find_encoding("utf_8")->name # ditto. "_" are treated as "-" find_encoding("UTF8")->name # is 'utf8'.</code></pre> <p>Perl's internal UTF8 flag is called "UTF8", without a hyphen. It indicates whether a string is internally encoded as "utf8", also without a hyphen.</p> <h1 id="SEE-ALSO"><a class="permalink" href="#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1> <p><a href="/5.24.3/Encode::Encoding">Encode::Encoding</a>, <a href="/5.24.3/Encode::Supported">Encode::Supported</a>, <a href="/5.24.3/Encode::PerlIO">Encode::PerlIO</a>, <a href="/5.24.3/encoding">encoding</a>, <a href="/5.24.3/perlebcdic">perlebcdic</a>, <a href="/5.24.3/perlfunc#open">"open" in perlfunc</a>, <a href="/5.24.3/perlunicode">perlunicode</a>, <a href="/5.24.3/perluniintro">perluniintro</a>, <a href="/5.24.3/perlunifaq">perlunifaq</a>, <a href="/5.24.3/perlunitut">perlunitut</a> <a href="/5.24.3/utf8">utf8</a>, the Perl Unicode Mailing List <a href="http://lists.perl.org/list/perl-unicode.html">http://lists.perl.org/list/perl-unicode.html</a></p> <h1 id="MAINTAINER"><a class="permalink" href="#MAINTAINER">#</a>MAINTAINER</h1> <p>This project was originated by the late Nick Ing-Simmons and later maintained by Dan Kogai <i><dankogai@cpan.org></i>. See AUTHORS for a full list of people involved. For any questions, send mail to <i><perl-unicode@perl.org></i> so that we can all share.</p> <p>While Dan Kogai retains the copyright as a maintainer, credit should go to all those involved. See AUTHORS for a list of those who submitted code to the project.</p> <h1 id="COPYRIGHT"><a class="permalink" href="#COPYRIGHT">#</a>COPYRIGHT</h1> <p>Copyright 2002-2014 Dan Kogai <i><dankogai@cpan.org></i>.</p> <p>This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.</p> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>