perllocale.html

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

HTML
1,009
字号
<p>You can switch locales as often as you wish at run time with the
POSIX::setlocale() function:</p>
<pre>
        <span class="comment"># This functionality not usable prior to Perl 5.004</span>
        <span class="keyword">require</span> <span class="number">5.004</span><span class="operator">;</span>
</pre>
<pre>
        <span class="comment"># Import locale-handling tool set from POSIX module.</span>
        <span class="comment"># This example uses: setlocale -- the function call</span>
        <span class="comment">#                    LC_CTYPE -- explained below</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"># query and save the old locale</span>
        <span class="variable">$old_locale</span> <span class="operator">=</span> <span class="variable">setlocale</span><span class="operator">(</span><span class="variable">LC_CTYPE</span><span class="operator">);</span>
</pre>
<pre>
        <span class="variable">setlocale</span><span class="operator">(</span><span class="variable">LC_CTYPE</span><span class="operator">,</span> <span class="string">"fr_CA.ISO8859-1"</span><span class="operator">);</span>
        <span class="comment"># LC_CTYPE now in locale "French, Canada, codeset ISO 8859-1"</span>
</pre>
<pre>
        <span class="variable">setlocale</span><span class="operator">(</span><span class="variable">LC_CTYPE</span><span class="operator">,</span> <span class="string">""</span><span class="operator">);</span>
        <span class="comment"># LC_CTYPE now reset to default defined by LC_ALL/LC_CTYPE/LANG</span>
        <span class="comment"># environment variables.  See below for documentation.</span>
</pre>
<pre>
        <span class="comment"># restore the old locale</span>
        <span class="variable">setlocale</span><span class="operator">(</span><span class="variable">LC_CTYPE</span><span class="operator">,</span> <span class="variable">$old_locale</span><span class="operator">);</span>
</pre>
<p>The first argument of <code>setlocale()</code> gives the <strong>category</strong>, the second the
<strong>locale</strong>.  The category tells in what aspect of data processing you
want to apply locale-specific rules.  Category names are discussed in
<a href="#locale_categories">LOCALE CATEGORIES</a> and <a href="#environment">ENVIRONMENT</a>.  The locale is the name of a
collection of customization information corresponding to a particular
combination of language, country or territory, and codeset.  Read on for
hints on the naming of locales: not all systems name locales as in the
example.</p>
<p>If no second argument is provided and the category is something else
than LC_ALL, the function returns a string naming the current locale
for the category.  You can use this value as the second argument in a
subsequent call to setlocale().</p>
<p>If no second argument is provided and the category is LC_ALL, the
result is implementation-dependent.  It may be a string of
concatenated locales names (separator also implementation-dependent)
or a single locale name.  Please consult your <em>setlocale(3)</em> for
details.</p>
<p>If a second argument is given and it corresponds to a valid locale,
the locale for the category is set to that value, and the function
returns the now-current locale value.  You can then use this in yet
another call to setlocale().  (In some implementations, the return
value may sometimes differ from the value you gave as the second
argument--think of it as an alias for the value you gave.)</p>
<p>As the example shows, if the second argument is an empty string, the
category's locale is returned to the default specified by the
corresponding environment variables.  Generally, this results in a
return to the default that was in force when Perl started up: changes
to the environment made by the application after startup may or may not
be noticed, depending on your system's C library.</p>
<p>If the second argument does not correspond to a valid locale, the locale
for the category is not changed, and the function returns <em>undef</em>.</p>
<p>For further information about the categories, consult <em>setlocale(3)</em>.</p>
<p>
</p>
<h2><a name="finding_locales">Finding locales</a></h2>
<p>For locales available in your system, consult also <em>setlocale(3)</em> to
see whether it leads to the list of available locales (search for the
<em>SEE ALSO</em> section).  If that fails, try the following command lines:</p>
<pre>
        locale -a</pre>
<pre>
        nlsinfo</pre>
<pre>
        ls /usr/lib/nls/loc</pre>
<pre>
        ls /usr/lib/locale</pre>
<pre>
        ls /usr/lib/nls</pre>
<pre>
        ls /usr/share/locale</pre>
<p>and see whether they list something resembling these</p>
<pre>
        en_US.ISO8859-1     de_DE.ISO8859-1     ru_RU.ISO8859-5
        en_US.iso88591      de_DE.iso88591      ru_RU.iso88595
        en_US               de_DE               ru_RU
        en                  de                  ru
        english             german              russian
        english.iso88591    german.iso88591     russian.iso88595
        english.roman8                          russian.koi8r</pre>
<p>Sadly, even though the calling interface for <code>setlocale()</code> has been
standardized, names of locales and the directories where the
configuration resides have not been.  The basic form of the name is
<em>language_territory</em><strong>.</strong><em>codeset</em>, but the latter parts after
<em>language</em> are not always present.  The <em>language</em> and <em>country</em>
are usually from the standards <strong>ISO 3166</strong> and <strong>ISO 639</strong>, the
two-letter abbreviations for the countries and the languages of the
world, respectively.  The <em>codeset</em> part often mentions some <strong>ISO
8859</strong> character set, the Latin codesets.  For example, <code>ISO 8859-1</code>
is the so-called &quot;Western European codeset&quot; that can be used to encode
most Western European languages adequately.  Again, there are several
ways to write even the name of that one standard.  Lamentably.</p>
<p>Two special locales are worth particular mention: &quot;C&quot; and &quot;POSIX&quot;.
Currently these are effectively the same locale: the difference is
mainly that the first one is defined by the C standard, the second by
the POSIX standard.  They define the <strong>default locale</strong> in which
every program starts in the absence of locale information in its
environment.  (The <em>default</em> default locale, if you will.)  Its language
is (American) English and its character codeset ASCII.</p>
<p><strong>NOTE</strong>: Not all systems have the &quot;POSIX&quot; locale (not all systems are
POSIX-conformant), so use &quot;C&quot; when you need explicitly to specify this
default locale.</p>
<p>
</p>
<h2><a name="locale_problems">LOCALE PROBLEMS</a></h2>
<p>You may encounter the following warning message at Perl startup:</p>
<pre>
        perl: warning: Setting locale failed.
        perl: warning: Please check that your locale settings:
                LC_ALL = &quot;En_US&quot;,
                LANG = (unset)
            are supported and installed on your system.
        perl: warning: Falling back to the standard locale (&quot;C&quot;).</pre>
