perllocale.html

来自「perl教程」· HTML 代码 · 共 1,009 行 · 第 1/5 页

HTML
1,009 行
字号
<a href="#finding_locales">Finding locales</a> about general rules.</p>
<p>
</p>
<h2><a name="fixing_system_locale_configuration">Fixing system locale configuration</a></h2>
<p>Contact a system administrator (preferably your own) and report the exact
error message you get, and ask them to read this same documentation you
are now reading.  They should be able to check whether there is something
wrong with the locale configuration of the system.  The <a href="#finding_locales">Finding locales</a>
section is unfortunately a bit vague about the exact commands and places
because these things are not that standardized.</p>
<p>
</p>
<h2><a name="the_localeconv_function">The localeconv function</a></h2>
<p>The POSIX::localeconv() function allows you to get particulars of the
locale-dependent numeric formatting information specified by the current
<a href="#item_lc_numeric"><code>LC_NUMERIC</code></a> and <a href="#item_lc_monetary"><code>LC_MONETARY</code></a> locales.  (If you just want the name of
the current locale for a particular category, use POSIX::setlocale()
with a single parameter--see <a href="#the_setlocale_function">The setlocale function</a>.)</p>
<pre>
        <span class="keyword">use</span> <span class="variable">POSIX</span> <span class="string">qw(locale_h)</span><span class="operator">;</span>
</pre>
<pre>
        <span class="comment"># Get a reference to a hash of locale-dependent info</span>
        <span class="variable">$locale_values</span> <span class="operator">=</span> <span class="variable">localeconv</span><span class="operator">();</span>
</pre>
<pre>
        <span class="comment"># Output sorted list of the values</span>
        <span class="keyword">for</span> <span class="operator">(</span><span class="keyword">sort</span> <span class="keyword">keys</span> <span class="variable">%$locale_values</span><span class="operator">)</span> <span class="operator">{</span>
            <span class="keyword">printf</span> <span class="string">"%-20s = %s\n"</span><span class="operator">,</span> <span class="variable">$_</span><span class="operator">,</span> <span class="variable">$locale_values</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="variable">$_</span><span class="operator">}</span>
        <span class="operator">}</span>
</pre>
<p><code>localeconv()</code> takes no arguments, and returns <strong>a reference to</strong> a hash.
The keys of this hash are variable names for formatting, such as
<code>decimal_point</code> and <code>thousands_sep</code>.  The values are the
corresponding, er, values.  See <a href="../../lib/auto/POSIX/localeconv.html">localeconv in the POSIX manpage</a> for a longer
example listing the categories an implementation might be expected to
provide; some provide more and others fewer.  You don't need an
explicit <code>use locale</code>, because <code>localeconv()</code> always observes the
current locale.</p>
<p>Here's a simple-minded example program that rewrites its command-line
parameters as integers correctly formatted in the current locale:</p>
<pre>
        <span class="comment"># See comments in previous example</span>
        <span class="keyword">require</span> <span class="number">5.004</span><span class="operator">;</span>
        <span class="keyword">use</span> <span class="variable">POSIX</span> <span class="string">qw(locale_h)</span><span class="operator">;</span>
</pre>
<pre>
        <span class="comment"># Get some of locale's numeric formatting parameters</span>
        <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$thousands_sep</span><span class="operator">,</span> <span class="variable">$grouping</span><span class="operator">)</span> <span class="operator">=</span>
             <span class="variable">@</span><span class="operator">{</span><span class="variable">localeconv</span><span class="operator">()</span><span class="operator">}{</span><span class="string">'thousands_sep'</span><span class="operator">,</span> <span class="string">'grouping'</span><span class="operator">}</span><span class="operator">;</span>
</pre>
<pre>
        <span class="comment"># Apply defaults if values are missing</span>
        <span class="variable">$thousands_sep</span> <span class="operator">=</span> <span class="string">','</span> <span class="keyword">unless</span> <span class="variable">$thousands_sep</span><span class="operator">;</span>
</pre>
<pre>
        <span class="comment"># grouping and mon_grouping are packed lists</span>
        <span class="comment"># of small integers (characters) telling the</span>
        <span class="comment"># grouping (thousand_seps and mon_thousand_seps</span>
        <span class="comment"># being the group dividers) of numbers and</span>
        <span class="comment"># monetary quantities.  The integers' meanings:</span>
        <span class="comment"># 255 means no more grouping, 0 means repeat</span>
        <span class="comment"># the previous grouping, 1-254 means use that</span>
        <span class="comment"># as the current grouping.  Grouping goes from</span>
        <span class="comment"># right to left (low to high digits).  In the</span>
        <span class="comment"># below we cheat slightly by never using anything</span>
        <span class="comment"># else than the first grouping (whatever that is).</span>
        <span class="keyword">if</span> <span class="operator">(</span><span class="variable">$grouping</span><span class="operator">)</span> <span class="operator">{</span>
            <span class="variable">@grouping</span> <span class="operator">=</span> <span class="keyword">unpack</span><span class="operator">(</span><span class="string">"C*"</span><span class="operator">,</span> <span class="variable">$grouping</span><span class="operator">);</span>
        <span class="operator">}</span> <span class="keyword">else</span> <span class="operator">{</span>
            <span class="variable">@grouping</span> <span class="operator">=</span> <span class="operator">(</span><span class="number">3</span><span class="operator">);</span>
        <span class="operator">}</span>
