perlunicook.html 106 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlunicook - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="index.html"></a>
	</div>
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
            
	    <div class="side_panel doc_panel">
              <p>Manual</p>
              <ul>
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">History / Changes</a>
                <li><a href="index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Special Variables</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>perlunicook</h1>
              </div>
              <div id="perl_version">
294
                Perl 5 version 26.0 documentation
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-language.html">Language reference</a> &gt;
      
    
    perlunicook
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
	    <div id="from_search"></div>
            <h1>perlunicook</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#EXAMPLES">EXAMPLES</a><ul><li><a href="#%211e-0%3a-Standard-preamble">℞ 0: Standard preamble</a><li><a href="#%211e-1%3a-Generic-Unicode-savvy-filter">℞ 1: Generic Unicode-savvy filter</a><li><a href="#%211e-2%3a-Fine-tuning-Unicode-warnings">℞ 2: Fine-tuning Unicode warnings</a><li><a href="#%211e-3%3a-Declare-source-in-utf8-for-identifiers-and-literals">℞ 3: Declare source in utf8 for identifiers and literals</a><li><a href="#%211e-4%3a-Characters-and-their-numbers">℞ 4: Characters and their numbers</a><li><a href="#%211e-5%3a-Unicode-literals-by-character-number">℞ 5: Unicode literals by character number</a><li><a href="#%211e-6%3a-Get-character-name-by-number">℞ 6: Get character name by number</a><li><a href="#%211e-7%3a-Get-character-number-by-name">℞ 7: Get character number by name</a><li><a href="#%211e-8%3a-Unicode-named-characters">℞ 8: Unicode named characters</a><li><a href="#%211e-9%3a-Unicode-named-sequences">℞ 9: Unicode named sequences</a><li><a href="#%211e-10%3a-Custom-named-characters">℞ 10: Custom named characters</a><li><a href="#%211e-11%3a-Names-of-CJK-codepoints">℞ 11: Names of CJK codepoints</a><li><a href="#%211e-12%3a-Explicit-encode%2fdecode">℞ 12: Explicit encode/decode</a><li><a href="#%211e-13%3a-Decode-program-arguments-as-utf8">℞ 13: Decode program arguments as utf8</a><li><a href="#%211e-14%3a-Decode-program-arguments-as-locale-encoding">℞ 14: Decode program arguments as locale encoding</a><li><a href="#%211e-15%3a-Declare-STD%7bIN%2cOUT%2cERR%7d-to-be-utf8">℞ 15: Declare STD{IN,OUT,ERR} to be utf8</a><li><a href="#%211e-16%3a-Declare-STD%7bIN%2cOUT%2cERR%7d-to-be-in-locale-encoding">℞ 16: Declare STD{IN,OUT,ERR} to be in locale encoding</a><li><a href="#%211e-17%3a-Make-file-I%2fO-default-to-utf8">℞ 17: Make file I/O default to utf8</a><li><a href="#%211e-18%3a-Make-all-I%2fO-and-args-default-to-utf8">℞ 18: Make all I/O and args default to utf8</a><li><a href="#%211e-19%3a-Open-file-with-specific-encoding">℞ 19: Open file with specific encoding</a><li><a href="#%211e-20%3a-Unicode-casing">℞ 20: Unicode casing</a><li><a href="#%211e-21%3a-Unicode-case-insensitive-comparisons">℞ 21: Unicode case-insensitive comparisons</a><li><a href="#%211e-22%3a-Match-Unicode-linebreak-sequence-in-regex">℞ 22: Match Unicode linebreak sequence in regex</a><li><a href="#%211e-23%3a-Get-character-category">℞ 23: Get character category</a><li><a href="#%211e-24%3a-Disabling-Unicode-awareness-in-builtin-charclasses">℞ 24: Disabling Unicode-awareness in builtin charclasses</a><li><a href="#%211e-25%3a-Match-Unicode-properties-in-regex-with-%5cp%2c-%5cP">℞ 25: Match Unicode properties in regex with \p, \P</a><li><a href="#%211e-26%3a-Custom-character-properties">℞ 26: Custom character properties</a><li><a href="#%211e-27%3a-Unicode-normalization">℞ 27: Unicode normalization</a><li><a href="#%211e-28%3a-Convert-non-ASCII-Unicode-numerics">℞ 28: Convert non-ASCII Unicode numerics</a><li><a href="#%211e-29%3a-Match-Unicode-grapheme-cluster-in-regex">℞ 29: Match Unicode grapheme cluster in regex</a><li><a href="#%211e-30%3a-Extract-by-grapheme-instead-of-by-codepoint-(regex)">℞ 30: Extract by grapheme instead of by codepoint (regex)</a><li><a href="#%211e-31%3a-Extract-by-grapheme-instead-of-by-codepoint-(substr)">℞ 31: Extract by grapheme instead of by codepoint (substr)</a><li><a href="#%211e-32%3a-Reverse-string-by-grapheme">℞ 32: Reverse string by grapheme</a><li><a href="#%211e-33%3a-String-length-in-graphemes">℞ 33: String length in graphemes</a><li><a href="#%211e-34%3a-Unicode-column-width-for-printing">℞ 34: Unicode column-width for printing</a><li><a href="#%211e-35%3a-Unicode-collation">℞ 35: Unicode collation</a><li><a href="#%211e-36%3a-Case--_and_-accent-insensitive-Unicode-sort">℞ 36: Case- _and_ accent-insensitive Unicode sort</a><li><a href="#%211e-37%3a-Unicode-locale-collation">℞ 37: Unicode locale collation</a><li><a href="#%211e-38%3a-Making-cmp-work-on-text-instead-of-codepoints">℞ 38: Making cmp work on text instead of codepoints</a><li><a href="#%211e-39%3a-Case--_and_-accent-insensitive-comparisons">℞ 39: Case- _and_ accent-insensitive comparisons</a><li><a href="#%211e-40%3a-Case--_and_-accent-insensitive-locale-comparisons">℞ 40: Case- _and_ accent-insensitive locale comparisons</a><li><a href="#%211e-41%3a-Unicode-linebreaking">℞ 41: Unicode linebreaking</a><li><a href="#%211e-42%3a-Unicode-text-in-DBM-hashes%2c-the-tedious-way">℞ 42: Unicode text in DBM hashes, the tedious way</a><li><a href="#%211e-43%3a-Unicode-text-in-DBM-hashes%2c-the-easy-way">℞ 43: Unicode text in DBM hashes, the easy way</a><li><a href="#%211e-44%3a-PROGRAM%3a-Demo-of-Unicode-collation-and-printing">℞ 44: PROGRAM: Demo of Unicode collation and printing</a></ul><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHOR">AUTHOR</a><li><a href="#COPYRIGHT-AND-LICENCE">COPYRIGHT AND LICENCE</a><li><a href="#REVISION-HISTORY">REVISION HISTORY</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlunicook - cookbookish examples of handling Unicode in Perl</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>This manpage contains short recipes demonstrating how to handle common Unicode
operations in Perl, plus one complete program at the end. Any undeclared
variables in individual recipes are assumed to have a previous appropriate
value in them.</p>
<a name="EXAMPLES"></a><h1>EXAMPLES</h1>
<a name="%211e-0%3a-Standard-preamble"></a><h2>&#x211e; 0: Standard preamble</h2>
<p>Unless otherwise notes, all examples below require this standard preamble
to work correctly, with the <code class="inline"><span class="c">#!</span></code>
 adjusted to work on your system:</p>
396
<pre class="verbatim"><ol><li> <span class="c">#!/usr/bin/env perl</span></li><li></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">utf8</span><span class="sc">;</span>      <span class="c"># so literals and identifiers can be in UTF-8</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.12</span><span class="sc">;</span>     <span class="c"># or later to get &quot;unicode_strings&quot; feature</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span>    <span class="c"># quote strings, declare variables</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span>  <span class="c"># on by default</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span>  <span class="q">qw(FATAL utf8)</span><span class="sc">;</span>    <span class="c"># fatalize encoding glitches</span></li><li> <a class="l_k" href="functions/use.html">use</a> <a class="l_k" href="functions/open.html">open</a>      <span class="q">qw(:std :encoding(UTF-8))</span><span class="sc">;</span> <span class="c"># undeclared streams in UTF-8</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">charnames</span> <span class="q">qw(:full :short)</span><span class="sc">;</span>  <span class="c"># unneeded in v5.16</span></li></ol></pre><p>This <i>does</i> make even Unix programmers <code class="inline"><a class="l_k" href="functions/binmode.html">binmode</a></code> your binary streams,
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
or open them with <code class="inline"><span class="j">:</span><span class="w">raw</span></code>
, but that's the only way to get at them
portably anyway.</p>
<p><b>WARNING</b>: <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">autodie</span></code>
 (pre 2.26) and <code class="inline"><a class="l_k" href="functions/use.html">use</a> <a class="l_k" href="functions/open.html">open</a></code>
 do not get along with each
other.</p>
<a name="%211e-1%3a-Generic-Unicode-savvy-filter"></a><h2>&#x211e; 1: Generic Unicode-savvy filter</h2>
<p>Always decompose on the way in, then recompose on the way out.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::Normalize</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>     <span class="i">$_</span> = <span class="i">NFD</span><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="sc">;</span>   <span class="c"># decompose + reorder canonically</span></li><li>     ...</li><li> <span class="s">}</span> <a class="l_k" href="functions/continue.html">continue</a> <span class="s">{</span></li><li>     <a class="l_k" href="functions/print.html">print</a> <span class="i">NFC</span><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="sc">;</span>  <span class="c"># recompose (where possible) + reorder canonically</span></li><li> <span class="s">}</span></li></ol></pre><a name="%211e-2%3a-Fine-tuning-Unicode-warnings"></a><h2>&#x211e; 2: Fine-tuning Unicode warnings</h2>
<p>As of v5.14, Perl distinguishes three subclasses of UTF&#x2011;8 warnings.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.14</span><span class="sc">;</span>                  <span class="c"># subwarnings unavailable any earlier</span></li><li> <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span> <span class="q">&quot;nonchar&quot;</span><span class="sc">;</span>      <span class="c"># the 66 forbidden non-characters</span></li><li> <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span> <span class="q">&quot;surrogate&quot;</span><span class="sc">;</span>    <span class="c"># UTF-16/CESU-8 nonsense</span></li><li> <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span> <span class="q">&quot;non_unicode&quot;</span><span class="sc">;</span>  <span class="c"># for codepoints over 0x10_FFFF</span></li></ol></pre><a name="%211e-3%3a-Declare-source-in-utf8-for-identifiers-and-literals"></a><h2>&#x211e; 3: Declare source in utf8 for identifiers and literals</h2>
<p>Without the all-critical <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">utf8</span></code>
 declaration, putting UTF&#x2011;8 in your