<p>This means that your locale settings had LC_ALL set to &quot;En_US&quot; and
LANG exists but has no value.  Perl tried to believe you but could not.
Instead, Perl gave up and fell back to the &quot;C&quot; locale, the default locale
that is supposed to work no matter what.  This usually means your locale
settings were wrong, they mention locales your system has never heard
of, or the locale installation in your system has problems (for example,
some system files are broken or missing).  There are quick and temporary
fixes to these problems, as well as more thorough and lasting fixes.</p>
<p>
</p>
<h2><a name="temporarily_fixing_locale_problems">Temporarily fixing locale problems</a></h2>
<p>The two quickest fixes are either to render Perl silent about any
locale inconsistencies or to run Perl under the default locale &quot;C&quot;.</p>
<p>Perl's moaning about locale problems can be silenced by setting the
environment variable PERL_BADLANG to a zero value, for example &quot;0&quot;.
This method really just sweeps the problem under the carpet: you tell
Perl to shut up even when Perl sees that something is wrong.  Do not
be surprised if later something locale-dependent misbehaves.</p>
<p>Perl can be run under the &quot;C&quot; locale by setting the environment
variable LC_ALL to &quot;C&quot;.  This method is perhaps a bit more civilized
than the PERL_BADLANG approach, but setting LC_ALL (or
other locale variables) may affect other programs as well, not just
Perl.  In particular, external programs run from within Perl will see
these changes.  If you make the new settings permanent (read on), all
programs you run see the changes.  See <em>ENVIRONMENT</em> for
the full list of relevant environment variables and <a href="#using_locales">USING LOCALES</a>
for their effects in Perl.  Effects in other programs are 
easily deducible.  For example, the variable LC_COLLATE may well affect
your <strong>sort</strong> program (or whatever the program that arranges &quot;records&quot;
alphabetically in your system is called).</p>
<p>You can test out changing these variables temporarily, and if the
new settings seem to help, put those settings into your shell startup
files.  Consult your local documentation for the exact details.  For in
Bourne-like shells (<strong>sh</strong>, <strong>ksh</strong>, <strong>bash</strong>, <strong>zsh</strong>):</p>
<pre>
        LC_ALL=en_US.ISO8859-1
        export LC_ALL</pre>
<p>This assumes that we saw the locale &quot;en_US.ISO8859-1&quot; using the commands
discussed above.  We decided to try that instead of the above faulty
locale &quot;En_US&quot;--and in Cshish shells (<strong>csh</strong>, <strong>tcsh</strong>)</p>
<pre>
        setenv LC_ALL en_US.ISO8859-1</pre>
<p>or if you have the &quot;env&quot; application you can do in any shell</p>
<pre>
        env LC_ALL=en_US.ISO8859-1 perl ...</pre>
<p>If you do not know what shell you have, consult your local
helpdesk or the equivalent.</p>
<p>
</p>
<h2><a name="permanently_fixing_locale_problems">Permanently fixing locale problems</a></h2>
<p>The slower but superior fixes are when you may be able to yourself
fix the misconfiguration of your own environment variables.  The
mis(sing)configuration of the whole system's locales usually requires
the help of your friendly system administrator.</p>
<p>First, see earlier in this document about <a href="#finding_locales">Finding locales</a>.  That tells
how to find which locales are really supported--and more importantly,
installed--on your system.  In our example error message, environment
variables affecting the locale are listed in the order of decreasing
importance (and unset variables do not matter).  Therefore, having
LC_ALL set to &quot;En_US&quot; must have been the bad choice, as shown by the
error message.  First try fixing locale settings listed first.</p>
<p>Second, if using the listed commands you see something <strong>exactly</strong>
(prefix matches do not count and case usually counts) like &quot;En_US&quot;
without the quotes, then you should be okay because you are using a
locale name that should be installed and available in your system.
In this case, see <a href="#permanently_fixing_your_system_s_locale_configuration">Permanently fixing your system's locale configuration</a>.</p>
<p>
</p>
<h2><a name="permanently_fixing_your_system_s_locale_configuration">Permanently fixing your system's locale configuration</a></h2>
<p>This is when you see something like:</p>
<pre>
        perl: warning: Please check that your locale settings:
                LC_ALL = &quot;En_US&quot;,
                LANG = (unset)
            are supported and installed on your system.</pre>
<p>but then cannot see that &quot;En_US&quot; listed by the above-mentioned
commands.  You may see things like &quot;en_US.ISO8859-1&quot;, but that isn't
the same.  In this case, try running under a locale
that you can list and which somehow matches what you tried.  The
rules for matching locale names are a bit vague because
standardization is weak in this area.  See again the 

⌨️ 快捷键说明

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