</pre>
<pre>
        <span class="comment"># Format command line params for current locale</span>
        <span class="keyword">for</span> <span class="operator">(</span><span class="variable">@ARGV</span><span class="operator">)</span> <span class="operator">{</span>
            <span class="variable">$_</span> <span class="operator">=</span> <span class="keyword">int</span><span class="operator">;</span>    <span class="comment"># Chop non-integer part</span>
            <span class="number">1</span> <span class="keyword">while</span>
            <span class="regex">s/(\d)(\d{$grouping[0]}($|$thousands_sep))/$1$thousands_sep$2/</span><span class="operator">;</span>
            <span class="keyword">print</span> <span class="string">"$_"</span><span class="operator">;</span>
        <span class="operator">}</span>
        <span class="keyword">print</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="i18n__langinfo">I18N::Langinfo</a></h2>
<p>Another interface for querying locale-dependent information is the
I18N::Langinfo::langinfo() function, available at least in UNIX-like
systems and VMS.</p>
<p>The following example will import the <code>langinfo()</code> function itself and
three constants to be used as arguments to langinfo(): a constant for
the abbreviated first day of the week (the numbering starts from
Sunday = 1) and two more constants for the affirmative and negative
answers for a yes/no question in the current locale.</p>
<pre>
    <span class="keyword">use</span> <span class="variable">I18N::Langinfo</span> <span class="string">qw(langinfo ABDAY_1 YESSTR NOSTR)</span><span class="operator">;</span>
</pre>
<pre>
    <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$abday_1</span><span class="operator">,</span> <span class="variable">$yesstr</span><span class="operator">,</span> <span class="variable">$nostr</span><span class="operator">)</span> <span class="operator">=</span> <span class="keyword">map</span> <span class="operator">{</span> <span class="variable">langinfo</span> <span class="operator">}</span> <span class="string">qw(ABDAY_1 YESSTR NOSTR)</span><span class="operator">;</span>
</pre>
<pre>
    <span class="keyword">print</span> <span class="string">"$abday_1? [$yesstr/$nostr] "</span><span class="operator">;</span>
</pre>
<p>In other words, in the &quot;C&quot; (or English) locale the above will probably
print something like:</p>
<pre>
    Sun? [yes/no]</pre>
<p>See <a href="../../I18N/Langinfo.html">the I18N::Langinfo manpage</a> for more information.</p>
<p>
</p>
<hr />
<h1><a name="locale_categories">LOCALE CATEGORIES</a></h1>
<p>The following subsections describe basic locale categories.  Beyond these,
some combination categories allow manipulation of more than one
basic category at a time.  See <a href="#environment">ENVIRONMENT</a> for a discussion of these.</p>
<p>
</p>
<h2><a name="category_lc_collate__collation">Category LC_COLLATE: Collation</a></h2>
<p>In the scope of <code>use&nbsp;locale</code>, Perl looks to the <a href="#item_lc_collate"><code>LC_COLLATE</code></a>
environment variable to determine the application's notions on collation
(ordering) of characters.  For example, 'b' follows 'a' in Latin
alphabets, but where do '&aacute;' and '&aring;' belong?  And while
'color' follows 'chocolate' in English, what about in Spanish?</p>
<p>The following collations all make sense and you may meet any of them
if you &quot;use locale&quot;.</p>
<pre>
        A B C D E a b c d e
        A a B b C c D d E e
        a A b B c C d D e E
        a b c d e A B C D E</pre>
<p>Here is a code snippet to tell what &quot;word&quot;
characters are in the current locale, in that locale's order:</p>
<pre>
        <span class="keyword">use</span> <span class="variable">locale</span><span class="operator">;</span>
        <span class="keyword">print</span> <span class="operator">+(</span><span class="keyword">sort</span> <span class="keyword">grep</span> <span class="regex">/\w/</span><span class="operator">,</span> <span class="keyword">map</span> <span class="operator">{</span> <span class="keyword">chr</span> <span class="operator">}</span> <span class="number">0</span><span class="operator">..</span><span class="number">255</span><span class="operator">),</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
<p>Compare this with the characters that you see and their order if you
state explicitly that the locale should be ignored:</p>
<pre>
        <span class="keyword">no</span> <span class="variable">locale</span><span class="operator">;</span>
        <span class="keyword">print</span> <span class="operator">+(</span><span class="keyword">sort</span> <span class="keyword">grep</span> <span class="regex">/\w/</span><span class="operator">,</span> <span class="keyword">map</span> <span class="operator">{</span> <span class="keyword">chr</span> <span class="operator">}</span> <span class="number">0</span><span class="operator">..</span><span class="number">255</span><span class="operator">),</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