literals and identifiers won&#x2019;t work right.  If you used the standard
preamble just given above, this already happened.  If you did, you can
do things like this:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">utf8</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$measure</span>   = <span class="q">&quot;&Aring;ngstr&ouml;m&quot;</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@&mu;soft</span>     = <span class="q">qw( cp852 cp1251 cp1252 )</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@&#x1F51;&pi;&#x3AD;&rho;&mu;&epsilon;&gamma;&alpha;&sigmaf;</span> = <span class="q">qw( &#x1F51;&pi;&#x3AD;&rho;  &mu;&epsilon;&gamma;&alpha;&sigmaf; )</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@&#x9BC9;</span>        = <span class="q">qw( koi8-f koi8-u koi8-r )</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$motto</span>     = <span class="q">&quot;&#x1F46A; &#x1F497; &#x1F42A;&quot;</span><span class="sc">;</span> <span class="c"># FAMILY, GROWING HEART, DROMEDARY CAMEL</span></li></ol></pre><p>If you forget <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">utf8</span></code>
, high bytes will be misunderstood as
separate characters, and nothing will work right.</p>
<a name="%211e-4%3a-Characters-and-their-numbers"></a><h2>&#x211e; 4: Characters and their numbers</h2>
<p>The <code class="inline"><a class="l_k" href="functions/ord.html">ord</a></code> and <code class="inline"><a class="l_k" href="functions/chr.html">chr</a></code> functions work transparently on all codepoints,
not just on ASCII alone &#x2014; nor in fact, not even just on Unicode alone.</p>
<pre class="verbatim"><ol><li> <span class="c"># ASCII characters</span></li><li> <a class="l_k" href="functions/ord.html">ord</a><span class="s">(</span><span class="q">&quot;A&quot;</span><span class="s">)</span></li><li> <a class="l_k" href="functions/chr.html">chr</a><span class="s">(</span><span class="n">65</span><span class="s">)</span></li><li></li><li> <span class="c"># characters from the Basic Multilingual Plane</span></li><li> <a class="l_k" href="functions/ord.html">ord</a><span class="s">(</span><span class="q">&quot;&Sigma;&quot;</span><span class="s">)</span></li><li> <a class="l_k" href="functions/chr.html">chr</a><span class="s">(</span><span class="n">0x3A3</span><span class="s">)</span></li><li></li><li> <span class="c"># beyond the BMP</span></li><li> <a class="l_k" href="functions/ord.html">ord</a><span class="s">(</span><span class="q">&quot;&#x1D45B;&quot;</span><span class="s">)</span>               <span class="c"># MATHEMATICAL ITALIC SMALL N</span></li><li> <a class="l_k" href="functions/chr.html">chr</a><span class="s">(</span><span class="n">0x1D45B</span><span class="s">)</span></li><li></li><li> <span class="c"># beyond Unicode! (up to MAXINT)</span></li><li> <a class="l_k" href="functions/ord.html">ord</a><span class="s">(</span><span class="q">&quot;\x{20_0000}&quot;</span><span class="s">)</span></li><li> <a class="l_k" href="functions/chr.html">chr</a><span class="s">(</span><span class="n">0x20_0000</span><span class="s">)</span></li></ol></pre><a name="%211e-5%3a-Unicode-literals-by-character-number"></a><h2>&#x211e; 5: Unicode literals by character number</h2>
<p>In an interpolated literal, whether a double-quoted string or a
regex, you may specify a character by its number using the
<code class="inline">\x{<i>HHHHHH</i>}</code> escape.</p>
<pre class="verbatim"><ol><li> <span class="j">String:</span> <span class="q">&quot;\x{3a3}&quot;</span></li><li> <span class="w">Regex</span><span class="co">:</span>  <span class="q">/\x{3a3}/</span></li><li></li><li> <span class="w">String</span><span class="co">:</span> <span class="q">&quot;\x{1d45b}&quot;</span></li><li> <span class="w">Regex</span><span class="co">:</span>  <span class="q">/\x{1d45b}/</span></li><li></li><li> <span class="c"># even non-BMP ranges in regex work fine</span></li><li> /<span class="s">[</span>\<span class="i">x</span><span class="s">{</span><span class="n">1</span><span class="w">D434</span><span class="s">}</span>-\<span class="i">x</span><span class="s">{</span><span class="n">1</span><span class="w">D467</span><span class="s">}</span><span class="s">]</span><span class="q">/</span></li></ol></pre><a name="%211e-6%3a-Get-character-name-by-number"></a><h2>&#x211e; 6: Get character name by number</h2>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">charnames</span> <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$name</span> = <span class="i">charnames::viacode</span><span class="s">(</span><span class="n">0x03A3</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="%211e-7%3a-Get-character-number-by-name"></a><h2>&#x211e; 7: Get character number by name</h2>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">charnames</span> <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$number</span> = <span class="i">charnames::vianame</span><span class="s">(</span><span class="q">&quot;GREEK CAPITAL LETTER SIGMA&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="%211e-8%3a-Unicode-named-characters"></a><h2>&#x211e; 8: Unicode named characters</h2>
<p>Use the <code class="inline">\N{<i>charname</i>}</code> notation to get the character
by that name for use in interpolated literals (double-quoted
strings and regexes).  In v5.16, there is an implicit</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">charnames</span> <span class="q">qw(:full :short)</span><span class="sc">;</span></li></ol></pre><p>But prior to v5.16, you must be explicit about which set of charnames you
want.  The <code class="inline"><span class="j">:</span><span class="w">full</span></code>
 names are the official Unicode character name, alias, or
sequence, which all share a namespace.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">charnames</span> <span class="q">qw(:full :short latin greek)</span><span class="sc">;</span></li><li></li><li> <span class="q">&quot;\N{MATHEMATICAL ITALIC SMALL N}&quot;</span>      <span class="c"># :full</span></li><li> <span class="q">&quot;\N{GREEK CAPITAL LETTER SIGMA}&quot;</span>       <span class="c"># :full</span></li></ol></pre><p>Anything else is a Perl-specific convenience abbreviation.  Specify one or
more scripts by names if you want short names that are script-specific.</p>
<pre class="verbatim"><ol><li> <span class="q">&quot;\N{Greek:Sigma}&quot;</span>                      <span class="c"># :short</span></li><li> <span class="q">&quot;\N{ae}&quot;</span>                               <span class="c">#  latin</span></li><li> <span class="q">&quot;\N{epsilon}&quot;</span>                          <span class="c">#  greek</span></li></ol></pre><p>The v5.16 release also supports a <code class="inline"><span class="j">:</span><span class="w">loose</span></code>
 import for loose matching of
character names, which works just like loose matching of property names:
that is, it disregards case, whitespace, and underscores:</p>
<pre class="verbatim"><ol><li> <span class="q">&quot;\N{euro sign}&quot;</span>                        <span class="c"># :loose (from v5.16)</span></li></ol></pre><a name="%211e-9%3a-Unicode-named-sequences"></a><h2>&#x211e; 9: Unicode named sequences</h2>
<p>These look just like character names but return multiple codepoints.
Notice the <code class="inline"><span class="i">%vx</span></code>
 vector-print functionality in <code class="inline"><a class="l_k" href="functions/printf.html">printf</a></code>.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">charnames</span> <span class="q">qw(:full)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$seq</span> = <span class="q">&quot;\N{LATIN CAPITAL LETTER A WITH MACRON AND GRAVE}&quot;</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;U+%v04X\n&quot;</span><span class="cm">,</span> <span class="i">$seq</span><span class="sc">;</span></li><li> <span class="w">U</span>+<span class="n">0100</span>.<span class="n">0300</span></li></ol></pre><a name="%211e-10%3a-Custom-named-characters"></a><h2>&#x211e; 10: Custom named characters</h2>
<p>Use <code class="inline"><span class="j">:</span><span class="w">alias</span></code>
 to give your own lexically scoped nicknames to existing
characters, or even to give unnamed private-use characters useful names.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">charnames</span> <span class="q">&quot;:full&quot;</span><span class="cm">,</span> <span class="q">&quot;:alias&quot;</span> <span class="cm">=&gt;</span> <span class="s">{</span></li><li>     <span class="w">ecute</span> <span class="cm">=&gt;</span> <span class="q">&quot;LATIN SMALL LETTER E WITH ACUTE&quot;</span><span class="cm">,</span></li><li>     <span class="q">&quot;APPLE LOGO&quot;</span> <span class="cm">=&gt;</span> <span class="n">0xF8FF</span><span class="cm">,</span> <span class="c"># private use character</span></li><li> <span class="s">}</span><span class="sc">;</span></li><li></li><li> <span class="q">&quot;\N{ecute}&quot;</span></li><li> <span class="q">&quot;\N{APPLE LOGO}&quot;</span></li></ol></pre><a name="%211e-11%3a-Names-of-CJK-codepoints"></a><h2>&#x211e; 11: Names of CJK codepoints</h2>
<p>Sinograms like &#x201c;&#x6771;&#x4eac;&#x201d; come back with character names of
<code class="inline"><span class="w">CJK</span> <span class="w">UNIFIED</span> <span class="w">IDEOGRAPH</span>-<span class="n">6771</span></code>
 and <code class="inline"><span class="w">CJK</span> <span class="w">UNIFIED</span> <span class="w">IDEOGRAPH</span>-<span class="n">4</span><span class="w">EAC</span></code>
,
because their &#x201c;names&#x201d; vary.  The CPAN <code class="inline"><span class="w">Unicode::Unihan</span></code>
 module