<p>This machine-native collation (which is what you get unless <code>use
locale</code> has appeared earlier in the same block) must be used for
sorting raw binary data, whereas the locale-dependent collation of the
first example is useful for natural text.</p>
<p>As noted in <a href="#using_locales">USING LOCALES</a>, <code>cmp</code> compares according to the current
collation locale when <code>use locale</code> is in effect, but falls back to a
char-by-char comparison for strings that the locale says are equal. You
can use POSIX::strcoll() if you don't want this fall-back:</p>
<pre>
        <span class="keyword">use</span> <span class="variable">POSIX</span> <span class="string">qw(strcoll)</span><span class="operator">;</span>
        <span class="variable">$equal_in_locale</span> <span class="operator">=</span>
            <span class="operator">!</span><span class="variable">strcoll</span><span class="operator">(</span><span class="string">"space and case ignored"</span><span class="operator">,</span> <span class="string">"SpaceAndCaseIgnored"</span><span class="operator">);</span>
</pre>
<p>$equal_in_locale will be true if the collation locale specifies a
dictionary-like ordering that ignores space characters completely and
which folds case.</p>
<p>If you have a single string that you want to check for &quot;equality in
locale&quot; against several others, you might think you could gain a little
efficiency by using POSIX::strxfrm() in conjunction with <code>eq</code>:</p>
<pre>
        <span class="keyword">use</span> <span class="variable">POSIX</span> <span class="string">qw(strxfrm)</span><span class="operator">;</span>
        <span class="variable">$xfrm_string</span> <span class="operator">=</span> <span class="variable">strxfrm</span><span class="operator">(</span><span class="string">"Mixed-case string"</span><span class="operator">);</span>
        <span class="keyword">print</span> <span class="string">"locale collation ignores spaces\n"</span>
            <span class="keyword">if</span> <span class="variable">$xfrm_string</span> <span class="keyword">eq</span> <span class="variable">strxfrm</span><span class="operator">(</span><span class="string">"Mixed-casestring"</span><span class="operator">);</span>
        <span class="keyword">print</span> <span class="string">"locale collation ignores hyphens\n"</span>
            <span class="keyword">if</span> <span class="variable">$xfrm_string</span> <span class="keyword">eq</span> <span class="variable">strxfrm</span><span class="operator">(</span><span class="string">"Mixedcase string"</span><span class="operator">);</span>
        <span class="keyword">print</span> <span class="string">"locale collation ignores case\n"</span>
            <span class="keyword">if</span> <span class="variable">$xfrm_string</span> <span class="keyword">eq</span> <span class="variable">strxfrm</span><span class="operator">(</span><span class="string">"mixed-case string"</span><span class="operator">);</span>
</pre>
<p><code>strxfrm()</code> takes a string and maps it into a transformed string for use
in char-by-char comparisons against other transformed strings during
collation.  &quot;Under the hood&quot;, locale-affected Perl comparison operators
call <code>strxfrm()</code> for both operands, then do a char-by-char
comparison of the transformed strings.  By calling <code>strxfrm()</code> explicitly
and using a non locale-affected comparison, the example attempts to save
a couple of transformations.  But in fact, it doesn't save anything: Perl
magic (see <a href="../../lib/Pod/perlguts.html#magic_variables">Magic Variables in the perlguts manpage</a>) creates the transformed version of a
string the first time it's needed in a comparison, then keeps this version around
in case it's needed again.  An example rewritten the easy way with
<code>cmp</code> runs just about as fast.  It also copes with null characters
embedded in strings; if you call <code>strxfrm()</code> directly, it treats the first
null it finds as a terminator.  don't expect the transformed strings
it produces to be portable across systems--or even from one revision
of your operating system to the next.  In short, don't call <code>strxfrm()</code>
directly: let Perl do it for you.</p>
<p>Note: <code>use locale</code> isn't shown in some of these examples because it isn't
needed: <code>strcoll()</code> and <code>strxfrm()</code> exist only to generate locale-dependent
results, and so always obey the current <a href="#item_lc_collate"><code>LC_COLLATE</code></a> locale.</p>
<p>
</p>
<h2><a name="category_lc_ctype__character_types">Category LC_CTYPE: Character Types</a></h2>
<p>In the scope of <code>use&nbsp;locale</code>, Perl obeys the <a href="#item_lc_ctype"><code>LC_CTYPE</code></a> locale
setting.  This controls the application's notion of which characters are
alphabetic.  This affects Perl's <code>\w</code> regular expression metanotation,
which stands for alphanumeric characters--that is, alphabetic,
numeric, and including other special characters such as the underscore or
hyphen.  (Consult <a href="../../lib/Pod/perlre.html">the perlre manpage</a> for more information about
regular expressions.)  Thanks to <a href="#item_lc_ctype"><code>LC_CTYPE</code></a>, depending on your locale
setting, characters like '&aelig;', '&eth;', '&szlig;', and
'&oslash;' may be understood as <code>\w</code> characters.</p>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?