has a large database for decoding these (and a whole lot more), provided you
know how to understand its output.</p>
457
<pre class="verbatim"><ol><li> <span class="c"># cpan -i Unicode::Unihan</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::Unihan</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$str</span> = <span class="q">&quot;&#x6771;&#x4EAC;&quot;</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$unhan</span> = <span class="w">Unicode::Unihan</span><span class="w">-&gt;new</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/for.html">for</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$lang</span> <span class="s">(</span><span class="q">qw(Mandarin Cantonese Korean JapaneseOn JapaneseKun)</span><span class="s">)</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;CJK $str in %-12s is &quot;</span><span class="cm">,</span> <span class="i">$lang</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/say.html">say</a> <span class="i">$unhan</span><span class="i">-&gt;$lang</span><span class="s">(</span><span class="i">$str</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>prints:</p>
458 459
<pre class="verbatim"><ol><li> <span class="w">CJK</span> <span class="w">&#x6771;&#x4EAC;</span> <span class="w">in</span> <span class="w">Mandarin</span>     <span class="w">is</span> <span class="w">DONG1JING1</span></li><li> <span class="w">CJK</span> <span class="w">&#x6771;&#x4EAC;</span> <span class="w">in</span> <span class="w">Cantonese</span>    <span class="w">is</span> <span class="w">dung1ging1</span></li><li> <span class="w">CJK</span> <span class="w">&#x6771;&#x4EAC;</span> <span class="w">in</span> <span class="w">Korean</span>       <span class="w">is</span> <span class="w">TONGKYENG</span></li><li> <span class="w">CJK</span> <span class="w">&#x6771;&#x4EAC;</span> <span class="w">in</span> <span class="w">JapaneseOn</span>   <span class="w">is</span> <span class="w">TOUKYOU</span> <span class="w">KEI</span> <span class="w">KIN</span></li><li> <span class="w">CJK</span> <span class="w">&#x6771;&#x4EAC;</span> <span class="w">in</span> <span class="w">JapaneseKun</span>  <span class="w">is</span> <span class="w">HIGASHI</span> <span class="w">AZUMAMIYAKO</span></li></ol></pre><p>If you have a specific romanization scheme in mind,
use the specific module:</p>
460
<pre class="verbatim"><ol><li> <span class="c"># cpan -i Lingua::JA::Romanize::Japanese</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Lingua::JA::Romanize::Japanese</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$k2r</span> = <span class="w">Lingua::JA::Romanize::Japanese</span><span class="w">-&gt;new</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$str</span> = <span class="q">&quot;&#x6771;&#x4EAC;&quot;</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;Japanese for $str is &quot;</span><span class="cm">,</span> <span class="i">$k2r</span><span class="i">-&gt;chars</span><span class="s">(</span><span class="i">$str</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>prints</p>
461 462 463 464 465 466 467
<pre class="verbatim"><ol><li> <span class="w">Japanese</span> <a class="l_k" href="functions/for.html">for</a> <span class="w">&#x6771;&#x4EAC;</span> <span class="w">is</span> <span class="w">toukyou</span></li></ol></pre><a name="%211e-12%3a-Explicit-encode%2fdecode"></a><h2>&#x211e; 12: Explicit encode/decode</h2>
<p>On rare occasion, such as a database read, you may be
given encoded text you need to decode.</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">Encode</span> <span class="q">qw(encode decode)</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$chars</span> = <span class="i">decode</span><span class="s">(</span><span class="q">&quot;shiftjis&quot;</span><span class="cm">,</span> <span class="i">$bytes</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="c"># OR</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$bytes</span> = <span class="i">encode</span><span class="s">(</span><span class="q">&quot;MIME-Header-ISO_2022_JP&quot;</span><span class="cm">,</span> <span class="i">$chars</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>For streams all in the same encoding, don't use encode/decode; instead
set the file encoding when you open the file or immediately after with
<code class="inline"><a class="l_k" href="functions/binmode.html">binmode</a></code> as described later below.</p>
<a name="%211e-13%3a-Decode-program-arguments-as-utf8"></a><h2>&#x211e; 13: Decode program arguments as utf8</h2>
468
<pre class="verbatim"><ol><li>     <span class="i">$ perl</span> -<span class="w">CA</span> ...</li><li> <a class="l_k" href="functions/or.html">or</a></li><li>     <span class="i">$ export</span> <span class="w">PERL_UNICODE</span>=<span class="w">A</span></li><li> <a class="l_k" href="functions/or.html">or</a></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Encode</span> <span class="q">qw(decode)</span><span class="sc">;</span></li><li>    <span class="i">@ARGV</span> = <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> <span class="i">decode</span><span class="s">(</span><span class="q">&#39;UTF-8&#39;</span><span class="cm">,</span> <span class="i">$_</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span> <span class="s">}</span> <span class="i">@ARGV</span><span class="sc">;</span></li></ol></pre><a name="%211e-14%3a-Decode-program-arguments-as-locale-encoding"></a><h2>&#x211e; 14: Decode program arguments as locale encoding</h2>
469 470 471
<pre class="verbatim"><ol><li>    <span class="c"># cpan -i Encode::Locale</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Encode</span> <span class="q">qw(locale)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Encode::Locale</span><span class="sc">;</span></li><li></li><li>    <span class="c"># use &quot;locale&quot; as an arg to encode/decode</span></li><li>    <span class="i">@ARGV</span> = <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> <span class="i">decode</span><span class="s">(</span><span class="w">locale</span> <span class="cm">=&gt;</span> <span class="i">$_</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span> <span class="s">}</span> <span class="i">@ARGV</span><span class="sc">;</span></li></ol></pre><a name="%211e-15%3a-Declare-STD%7bIN%2cOUT%2cERR%7d-to-be-utf8"></a><h2>&#x211e; 15: Declare STD{IN,OUT,ERR} to be utf8</h2>
<p>Use a command-line option, an environment variable, or else
call <code class="inline"><a class="l_k" href="functions/binmode.html">binmode</a></code> explicitly:</p>
472
<pre class="verbatim"><ol><li>     <span class="i">$ perl</span> -<span class="w">CS</span> ...</li><li> <a class="l_k" href="functions/or.html">or</a></li><li>     <span class="i">$ export</span> <span class="w">PERL_UNICODE</span>=<span class="w">S</span></li><li> <a class="l_k" href="functions/or.html">or</a></li><li>     <a class="l_k" href="functions/use.html">use</a> <a class="l_k" href="functions/open.html">open</a> <span class="q">qw(:std :encoding(UTF-8))</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/or.html">or</a></li><li>     <a class="l_k" href="functions/binmode.html">binmode</a><span class="s">(</span><span class="w">STDIN</span><span class="cm">,</span>  <span class="q">&quot;:encoding(UTF-8)&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/binmode.html">binmode</a><span class="s">(</span><span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&quot;:utf8&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/binmode.html">binmode</a><span class="s">(</span><span class="w">STDERR</span><span class="cm">,</span> <span class="q">&quot;:utf8&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="%211e-16%3a-Declare-STD%7bIN%2cOUT%2cERR%7d-to-be-in-locale-encoding"></a><h2>&#x211e; 16: Declare STD{IN,OUT,ERR} to be in locale encoding</h2>
473 474
<pre class="verbatim"><ol><li>    <span class="c"># cpan -i Encode::Locale</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Encode</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Encode::Locale</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or as a stream for binmode or open</span></li><li>    <a class="l_k" href="functions/binmode.html">binmode</a> <span class="w">STDIN</span><span class="cm">,</span>  <span class="q">&quot;:encoding(console_in)&quot;</span>  <a class="l_k" href="functions/if.html">if</a> -t <span class="w">STDIN</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/binmode.html">binmode</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&quot;:encoding(console_out)&quot;</span> <a class="l_k" href="functions/if.html">if</a> -t <span class="w">STDOUT</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/binmode.html">binmode</a> <span class="w">STDERR</span><span class="cm">,</span> <span class="q">&quot;:encoding(console_out)&quot;</span> <a class="l_k" href="functions/if.html">if</a> -t <span class="w">STDERR</span><span class="sc">;</span></li></ol></pre><a name="%211e-17%3a-Make-file-I%2fO-default-to-utf8"></a><h2>&#x211e; 17: Make file I/O default to utf8</h2>
<p>Files opened without an encoding argument will be in UTF-8:</p>
475 476
<pre class="verbatim"><ol><li>     <span class="i">$ perl</span> -<span class="w">CD</span> ...</li><li> <a class="l_k" href="functions/or.html">or</a></li><li>     <span class="i">$ export</span> <span class="w">PERL_UNICODE</span>=<span class="w">D</span></li><li> <a class="l_k" href="functions/or.html">or</a></li><li>     <a class="l_k" href="functions/use.html">use</a> <a class="l_k" href="functions/open.html">open</a> <span class="q">qw(:encoding(UTF-8))</span><span class="sc">;</span></li></ol></pre><a name="%211e-18%3a-Make-all-I%2fO-and-args-default-to-utf8"></a><h2>&#x211e; 18: Make all I/O and args default to utf8</h2>
<pre class="verbatim"><ol><li>     <span class="i">$ perl</span> -<span class="w">CSDA</span> ...</li><li> <a class="l_k" href="functions/or.html">or</a></li><li>     <span class="i">$ export</span> <span class="w">PERL_UNICODE</span>=<span class="w">SDA</span></li><li> <a class="l_k" href="functions/or.html">or</a></li><li>     <a class="l_k" href="functions/use.html">use</a> <a class="l_k" href="functions/open.html">open</a> <span class="q">qw(:std :encoding(UTF-8))</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/use.html">use</a> <span class="w">Encode</span> <span class="q">qw(decode)</span><span class="sc">;</span></li><li>     <span class="i">@ARGV</span> = <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> <span class="i">decode</span><span class="s">(</span><span class="q">&#39;UTF-8&#39;</span><span class="cm">,</span> <span class="i">$_</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span> <span class="s">}</span> <span class="i">@ARGV</span><span class="sc">;</span></li></ol></pre><a name="%211e-19%3a-Open-file-with-specific-encoding"></a><h2>&#x211e; 19: Open file with specific encoding</h2>
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
<p>Specify stream encoding.  This is the normal way
to deal with encoded text, not by calling low-level
functions.</p>
<pre class="verbatim"><ol><li> <span class="c"># input file</span></li><li>     <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$in_file</span><span class="cm">,</span> <span class="q">&quot;&lt; :encoding(UTF-16)&quot;</span><span class="cm">,</span> <span class="q">&quot;wintext&quot;</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="w">OR</span></li><li>     <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$in_file</span><span class="cm">,</span> <span class="q">&quot;&lt;&quot;</span><span class="cm">,</span> <span class="q">&quot;wintext&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/binmode.html">binmode</a><span class="s">(</span><span class="i">$in_file</span><span class="cm">,</span> <span class="q">&quot;:encoding(UTF-16)&quot;</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="w">THEN</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$line</span> = <span class="q">&lt;$in_file&gt;</span><span class="sc">;</span></li><li></li><li> <span class="c"># output file</span></li><li>     <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="i">$out_file</span><span class="cm">,</span> <span class="q">&quot;&gt; :encoding(cp1252)&quot;</span><span class="cm">,</span> <span class="q">&quot;wintext&quot;</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="w">OR</span></li><li>     <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$out_file</span><span class="cm">,</span> <span class="q">&quot;&gt;&quot;</span><span class="cm">,</span> <span class="q">&quot;wintext&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/binmode.html">binmode</a><span class="s">(</span><span class="i">$out_file</span><span class="cm">,</span> <span class="q">&quot;:encoding(cp1252)&quot;</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="w">THEN</span></li><li>     <a class="l_k" href="functions/print.html">print</a> <span class="i">$out_file</span> <span class="q">&quot;some text\n&quot;</span><span class="sc">;</span></li></ol></pre><p>More layers than just the encoding can be specified here. For example,
the incantation <code class="inline"><span class="q">&quot;:raw :encoding(UTF-16LE) :crlf&quot;</span></code>
 includes implicit
CRLF handling.</p>
<a name="%211e-20%3a-Unicode-casing"></a><h2>&#x211e; 20: Unicode casing</h2>
<p>Unicode casing is very different from ASCII casing.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/uc.html">uc</a><span class="s">(</span><span class="q">&quot;henry &#x2177;&quot;</span><span class="s">)</span>  <span class="c"># &quot;HENRY &#x2167;&quot;</span></li><li> <a class="l_k" href="functions/uc.html">uc</a><span class="s">(</span><span class="q">&quot;tsch&uuml;&szlig;&quot;</span><span class="s">)</span>   <span class="c"># &quot;TSCH&Uuml;SS&quot;  notice &szlig; =&gt; SS</span></li><li></li><li> <span class="c"># both are true:</span></li><li> <span class="q">&quot;tsch&uuml;&szlig;&quot;</span>  =~ <span class="q">/TSCH&Uuml;SS/i</span>   <span class="c"># notice &szlig; =&gt; SS</span></li><li> <span class="q">&quot;&Sigma;&#x3AF;&sigma;&upsilon;&phi;&omicron;&sigmaf;&quot;</span> =~ <span class="q">/&Sigma;&#x38A;&Sigma;&Upsilon;&Phi;&Omicron;&Sigma;/i</span>   <span class="c"># notice &Sigma;,&sigma;,&sigmaf; sameness</span></li></ol></pre><a name="%211e-21%3a-Unicode-case-insensitive-comparisons"></a><h2>&#x211e; 21: Unicode case-insensitive comparisons</h2>
<p>Also available in the CPAN <a href="http://search.cpan.org/perldoc/Unicode::CaseFold">Unicode::CaseFold</a> module,
the new <code class="inline"><a class="l_k" href="functions/fc.html">fc</a></code> &#x201c;foldcase&#x201d; function from v5.16 grants
access to the same Unicode casefolding as the <code class="inline">/i</code>
pattern modifier has always used:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">feature</span> <span class="q">&quot;fc&quot;</span><span class="sc">;</span> <span class="c"># fc() function is from v5.16</span></li><li></li><li> <span class="c"># sort case-insensitively</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@sorted</span> = <a class="l_k" href="functions/sort.html">sort</a> <span class="s">{</span> <span class="i">fc</span><span class="s">(</span><span class="i">$a</span><span class="s">)</span> <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">fc</span><span class="s">(</span><span class="i">$b</span><span class="s">)</span> <span class="s">}</span> <span class="i">@list</span><span class="sc">;</span></li><li></li><li> <span class="c"># both are true:</span></li><li> <span class="i">fc</span><span class="s">(</span><span class="q">&quot;tsch&uuml;&szlig;&quot;</span><span class="s">)</span>  <a class="l_k" href="functions/eq.html">eq</a> <span class="i">fc</span><span class="s">(</span><span class="q">&quot;TSCH&Uuml;SS&quot;</span><span class="s">)</span></li><li> <span class="i">fc</span><span class="s">(</span><span class="q">&quot;&Sigma;&#x3AF;&sigma;&upsilon;&phi;&omicron;&sigmaf;&quot;</span><span class="s">)</span> <a class="l_k" href="functions/eq.html">eq</a> <span class="i">fc</span><span class="s">(</span><span class="q">&quot;&Sigma;&#x38A;&Sigma;&Upsilon;&Phi;&Omicron;&Sigma;&quot;</span><span class="s">)</span></li></ol></pre><a name="%211e-22%3a-Match-Unicode-linebreak-sequence-in-regex"></a><h2>&#x211e; 22: Match Unicode linebreak sequence in regex</h2>
<p>A Unicode linebreak matches the two-character CRLF
grapheme or any of seven vertical whitespace characters.
Good for dealing with textfiles coming from different
operating systems.</p>
<pre class="verbatim"><ol><li> \<span class="w">R</span></li><li></li><li> <span class="q">s/\R/\n/g</span><span class="sc">;</span>  <span class="c"># normalize all linebreaks to \n</span></li></ol></pre><a name="%211e-23%3a-Get-character-category"></a><h2>&#x211e; 23: Get character category</h2>
<p>Find the general category of a numeric codepoint.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::UCD</span> <span class="q">qw(charinfo)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$cat</span> = <span class="i">charinfo</span><span class="s">(</span><span class="n">0x3A3</span><span class="s">)</span>-&gt;{<span class="w">category</span>}<span class="sc">;</span>  <span class="c"># &quot;Lu&quot;</span></li></ol></pre><a name="%211e-24%3a-Disabling-Unicode-awareness-in-builtin-charclasses"></a><h2>&#x211e; 24: Disabling Unicode-awareness in builtin charclasses</h2>
<p>Disable <code class="inline">\<span class="w">w</span></code>
, <code class="inline">\<span class="w">b</span></code>
, <code class="inline">\s</code>, <code class="inline">\<span class="w">d</span></code>
, and the POSIX
classes from working correctly on Unicode either in this
scope, or in just one regex.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.14</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">re</span> <span class="q">&quot;/a&quot;</span><span class="sc">;</span></li><li></li><li> <span class="c"># OR</span></li><li></li><li> <a class="l_k" href="functions/my.html">my</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span> = <span class="i">$str</span> =~ <span class="q">/(\d+)/</span><span class="w">a</span><span class="sc">;</span></li></ol></pre><p>Or use specific un-Unicode properties, like <code class="inline">\<span class="i">p</span><span class="s">{</span><span class="w">ahex</span><span class="s">}</span></code>

and <code class="inline">\<span class="i">p</span><span class="s">{</span><span class="w">POSIX_Digit</span></code>
}.  Properties still work normally
no matter what charset modifiers (<code class="inline"><span class="q">/d /</span><span class="w">u</span> /<span class="w">l</span> /<span class="w">a</span> /<span class="w">aa</span></code>
)
should be effect.</p>
<a name="%211e-25%3a-Match-Unicode-properties-in-regex-with-%5cp%2c-%5cP"></a><h2>&#x211e; 25: Match Unicode properties in regex with \p, \P</h2>
<p>These all match a single codepoint with the given
property.  Use <code class="inline">\<span class="w">P</span></code>
 in place of <code class="inline">\<span class="w">p</span></code>
 to match
one codepoint lacking that property.</p>
518
<pre class="verbatim"><ol><li> \<span class="w">pL</span><span class="cm">,</span> \<span class="w">pN</span><span class="cm">,</span> \<span class="w">pS</span><span class="cm">,</span> \<span class="w">pP</span><span class="cm">,</span> \<span class="w">pM</span><span class="cm">,</span> \<span class="w">pZ</span><span class="cm">,</span> \<span class="w">pC</span></li><li> \<span class="i">p</span><span class="s">{</span><span class="w">Sk</span><span class="s">}</span><span class="cm">,</span> \<span class="i">p</span><span class="s">{</span><span class="w">Ps</span><span class="s">}</span><span class="cm">,</span> \<span class="i">p</span><span class="s">{</span><span class="w">Lt</span><span class="s">}</span></li><li> \<span class="i">p</span><span class="s">{</span><span class="w">alpha</span><span class="s">}</span><span class="cm">,</span> \<span class="i">p</span><span class="s">{</span><span class="w">upper</span><span class="s">}</span><span class="cm">,</span> \<span class="i">p</span><span class="s">{</span><span class="w">lower</span><span class="s">}</span></li><li> \<span class="i">p</span><span class="s">{</span><span class="w">Latin</span><span class="s">}</span><span class="cm">,</span> \<span class="i">p</span><span class="s">{</span><span class="w">Greek</span><span class="s">}</span></li><li> \<span class="i">p</span><span class="s">{</span><span class="w">script_extensions</span>=<span class="w">Latin</span><span class="s">}</span><span class="cm">,</span> \<span class="i">p</span><span class="s">{</span><span class="w">scx</span>=<span class="w">Greek</span><span class="s">}</span></li><li> \<span class="i">p</span><span class="s">{</span><span class="w">East_Asian_Width</span>=<span class="w">Wide</span><span class="s">}</span><span class="cm">,</span> \<span class="i">p</span><span class="s">{</span><span class="w">EA</span>=<span class="w">W</span><span class="s">}</span></li><li> \<span class="i">p</span><span class="s">{</span><span class="w">Line_Break</span>=<span class="w">Hyphen</span><span class="s">}</span><span class="cm">,</span> \<span class="i">p</span><span class="s">{</span><span class="w">LB</span>=<span class="w">HY</span><span class="s">}</span></li><li> \<span class="i">p</span><span class="s">{</span><span class="w">Numeric_Value</span>=<span class="n">4</span><span class="s">}</span><span class="cm">,</span> \<span class="i">p</span><span class="s">{</span><span class="w">NV</span>=<span class="n">4</span><span class="s">}</span></li></ol></pre><a name="%211e-26%3a-Custom-character-properties"></a><h2>&#x211e; 26: Custom character properties</h2>
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
<p>Define at compile-time your own custom character
properties for use in regexes.</p>
<pre class="verbatim"><ol><li> <span class="c"># using private-use characters</span></li><li><a name="In_Tengwar"></a> sub <span class="m">In_Tengwar</span> <span class="s">{</span> <span class="q">&quot;E000\tE07F\n&quot;</span> <span class="s">}</span></li><li></li><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="q">/\p{In_Tengwar}/</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li><li></li><li> <span class="c"># blending existing properties</span></li><li><a name="Is_GraecoRoman_Title"></a> sub <span class="m">Is_GraecoRoman_Title</span> <span class="s">{</span><span class="h">&lt;&lt;&#39;END_OF_SET&#39;</span><span class="s">}</span></li><li><span class="hh"> +utf8::IsLatin</span></li><li><span class="hh"> +utf8::IsGreek</span></li><li><span class="hh"> &amp;utf8::IsTitle</span></li><li><span class="hh"> END_OF_SET</span></li><li></li><li><span class="hh"> if (/\p{Is_GraecoRoman_Title}/ { ... }</span></li></ol></pre><a name="%211e-27%3a-Unicode-normalization"></a><h2>&#x211e; 27: Unicode normalization</h2>
<p>Typically render into NFD on input and NFC on output. Using NFKC or NFKD
functions improves recall on searches, assuming you've already done to the
same text to be searched. Note that this is about much more than just pre-
combined compatibility glyphs; it also reorders marks according to their
canonical combining classes and weeds out singletons.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::Normalize</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$nfd</span>  = <span class="i">NFD</span><span class="s">(</span><span class="i">$orig</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$nfc</span>  = <span class="i">NFC</span><span class="s">(</span><span class="i">$orig</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$nfkd</span> = <span class="i">NFKD</span><span class="s">(</span><span class="i">$orig</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$nfkc</span> = <span class="i">NFKC</span><span class="s">(</span><span class="i">$orig</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="%211e-28%3a-Convert-non-ASCII-Unicode-numerics"></a><h2>&#x211e; 28: Convert non-ASCII Unicode numerics</h2>
<p>Unless you&#x2019;ve used <code class="inline"><span class="q">/a</span></code>
 or <code class="inline"><span class="q">/aa</span></code>
, <code class="inline">\<span class="w">d</span></code>
 matches more than
ASCII digits only, but Perl&#x2019;s implicit string-to-number
conversion does not current recognize these.  Here&#x2019;s how to
convert such strings manually.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.14</span><span class="sc">;</span>  <span class="c"># needed for num() function</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::UCD</span> <span class="q">qw(num)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$str</span> = <span class="q">&quot;got &#x216B; and &#x96A;&#x96B;&#x96C;&#x96D; and &#x215E; and here&quot;</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@nums</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="i">$str</span> =~ <span class="q">/(\d+|\N)/g</span><span class="s">)</span> <span class="s">{</span>  <span class="c"># not just ASCII!</span></li><li>    <a class="l_k" href="functions/push.html">push</a> <span class="i">@nums</span><span class="cm">,</span> <span class="i">num</span><span class="s">(</span><span class="i">$1</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;@nums&quot;</span><span class="sc">;</span>   <span class="c">#     12      4567      0.875</span></li><li></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">charnames</span> <span class="q">qw(:full)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$nv</span> = <span class="i">num</span><span class="s">(</span><span class="q">&quot;\N{RUMI DIGIT ONE}\N{RUMI DIGIT TWO}&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="%211e-29%3a-Match-Unicode-grapheme-cluster-in-regex"></a><h2>&#x211e; 29: Match Unicode grapheme cluster in regex</h2>
<p>Programmer-visible &#x201c;characters&#x201d; are codepoints matched by <code class="inline"><span class="q">/./s</span></code>
,
but user-visible &#x201c;characters&#x201d; are graphemes matched by <code class="inline"><span class="q">/\X/</span></code>
.</p>
<pre class="verbatim"><ol><li> <span class="c"># Find vowel *plus* any combining diacritics,underlining,etc.</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$nfd</span> = <span class="i">NFD</span><span class="s">(</span><span class="i">$orig</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">$nfd</span> =~ <span class="q">/ (?=[aeiou]) \X /xi</span></li></ol></pre><a name="%211e-30%3a-Extract-by-grapheme-instead-of-by-codepoint-(regex)"></a><h2>&#x211e; 30: Extract by grapheme instead of by codepoint (regex)</h2>
<pre class="verbatim"><ol><li> <span class="c"># match and grab five first graphemes</span></li><li> <a class="l_k" href="functions/my.html">my</a><span class="s">(</span><span class="i">$first_five</span><span class="s">)</span> = <span class="i">$str</span> =~ <span class="q">/^ ( \X{5} ) /x</span><span class="sc">;</span></li></ol></pre><a name="%211e-31%3a-Extract-by-grapheme-instead-of-by-codepoint-(substr)"></a><h2>&#x211e; 31: Extract by grapheme instead of by codepoint (substr)</h2>
<pre class="verbatim"><ol><li> <span class="c"># cpan -i Unicode::GCString</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::GCString</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$gcs</span> = <span class="w">Unicode::GCString</span><span class="w">-&gt;new</span><span class="s">(</span><span class="i">$str</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$first_five</span> = <span class="i">$gcs</span><span class="i">-&gt;substr</span><span class="s">(</span><span class="n">0</span><span class="cm">,</span> <span class="n">5</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="%211e-32%3a-Reverse-string-by-grapheme"></a><h2>&#x211e; 32: Reverse string by grapheme</h2>
<p>Reversing by codepoint messes up diacritics, mistakenly converting
<code class="inline"><span class="w">cr&egrave;me</span> <span class="w">br&ucirc;l&eacute;e</span></code>
 into <code class="inline"><span class="w">&eacute;el&#x302;urb</span> <span class="w">em&#x300;erc</span></code>
 instead of into <code class="inline"><span class="w">e&eacute;l&ucirc;rb</span> <span class="w">em&egrave;rc</span></code>
;
so reverse by grapheme instead.  Both these approaches work
right no matter what normalization the string is in:</p>
<pre class="verbatim"><ol><li> <span class="i">$str</span> = <a class="l_k" href="functions/join.html">join</a><span class="s">(</span><span class="q">&quot;&quot;</span><span class="cm">,</span> <a class="l_k" href="functions/reverse.html">reverse</a> <span class="i">$str</span> =~ <span class="q">/\X/g</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="c"># OR: cpan -i Unicode::GCString</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::GCString</span><span class="sc">;</span></li><li> <span class="i">$str</span> = <a class="l_k" href="functions/reverse.html">reverse</a> <span class="w">Unicode::GCString</span><span class="w">-&gt;new</span><span class="s">(</span><span class="i">$str</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="%211e-33%3a-String-length-in-graphemes"></a><h2>&#x211e; 33: String length in graphemes</h2>
<p>The string <code class="inline"><span class="w">br&ucirc;l&eacute;e</span></code>
 has six graphemes but up to eight codepoints.
This counts by grapheme, not by codepoint:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$str</span> = <span class="q">&quot;br&ucirc;l&eacute;e&quot;</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$count</span> = <span class="n">0</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="i">$str</span> =~ <span class="q">/\X/g</span><span class="s">)</span> <span class="s">{</span> <span class="i">$count</span>++ <span class="s">}</span></li><li></li><li>  <span class="c"># OR: cpan -i Unicode::GCString</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::GCString</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$gcs</span> = <span class="w">Unicode::GCString</span><span class="w">-&gt;new</span><span class="s">(</span><span class="i">$str</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$count</span> = <span class="i">$gcs</span><span class="i">-&gt;length</span><span class="sc">;</span></li></ol></pre><a name="%211e-34%3a-Unicode-column-width-for-printing"></a><h2>&#x211e; 34: Unicode column-width for printing</h2>
<p>Perl&#x2019;s <code class="inline"><a class="l_k" href="functions/printf.html">printf</a></code>, <code class="inline"><a class="l_k" href="functions/sprintf.html">sprintf</a></code>, and <code class="inline"><a class="l_k" href="functions/format.html">format</a></code> think all
codepoints take up 1 print column, but many take 0 or 2.
Here to show that normalization makes no difference,
we print out both forms:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::GCString</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::Normalize</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@words</span> = <span class="q">qw/cr&egrave;me br&ucirc;l&eacute;e/</span><span class="sc">;</span></li><li> <span class="i">@words</span> = <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> <span class="i">NFC</span><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="cm">,</span> <span class="i">NFD</span><span class="s">(</span><span class="i">$_</span><span class="s">)</span> <span class="s">}</span> <span class="i">@words</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/for.html">for</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$str</span> <span class="s">(</span><span class="i">@words</span><span class="s">)</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$gcs</span> = <span class="w">Unicode::GCString</span><span class="w">-&gt;new</span><span class="s">(</span><span class="i">$str</span><span class="s">)</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$cols</span> = <span class="i">$gcs</span><span class="i">-&gt;columns</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$pad</span> = <span class="q">&quot; &quot;</span> x <span class="s">(</span><span class="n">10</span> - <span class="i">$cols</span><span class="s">)</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/say.html">say</a> <span class="i">str</span><span class="cm">,</span> <span class="i">$pad</span><span class="cm">,</span> <span class="q">&quot; |&quot;</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>generates this to show that it pads correctly no matter
the normalization:</p>
<pre class="verbatim"><ol><li> <span class="w">cr&egrave;me</span>      |</li><li> <span class="w">cre&#x300;me</span>      |</li><li> <span class="w">br&ucirc;l&eacute;e</span>     |</li><li> <span class="w">bru&#x302;le&#x301;e</span>     |</li></ol></pre><a name="%211e-35%3a-Unicode-collation"></a><h2>&#x211e; 35: Unicode collation</h2>
<p>Text sorted by numeric codepoint follows no reasonable alphabetic order;
use the UCA for sorting text.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::Collate</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$col</span> = <span class="w">Unicode::Collate</span><span class="w">-&gt;new</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@list</span> = <span class="i">$col</span><span class="i">-&gt;sort</span><span class="s">(</span><span class="i">@old_list</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>See the <i>ucsort</i> program from the <a href="http://search.cpan.org/perldoc/Unicode::Tussle">Unicode::Tussle</a> CPAN module
for a convenient command-line interface to this module.</p>
<a name="%211e-36%3a-Case--_and_-accent-insensitive-Unicode-sort"></a><h2>&#x211e; 36: Case- <i>and</i> accent-insensitive Unicode sort</h2>
<p>Specify a collation strength of level 1 to ignore case and
diacritics, only looking at the basic character.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::Collate</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$col</span> = <span class="w">Unicode::Collate</span><span class="w">-&gt;new</span><span class="s">(</span><span class="w">level</span> <span class="cm">=&gt;</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@list</span> = <span class="i">$col</span><span class="i">-&gt;sort</span><span class="s">(</span><span class="i">@old_list</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="%211e-37%3a-Unicode-locale-collation"></a><h2>&#x211e; 37: Unicode locale collation</h2>
<p>Some locales have special sorting rules.</p>
<pre class="verbatim"><ol><li> <span class="c"># either use v5.12, OR: cpan -i Unicode::Collate::Locale</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::Collate::Locale</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$col</span> = <span class="w">Unicode::Collate::Locale</span><span class="w">-&gt;new</span><span class="s">(</span><span class="w">locale</span> <span class="cm">=&gt;</span> <span class="q">&quot;de__phonebook&quot;</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@list</span> = <span class="i">$col</span><span class="i">-&gt;sort</span><span class="s">(</span><span class="i">@old_list</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The <i>ucsort</i> program mentioned above accepts a <code class="inline">--<span class="w">locale</span></code>
 parameter.</p>
<a name="%211e-38%3a-Making-cmp-work-on-text-instead-of-codepoints"></a><h2>&#x211e; 38: Making <code class="inline">cmp</code>
 work on text instead of codepoints</h2>
<p>Instead of this:</p>
<pre class="verbatim"><ol><li> <span class="i">@srecs</span> = <a class="l_k" href="functions/sort.html">sort</a> <span class="s">{</span></li><li>     <span class="i">$b</span>-&gt;{<span class="w">AGE</span>}   &lt;=&gt;  <span class="i">$a</span>-&gt;{<span class="w">AGE</span>}</li><li>                 ||</li><li>     <span class="i">$a</span>-&gt;{<span class="w">NAME</span>}  <a class="l_k" href="functions/cmp.html">cmp</a>  <span class="i">$b</span>-&gt;{<span class="w">NAME</span>}</li><li> <span class="s">}</span> <span class="i">@recs</span><span class="sc">;</span></li></ol></pre><p>Use this:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$coll</span> = <span class="w">Unicode::Collate</span><span class="w">-&gt;new</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/for.html">for</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$rec</span> <span class="s">(</span><span class="i">@recs</span><span class="s">)</span> <span class="s">{</span></li><li>     <span class="i">$rec</span>-&gt;{<span class="w">NAME_key</span>} = <span class="i">$coll</span><span class="i">-&gt;getSortKey</span><span class="s">(</span> <span class="i">$rec</span>-&gt;{<span class="w">NAME</span>} <span class="s">)</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <span class="i">@srecs</span> = <a class="l_k" href="functions/sort.html">sort</a> <span class="s">{</span></li><li>     <span class="i">$b</span>-&gt;{<span class="w">AGE</span>}       &lt;=&gt;  <span class="i">$a</span>-&gt;{<span class="w">AGE</span>}</li><li>                     ||</li><li>     <span class="i">$a</span>-&gt;{<span class="w">NAME_key</span>}  <a class="l_k" href="functions/cmp.html">cmp</a>  <span class="i">$b</span>-&gt;{<span class="w">NAME_key</span>}</li><li> <span class="s">}</span> <span class="i">@recs</span><span class="sc">;</span></li></ol></pre><a name="%211e-39%3a-Case--_and_-accent-insensitive-comparisons"></a><h2>&#x211e; 39: Case- <i>and</i> accent-insensitive comparisons</h2>
<p>Use a collator object to compare Unicode text by character
instead of by codepoint.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::Collate</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$es</span> = <span class="w">Unicode::Collate</span><span class="w">-&gt;new</span><span class="s">(</span></li><li>     <span class="w">level</span> <span class="cm">=&gt;</span> <span class="n">1</span><span class="cm">,</span></li><li>     <span class="w">normalization</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/undef.html">undef</a></li><li> <span class="s">)</span><span class="sc">;</span></li><li></li><li>  <span class="c"># now both are true:</span></li><li> <span class="i">$es</span><span class="i">-&gt;eq</span><span class="s">(</span><span class="q">&quot;Garc&iacute;a&quot;</span><span class="cm">,</span>  <span class="q">&quot;GARCIA&quot;</span> <span class="s">)</span><span class="sc">;</span></li><li> <span class="i">$es</span><span class="i">-&gt;eq</span><span class="s">(</span><span class="q">&quot;M&aacute;rquez&quot;</span><span class="cm">,</span> <span class="q">&quot;MARQUEZ&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="%211e-40%3a-Case--_and_-accent-insensitive-locale-comparisons"></a><h2>&#x211e; 40: Case- <i>and</i> accent-insensitive locale comparisons</h2>
<p>Same, but in a specific locale.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$de</span> = <span class="w">Unicode::Collate::Locale</span><span class="w">-&gt;new</span><span class="s">(</span></li><li>            <span class="w">locale</span> <span class="cm">=&gt;</span> <span class="q">&quot;de__phonebook&quot;</span><span class="cm">,</span></li><li>          <span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="c"># now this is true:</span></li><li> <span class="i">$de</span><span class="i">-&gt;eq</span><span class="s">(</span><span class="q">&quot;tsch&uuml;&szlig;&quot;</span><span class="cm">,</span> <span class="q">&quot;TSCHUESS&quot;</span><span class="s">)</span><span class="sc">;</span>  <span class="c"># notice &uuml; =&gt; UE, &szlig; =&gt; SS</span></li></ol></pre><a name="%211e-41%3a-Unicode-linebreaking"></a><h2>&#x211e; 41: Unicode linebreaking</h2>
<p>Break up text into lines according to Unicode rules.</p>
584
<pre class="verbatim"><ol><li> <span class="c"># cpan -i Unicode::LineBreak</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::LineBreak</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">charnames</span> <span class="q">qw(:full)</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$para</span> = <span class="q">&quot;This is a super\N{HYPHEN}long string. &quot;</span> x <span class="n">20</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$fmt</span> = <span class="w">Unicode::LineBreak</span><span class="w">-&gt;new</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$fmt</span><span class="i">-&gt;break</span><span class="s">(</span><span class="i">$para</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><a name="%211e-42%3a-Unicode-text-in-DBM-hashes%2c-the-tedious-way"></a><h2>&#x211e; 42: Unicode text in DBM hashes, the tedious way</h2>
585 586 587 588 589 590 591 592 593 594 595 596 597
<p>Using a regular Perl string as a key or value for a DBM
hash will trigger a wide character exception if any codepoints
won&#x2019;t fit into a byte.  Here&#x2019;s how to manually manage the translation:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">DB_File</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Encode</span> <span class="q">qw(encode decode)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/tie.html">tie</a> <span class="i">%dbhash</span><span class="cm">,</span> <span class="q">&quot;DB_File&quot;</span><span class="cm">,</span> <span class="q">&quot;pathname&quot;</span><span class="sc">;</span></li><li></li><li> <span class="c"># STORE</span></li><li></li><li>    <span class="c"># assume $uni_key and $uni_value are abstract Unicode strings</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$enc_key</span>   = <span class="i">encode</span><span class="s">(</span><span class="q">&quot;UTF-8&quot;</span><span class="cm">,</span> <span class="i">$uni_key</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$enc_value</span> = <span class="i">encode</span><span class="s">(</span><span class="q">&quot;UTF-8&quot;</span><span class="cm">,</span> <span class="i">$uni_value</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$dbhash</span>{<span class="i">$enc_key</span>} = <span class="i">$enc_value</span><span class="sc">;</span></li><li></li><li> <span class="c"># FETCH</span></li><li></li><li>    <span class="c"># assume $uni_key holds a normal Perl string (abstract Unicode)</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$enc_key</span>   = <span class="i">encode</span><span class="s">(</span><span class="q">&quot;UTF-8&quot;</span><span class="cm">,</span> <span class="i">$uni_key</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$enc_value</span> = <span class="i">$dbhash</span>{<span class="i">$enc_key</span>}<span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$uni_value</span> = <span class="i">decode</span><span class="s">(</span><span class="q">&quot;UTF-8&quot;</span><span class="cm">,</span> <span class="i">$enc_value</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="%211e-43%3a-Unicode-text-in-DBM-hashes%2c-the-easy-way"></a><h2>&#x211e; 43: Unicode text in DBM hashes, the easy way</h2>
<p>Here&#x2019;s how to implicitly manage the translation; all encoding
and decoding is done automatically, just as with streams that
have a particular encoding attached to them:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">DB_File</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">DBM_Filter</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$dbobj</span> = <a class="l_k" href="functions/tie.html">tie</a> <span class="i">%dbhash</span><span class="cm">,</span> <span class="q">&quot;DB_File&quot;</span><span class="cm">,</span> <span class="q">&quot;pathname&quot;</span><span class="sc">;</span></li><li>    <span class="i">$dbobj</span><span class="i">-&gt;Filter_Value</span><span class="s">(</span><span class="q">&quot;utf8&quot;</span><span class="s">)</span><span class="sc">;</span>  <span class="c"># this is the magic bit</span></li><li></li><li> <span class="c"># STORE</span></li><li></li><li>    <span class="c"># assume $uni_key and $uni_value are abstract Unicode strings</span></li><li>    <span class="i">$dbhash</span>{<span class="i">$uni_key</span>} = <span class="i">$uni_value</span><span class="sc">;</span></li><li></li><li>  <span class="c"># FETCH</span></li><li></li><li>    <span class="c"># $uni_key holds a normal Perl string (abstract Unicode)</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$uni_value</span> = <span class="i">$dbhash</span>{<span class="i">$uni_key</span>}<span class="sc">;</span></li></ol></pre><a name="%211e-44%3a-PROGRAM%3a-Demo-of-Unicode-collation-and-printing"></a><h2>&#x211e; 44: PROGRAM: Demo of Unicode collation and printing</h2>
<p>Here&#x2019;s a full program showing how to make use of locale-sensitive
sorting, Unicode casing, and managing print widths when some of the
characters take up zero or two columns, not just one column each time.
When run, the following program produces this nicely aligned output:</p>
<pre class="verbatim"><ol><li><span class="q">    Cr&egrave;me Br&ucirc;l&eacute;e....... &euro;2.00</span></li><li><span class="q">    &Eacute;clair............. &euro;1.60</span></li><li><span class="q">    Fideu&agrave;............. &euro;4.20</span></li><li><span class="q">    Hamburger.......... &euro;6.00</span></li><li><span class="q">    Jam&oacute;n Serrano...... &euro;4.45</span></li><li><span class="q">    Lingui&ccedil;a........... &euro;7.00</span></li><li><span class="q">    P&acirc;t&eacute;............... &euro;4.15</span></li><li><span class="q">    Pears.............. &euro;2.00</span></li><li><span class="q">    P&ecirc;ches............. &euro;2.25</span></li><li><span class="q">    Sm&oslash;rbr&oslash;d........... &euro;5.75</span></li><li><span class="q">    Sp&auml;tzle............ &euro;5.50</span></li><li><span class="q">    Xori&ccedil;o............. &euro;3.00</span></li><li><span class="q">    &Gamma;&#x3CD;&rho;&omicron;&sigmaf;.............. &euro;6.50</span></li><li><span class="q">    &#xB9C9;&#xAC78;&#xB9AC;............. &euro;4.00</span></li><li><span class="q">    &#x304A;&#x3082;&#x3061;............. &euro;2.65</span></li><li><span class="q">    &#x304A;&#x597D;&#x307F;&#x713C;&#x304D;......... &euro;8.00</span></li><li><span class="q">    &#x30B7;&#x30E5;&#x30FC;&#x30AF;&#x30EA;&#x30FC;&#x30E0;..... &euro;1.85</span></li><li><span class="q">    &#x5BFF;&#x53F8;............... &euro;9.99</span></li><li><span class="q">    &#x5305;&#x5B50;............... &euro;7.50</span></li></ol></pre><p>Here's that program; tested on v5.14.</p>
598
<pre class="verbatim"><ol><li> <span class="c">#!/usr/bin/env perl</span></li><li> <span class="c"># umenu - demo sorting and printing of Unicode food</span></li><li> <span class="c">#</span></li><li> <span class="c"># (obligatory and increasingly long preamble)</span></li><li> <span class="c">#</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">utf8</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.14</span><span class="sc">;</span>                       <span class="c"># for locale sorting</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span>  <span class="q">qw(FATAL utf8)</span><span class="sc">;</span>    <span class="c"># fatalize encoding faults</span></li><li> <a class="l_k" href="functions/use.html">use</a> <a class="l_k" href="functions/open.html">open</a>      <span class="q">qw(:std :encoding(UTF-8))</span><span class="sc">;</span> <span class="c"># undeclared streams in UTF-8</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">charnames</span> <span class="q">qw(:full :short)</span><span class="sc">;</span>  <span class="c"># unneeded in v5.16</span></li><li></li><li> <span class="c"># std modules</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::Normalize</span><span class="sc">;</span>          <span class="c"># std perl distro as of v5.8</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">List::Util</span> <span class="q">qw(max)</span><span class="sc">;</span>          <span class="c"># std perl distro as of v5.10</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::Collate::Locale</span><span class="sc">;</span>    <span class="c"># std perl distro as of v5.14</span></li><li></li><li> <span class="c"># cpan modules</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Unicode::GCString</span><span class="sc">;</span>           <span class="c"># from CPAN</span></li><li></li><li> <span class="c"># forward defs</span></li><li> sub <span class="m">pad($$$)</span><span class="sc">;</span></li><li> sub <span class="m">colwidth(_)</span><span class="sc">;</span></li><li> sub <span class="m">entitle(_)</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">%price</span> = <span class="s">(</span></li><li>     <span class="q">&quot;&gamma;&#x3CD;&rho;&omicron;&sigmaf;&quot;</span>             <span class="cm">=&gt;</span> <span class="n">6.50</span><span class="cm">,</span> <span class="c"># gyros</span></li><li>     <span class="q">&quot;pears&quot;</span>             <span class="cm">=&gt;</span> <span class="n">2.00</span><span class="cm">,</span> <span class="c"># like um, pears</span></li><li>     <span class="q">&quot;lingui&ccedil;a&quot;</span>          <span class="cm">=&gt;</span> <span class="n">7.00</span><span class="cm">,</span> <span class="c"># spicy sausage, Portuguese</span></li><li>     <span class="q">&quot;xori&ccedil;o&quot;</span>            <span class="cm">=&gt;</span> <span class="n">3.00</span><span class="cm">,</span> <span class="c"># chorizo sausage, Catalan</span></li><li>     <span class="q">&quot;hamburger&quot;</span>         <span class="cm">=&gt;</span> <span class="n">6.00</span><span class="cm">,</span> <span class="c"># burgermeister meisterburger</span></li><li>     <span class="q">&quot;&eacute;clair&quot;</span>            <span class="cm">=&gt;</span> <span class="n">1.60</span><span class="cm">,</span> <span class="c"># dessert, French</span></li><li>     <span class="q">&quot;sm&oslash;rbr&oslash;d&quot;</span>          <span class="cm">=&gt;</span> <span class="n">5.75</span><span class="cm">,</span> <span class="c"># sandwiches, Norwegian</span></li><li>     <span class="q">&quot;sp&auml;tzle&quot;</span>           <span class="cm">=&gt;</span> <span class="n">5.50</span><span class="cm">,</span> <span class="c"># Bayerisch noodles, little sparrows</span></li><li>     <span class="q">&quot;&#x5305;&#x5B50;&quot;</span>              <span class="cm">=&gt;</span> <span class="n">7.50</span><span class="cm">,</span> <span class="c"># bao1 zi5, steamed pork buns, Mandarin</span></li><li>     <span class="q">&quot;jam&oacute;n serrano&quot;</span>     <span class="cm">=&gt;</span> <span class="n">4.45</span><span class="cm">,</span> <span class="c"># country ham, Spanish</span></li><li>     <span class="q">&quot;p&ecirc;ches&quot;</span>            <span class="cm">=&gt;</span> <span class="n">2.25</span><span class="cm">,</span> <span class="c"># peaches, French</span></li><li>     <span class="q">&quot;&#x30B7;&#x30E5;&#x30FC;&#x30AF;&#x30EA;&#x30FC;&#x30E0;&quot;</span>    <span class="cm">=&gt;</span> <span class="n">1.85</span><span class="cm">,</span> <span class="c"># cream-filled pastry like eclair</span></li><li>     <span class="q">&quot;&#xB9C9;&#xAC78;&#xB9AC;&quot;</span>            <span class="cm">=&gt;</span> <span class="n">4.00</span><span class="cm">,</span> <span class="c"># makgeolli, Korean rice wine</span></li><li>     <span class="q">&quot;&#x5BFF;&#x53F8;&quot;</span>              <span class="cm">=&gt;</span> <span class="n">9.99</span><span class="cm">,</span> <span class="c"># sushi, Japanese</span></li><li>     <span class="q">&quot;&#x304A;&#x3082;&#x3061;&quot;</span>            <span class="cm">=&gt;</span> <span class="n">2.65</span><span class="cm">,</span> <span class="c"># omochi, rice cakes, Japanese</span></li><li>     <span class="q">&quot;cr&egrave;me br&ucirc;l&eacute;e&quot;</span>      <span class="cm">=&gt;</span> <span class="n">2.00</span><span class="cm">,</span> <span class="c"># crema catalana</span></li><li>     <span class="q">&quot;fideu&agrave;&quot;</span>            <span class="cm">=&gt;</span> <span class="n">4.20</span><span class="cm">,</span> <span class="c"># more noodles, Valencian</span></li><li>                                  <span class="c"># (Catalan=fideuada)</span></li><li>     <span class="q">&quot;p&acirc;t&eacute;&quot;</span>              <span class="cm">=&gt;</span> <span class="n">4.15</span><span class="cm">,</span> <span class="c"># gooseliver paste, French</span></li><li>     <span class="q">&quot;&#x304A;&#x597D;&#x307F;&#x713C;&#x304D;&quot;</span>        <span class="cm">=&gt;</span> <span class="n">8.00</span><span class="cm">,</span> <span class="c"># okonomiyaki, Japanese</span></li><li> <span class="s">)</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$width</span> = <span class="n">5</span> + <span class="w">max</span> <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> <span class="i">colwidth</span> <span class="s">}</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%price</span><span class="sc">;</span></li><li></li><li> <span class="c"># So the Asian stuff comes out in an order that someone</span></li><li> <span class="c"># who reads those scripts won&#39;t freak out over; the</span></li><li> <span class="c"># CJK stuff will be in JIS X 0208 order that way.</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$coll</span>  = <span class="w">Unicode::Collate::Locale</span><span class="w">-&gt;new</span><span class="s">(</span><span class="w">locale</span> <span class="cm">=&gt;</span> <span class="q">&quot;ja&quot;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/for.html">for</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$item</span> <span class="s">(</span><span class="i">$coll</span><span class="i">-&gt;sort</span><span class="s">(</span><a class="l_k" href="functions/keys.html">keys</a> <span class="i">%price</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/print.html">print</a> <span class="i">pad</span><span class="s">(</span><span class="i">entitle</span><span class="s">(</span><span class="i">$item</span><span class="s">)</span><span class="cm">,</span> <span class="i">$width</span><span class="cm">,</span> <span class="q">&quot;.&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot; &euro;%.2f\n&quot;</span><span class="cm">,</span> <span class="i">$price</span>{<span class="i">$item</span>}<span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li><a name="pad"></a> sub <span class="m">pad($$$)</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/my.html">my</a><span class="s">(</span><span class="i">$str</span><span class="cm">,</span> <span class="i">$width</span><span class="cm">,</span> <span class="i">$padchar</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/return.html">return</a> <span class="i">$str</span> . <span class="s">(</span><span class="i">$padchar</span> x <span class="s">(</span><span class="i">$width</span> - <span class="i">colwidth</span><span class="s">(</span><span class="i">$str</span><span class="s">)</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li><a name="colwidth"></a> sub <span class="m">colwidth(_)</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/my.html">my</a><span class="s">(</span><span class="i">$str</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/return.html">return</a> <span class="w">Unicode::GCString</span><span class="w">-&gt;new</span><span class="s">(</span><span class="i">$str</span><span class="s">)</span><span class="i">-&gt;columns</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li><a name="entitle"></a> sub <span class="m">entitle(_)</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/my.html">my</a><span class="s">(</span><span class="i">$str</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>     <span class="i">$str</span> =~ <span class="q">s{ (?=\pL)(\S)     (\S*) }</span></li><li>              <span class="q">              { ucfirst($1) . lc($2)  }xge</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/return.html">return</a> <span class="i">$str</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
<p>See these manpages, some of which are CPAN modules:
<a href="perlunicode.html">perlunicode</a>, <a href="perluniprops.html">perluniprops</a>,
<a href="perlre.html">perlre</a>, <a href="perlrecharclass.html">perlrecharclass</a>,
<a href="perluniintro.html">perluniintro</a>, <a href="perlunitut.html">perlunitut</a>, <a href="perlunifaq.html">perlunifaq</a>,
<a href="PerlIO.html">PerlIO</a>, <a href="DB_File.html">DB_File</a>, <a href="DBM_Filter.html">DBM_Filter</a>, <a href="DBM_Filter/utf8.html">DBM_Filter::utf8</a>,
<a href="Encode.html">Encode</a>, <a href="http://search.cpan.org/perldoc/Encode::Locale">Encode::Locale</a>,
<a href="Unicode/UCD.html">Unicode::UCD</a>,
<a href="Unicode/Normalize.html">Unicode::Normalize</a>,
<a href="http://search.cpan.org/perldoc/Unicode::GCString">Unicode::GCString</a>, <a href="http://search.cpan.org/perldoc/Unicode::LineBreak">Unicode::LineBreak</a>,
<a href="Unicode/Collate.html">Unicode::Collate</a>, <a href="Unicode/Collate/Locale.html">Unicode::Collate::Locale</a>,
<a href="http://search.cpan.org/perldoc/Unicode::Unihan">Unicode::Unihan</a>,
<a href="http://search.cpan.org/perldoc/Unicode::CaseFold">Unicode::CaseFold</a>,
<a href="http://search.cpan.org/perldoc/Unicode::Tussle">Unicode::Tussle</a>,
<a href="http://search.cpan.org/perldoc/Lingua::JA::Romanize::Japanese">Lingua::JA::Romanize::Japanese</a>,
<a href="http://search.cpan.org/perldoc/Lingua::ZH::Romanize::Pinyin">Lingua::ZH::Romanize::Pinyin</a>,
<a href="http://search.cpan.org/perldoc/Lingua::KO::Romanize::Hangul">Lingua::KO::Romanize::Hangul</a>.</p>
<p>The <a href="http://search.cpan.org/perldoc/Unicode::Tussle">Unicode::Tussle</a> CPAN module includes many programs
to help with working with Unicode, including
these programs to fully or partly replace standard utilities:
<i>tcgrep</i> instead of <i>egrep</i>,
<i>uniquote</i> instead of <i>cat -v</i> or <i>hexdump</i>,
<i>uniwc</i> instead of <i>wc</i>,
<i>unilook</i> instead of <i>look</i>,
<i>unifmt</i> instead of <i>fmt</i>,
and
<i>ucsort</i> instead of <i>sort</i>.
For exploring Unicode character names and character properties,
see its <i>uniprops</i>, <i>unichars</i>, and <i>uninames</i> programs.
It also supplies these programs, all of which are general filters that do Unicode-y things:
<i>unititle</i> and <i>unicaps</i>;
<i>uniwide</i> and <i>uninarrow</i>;
<i>unisupers</i> and <i>unisubs</i>;
<i>nfd</i>, <i>nfc</i>, <i>nfkd</i>, and <i>nfkc</i>;
and <i>uc</i>, <i>lc</i>, and <i>tc</i>.</p>
<p>Finally, see the published Unicode Standard (page numbers are from version
6.0.0), including these specific annexes and technical reports:</p>
<ul>
<li><a name="%a73.13-Default-Case-Algorithms%2c-page-113%3b-%a74.2--Case%2c-pages-120%2013122%3b-Case-Mappings%2c-page-166%2013172%2c-especially-Caseless-Matching-starting-on-page-170."></a><b>&#xa7;3.13 Default Case Algorithms, page 113;
&#xa7;4.2  Case, pages 120&#x2013;122;
Case Mappings, page 166&#x2013;172, especially Caseless Matching starting on page 170.</b>
</li>
<li><a name="UAX-%2344%3a-Unicode-Character-Database"></a><b>UAX #44: Unicode Character Database</b>
</li>
<li><a name="UTS-%2318%3a-Unicode-Regular-Expressions"></a><b>UTS #18: Unicode Regular Expressions</b>
</li>
<li><a name="UAX-%2315%3a-Unicode-Normalization-Forms"></a><b>UAX #15: Unicode Normalization Forms</b>
</li>
<li><a name="UTS-%2310%3a-Unicode-Collation-Algorithm"></a><b>UTS #10: Unicode Collation Algorithm</b>
</li>
<li><a name="UAX-%2329%3a-Unicode-Text-Segmentation"></a><b>UAX #29: Unicode Text Segmentation</b>
</li>
<li><a name="UAX-%2314%3a-Unicode-Line-Breaking-Algorithm"></a><b>UAX #14: Unicode Line Breaking Algorithm</b>
</li>
<li><a name="UAX-%2311%3a-East-Asian-Width"></a><b>UAX #11: East Asian Width</b>
</li>
</ul>
<a name="AUTHOR"></a><h1>AUTHOR</h1>
<p>Tom Christiansen &lt;tchrist@perl.com&gt; wrote this, with occasional
kibbitzing from Larry Wall and Jeffrey Friedl in the background.</p>
<a name="COPYRIGHT-AND-LICENCE"></a><h1>COPYRIGHT AND LICENCE</h1>
<p>Copyright &#xa9; 2012 Tom Christiansen.</p>
<p>This program is free software; you may redistribute it and/or modify it
under the same terms as Perl itself.</p>
<p>Most of these examples taken from the current edition of the &#x201c;Camel Book&#x201d;;
that is, from the 4&#x1d57;&#x2b0; Edition of <i>Programming Perl</i>, Copyright &#xa9; 2012 Tom
Christiansen &lt;et al.&gt;, 2012-02-13 by O&#x2019;Reilly Media.  The code itself is
freely redistributable, and you are encouraged to transplant, fold,
spindle, and mutilate any of the examples in this manpage however you please
for inclusion into your own programs without any encumbrance whatsoever.
Acknowledgement via code comment is polite but not required.</p>
<a name="REVISION-HISTORY"></a><h1>REVISION HISTORY</h1>
<p>v1.0.0 &#x2013; first public release, 2012-02-27</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#EXAMPLES">EXAMPLES</a><ul><li><a href="#%211e-0%3a-Standard-preamble">℞ 0: Standard preamble</a><li><a href="#%211e-1%3a-Generic-Unicode-savvy-filter">℞ 1: Generic Unicode-savvy filter</a><li><a href="#%211e-2%3a-Fine-tuning-Unicode-warnings">℞ 2: Fine-tuning Unicode warnings</a><li><a href="#%211e-3%3a-Declare-source-in-utf8-for-identifiers-and-literals">℞ 3: Declare source in utf8 for identifiers and literals</a><li><a href="#%211e-4%3a-Characters-and-their-numbers">℞ 4: Characters and their numbers</a><li><a href="#%211e-5%3a-Unicode-literals-by-character-number">℞ 5: Unicode literals by character number</a><li><a href="#%211e-6%3a-Get-character-name-by-number">℞ 6: Get character name by number</a><li><a href="#%211e-7%3a-Get-character-number-by-name">℞ 7: Get character number by name</a><li><a href="#%211e-8%3a-Unicode-named-characters">℞ 8: Unicode named characters</a><li><a href="#%211e-9%3a-Unicode-named-sequences">℞ 9: Unicode named sequences</a><li><a href="#%211e-10%3a-Custom-named-characters">℞ 10: Custom named characters</a><li><a href="#%211e-11%3a-Names-of-CJK-codepoints">℞ 11: Names of CJK codepoints</a><li><a href="#%211e-12%3a-Explicit-encode%2fdecode">℞ 12: Explicit encode/decode</a><li><a href="#%211e-13%3a-Decode-program-arguments-as-utf8">℞ 13: Decode program arguments as utf8</a><li><a href="#%211e-14%3a-Decode-program-arguments-as-locale-encoding">℞ 14: Decode program arguments as locale encoding</a><li><a href="#%211e-15%3a-Declare-STD%7bIN%2cOUT%2cERR%7d-to-be-utf8">℞ 15: Declare STD{IN,OUT,ERR} to be utf8</a><li><a href="#%211e-16%3a-Declare-STD%7bIN%2cOUT%2cERR%7d-to-be-in-locale-encoding">℞ 16: Declare STD{IN,OUT,ERR} to be in locale encoding</a><li><a href="#%211e-17%3a-Make-file-I%2fO-default-to-utf8">℞ 17: Make file I/O default to utf8</a><li><a href="#%211e-18%3a-Make-all-I%2fO-and-args-default-to-utf8">℞ 18: Make all I/O and args default to utf8</a><li><a href="#%211e-19%3a-Open-file-with-specific-encoding">℞ 19: Open file with specific encoding</a><li><a href="#%211e-20%3a-Unicode-casing">℞ 20: Unicode casing</a><li><a href="#%211e-21%3a-Unicode-case-insensitive-comparisons">℞ 21: Unicode case-insensitive comparisons</a><li><a href="#%211e-22%3a-Match-Unicode-linebreak-sequence-in-regex">℞ 22: Match Unicode linebreak sequence in regex</a><li><a href="#%211e-23%3a-Get-character-category">℞ 23: Get character category</a><li><a href="#%211e-24%3a-Disabling-Unicode-awareness-in-builtin-charclasses">℞ 24: Disabling Unicode-awareness in builtin charclasses</a><li><a href="#%211e-25%3a-Match-Unicode-properties-in-regex-with-%5cp%2c-%5cP">℞ 25: Match Unicode properties in regex with \p, \P</a><li><a href="#%211e-26%3a-Custom-character-properties">℞ 26: Custom character properties</a><li><a href="#%211e-27%3a-Unicode-normalization">℞ 27: Unicode normalization</a><li><a href="#%211e-28%3a-Convert-non-ASCII-Unicode-numerics">℞ 28: Convert non-ASCII Unicode numerics</a><li><a href="#%211e-29%3a-Match-Unicode-grapheme-cluster-in-regex">℞ 29: Match Unicode grapheme cluster in regex</a><li><a href="#%211e-30%3a-Extract-by-grapheme-instead-of-by-codepoint-(regex)">℞ 30: Extract by grapheme instead of by codepoint (regex)</a><li><a href="#%211e-31%3a-Extract-by-grapheme-instead-of-by-codepoint-(substr)">℞ 31: Extract by grapheme instead of by codepoint (substr)</a><li><a href="#%211e-32%3a-Reverse-string-by-grapheme">℞ 32: Reverse string by grapheme</a><li><a href="#%211e-33%3a-String-length-in-graphemes">℞ 33: String length in graphemes</a><li><a href="#%211e-34%3a-Unicode-column-width-for-printing">℞ 34: Unicode column-width for printing</a><li><a href="#%211e-35%3a-Unicode-collation">℞ 35: Unicode collation</a><li><a href="#%211e-36%3a-Case--_and_-accent-insensitive-Unicode-sort">℞ 36: Case- _and_ accent-insensitive Unicode sort</a><li><a href="#%211e-37%3a-Unicode-locale-collation">℞ 37: Unicode locale collation</a><li><a href="#%211e-38%3a-Making-cmp-work-on-text-instead-of-codepoints">℞ 38: Making cmp work on text instead of codepoints</a><li><a href="#%211e-39%3a-Case--_and_-accent-insensitive-comparisons">℞ 39: Case- _and_ accent-insensitive comparisons</a><li><a href="#%211e-40%3a-Case--_and_-accent-insensitive-locale-comparisons">℞ 40: Case- _and_ accent-insensitive locale comparisons</a><li><a href="#%211e-41%3a-Unicode-linebreaking">℞ 41: Unicode linebreaking</a><li><a href="#%211e-42%3a-Unicode-text-in-DBM-hashes%2c-the-tedious-way">℞ 42: Unicode text in DBM hashes, the tedious way</a><li><a href="#%211e-43%3a-Unicode-text-in-DBM-hashes%2c-the-easy-way">℞ 43: Unicode text in DBM hashes, the easy way</a><li><a href="#%211e-44%3a-PROGRAM%3a-Demo-of-Unicode-collation-and-printing">℞ 44: PROGRAM: Demo of Unicode collation and printing</a></ul><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHOR">AUTHOR</a><li><a href="#COPYRIGHT-AND-LICENCE">COPYRIGHT AND LICENCE</a><li><a href="#REVISION-HISTORY">REVISION HISTORY</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
      </div>
      
    <div id="footer">
      <div id="footer_content">
        <div id="footer_strapline">
          perldoc.perl.org - Official documentation for the Perl programming language
        </div>
        <div id="footer_links">
          <div id="address">
            <p class="name">Contact details</p>
            <p class="address">
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
	    </p>
            <p class="contact">
              Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
            </p>
          </div>
          <ul class="f1">
            <li>Manual
              <ul class="f2">
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">Changes</a>
              </ul>
            <li>Reference
              <ul class="f2">
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Variables</a>
              </ul>
            <li>Modules
              <ul class="f2">
                <li><a href="index-modules-A.html">Modules</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
              </ul>
            <li>Misc
              <ul class="f2">
                <li><a href="index-licence.html">License</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platforms</a>
              </ul>          </ul>
          <div class="clear"></div>
        </div>
      </div>
      <div id="footer_end">
      </div>
    </div>
      
    </div>
      <script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
      <script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(0);
  perldoc.pageName    = 'perlunicook';
  perldoc.pageAddress = 'perlunicook.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>