perllocale.html
来自「perl教程」· HTML 代码 · 共 1,009 行 · 第 1/5 页
HTML
1,009 行
The first program, which ignores its locale, won't run: a value taken
directly from the command line may not be used to name an output file
when taint checks are enabled.</p>
<pre>
<span class="comment">#/usr/local/bin/perl -T</span>
<span class="comment"># Run with taint checking</span>
</pre>
<pre>
<span class="comment"># Command line sanity check omitted...</span>
<span class="variable">$tainted_output_file</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">open</span><span class="operator">(</span><span class="variable">F</span><span class="operator">,</span> <span class="string">">$tainted_output_file"</span><span class="operator">)</span>
<span class="keyword">or</span> <span class="keyword">warn</span> <span class="string">"Open of $untainted_output_file failed: $!\n"</span><span class="operator">;</span>
</pre>
<p>The program can be made to run by "laundering" the tainted value through
a regular expression: the second example--which still ignores locale
information--runs, creating the file named on its command line
if it can.</p>
<pre>
<span class="comment">#/usr/local/bin/perl -T</span>
</pre>
<pre>
<span class="variable">$tainted_output_file</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
<span class="variable">$tainted_output_file</span> <span class="operator">=~</span> <span class="regex">m%[\w/]+%</span><span class="operator">;</span>
<span class="variable">$untainted_output_file</span> <span class="operator">=</span> <span class="variable">$&</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">open</span><span class="operator">(</span><span class="variable">F</span><span class="operator">,</span> <span class="string">">$untainted_output_file"</span><span class="operator">)</span>
<span class="keyword">or</span> <span class="keyword">warn</span> <span class="string">"Open of $untainted_output_file failed: $!\n"</span><span class="operator">;</span>
</pre>
<p>Compare this with a similar but locale-aware program:</p>
<pre>
<span class="comment">#/usr/local/bin/perl -T</span>
</pre>
<pre>
<span class="variable">$tainted_output_file</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">locale</span><span class="operator">;</span>
<span class="variable">$tainted_output_file</span> <span class="operator">=~</span> <span class="regex">m%[\w/]+%</span><span class="operator">;</span>
<span class="variable">$localized_output_file</span> <span class="operator">=</span> <span class="variable">$&</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">open</span><span class="operator">(</span><span class="variable">F</span><span class="operator">,</span> <span class="string">">$localized_output_file"</span><span class="operator">)</span>
<span class="keyword">or</span> <span class="keyword">warn</span> <span class="string">"Open of $localized_output_file failed: $!\n"</span><span class="operator">;</span>
</pre>
<p>This third program fails to run because $& is tainted: it is the result
of a match involving <code>\w</code> while <code>use locale</code> is in effect.</p>
<p>
</p>
<hr />
<h1><a name="environment">ENVIRONMENT</a></h1>
<dl>
<dt><strong><a name="item_perl_badlang">PERL_BADLANG</a></strong>
<dd>
<p>A string that can suppress Perl's warning about failed locale settings
at startup. Failure can occur if the locale support in the operating
system is lacking (broken) in some way--or if you mistyped the name of
a locale when you set up your environment. If this environment
variable is absent, or has a value that does not evaluate to integer
zero--that is, "0" or ""-- Perl will complain about locale setting
failures.</p>
</dd>
<dd>
<p><strong>NOTE</strong>: PERL_BADLANG only gives you a way to hide the warning message.
The message tells about some problem in your system's locale support,
and you should investigate what the problem is.</p>
</dd>
</li>
</dl>
<p>The following environment variables are not specific to Perl: They are
part of the standardized (ISO C, XPG4, POSIX 1.c) <code>setlocale()</code> method
for controlling an application's opinion on data.</p>
<dl>
<dt><strong><a name="item_lc_all">LC_ALL</a></strong>
<dd>
<p><a href="#item_lc_all"><code>LC_ALL</code></a> is the "override-all" locale environment variable. If
set, it overrides all the rest of the locale environment variables.</p>
</dd>
</li>
<dt><strong><a name="item_language">LANGUAGE</a></strong>
<dd>
<p><strong>NOTE</strong>: <a href="#item_language"><code>LANGUAGE</code></a> is a GNU extension, it affects you only if you
are using the GNU libc. This is the case if you are using e.g. Linux.
If you are using "commercial" UNIXes you are most probably <em>not</em>
using GNU libc and you can ignore <a href="#item_language"><code>LANGUAGE</code></a>.</p>
</dd>
<dd>
<p>However, in the case you are using <a href="#item_language"><code>LANGUAGE</code></a>: it affects the
language of informational, warning, and error messages output by
commands (in other words, it's like <code>LC_MESSAGES</code>) but it has higher
priority than <a href="#item_lc_all">LC_ALL</a>. Moreover, it's not a single value but
instead a "path" (":"-separated list) of <em>languages</em> (not locales).
See the GNU <code>gettext</code> library documentation for more information.</p>
</dd>
</li>
<dt><strong><a name="item_lc_ctype">LC_CTYPE</a></strong>
<dd>
<p>In the absence of <a href="#item_lc_all"><code>LC_ALL</code></a>, <a href="#item_lc_ctype"><code>LC_CTYPE</code></a> chooses the character type
locale. In the absence of both <a href="#item_lc_all"><code>LC_ALL</code></a> and <a href="#item_lc_ctype"><code>LC_CTYPE</code></a>, <a href="#item_lang"><code>LANG</code></a>
chooses the character type locale.</p>
</dd>
</li>
<dt><strong><a name="item_lc_collate">LC_COLLATE</a></strong>
<dd>
<p>In the absence of <a href="#item_lc_all"><code>LC_ALL</code></a>, <a href="#item_lc_collate"><code>LC_COLLATE</code></a> chooses the collation
(sorting) locale. In the absence of both <a href="#item_lc_all"><code>LC_ALL</code></a> and <a href="#item_lc_collate"><code>LC_COLLATE</code></a>,
<a href="#item_lang"><code>LANG</code></a> chooses the collation locale.</p>
</dd>
</li>
<dt><strong><a name="item_lc_monetary">LC_MONETARY</a></strong>
<dd>
<p>In the absence of <a href="#item_lc_all"><code>LC_ALL</code></a>, <a href="#item_lc_monetary"><code>LC_MONETARY</code></a> chooses the monetary
formatting locale. In the absence of both <a href="#item_lc_all"><code>LC_ALL</code></a> and <a href="#item_lc_monetary"><code>LC_MONETARY</code></a>,
<a href="#item_lang"><code>LANG</code></a> chooses the monetary formatting locale.</p>
</dd>
</li>
<dt><strong><a name="item_lc_numeric">LC_NUMERIC</a></strong>
<dd>
<p>In the absence of <a href="#item_lc_all"><code>LC_ALL</code></a>, <a href="#item_lc_numeric"><code>LC_NUMERIC</code></a> chooses the numeric format
locale. In the absence of both <a href="#item_lc_all"><code>LC_ALL</code></a> and <a href="#item_lc_numeric"><code>LC_NUMERIC</code></a>, <a href="#item_lang"><code>LANG</code></a>
chooses the numeric format.</p>
</dd>
</li>
<dt><strong><a name="item_lc_time">LC_TIME</a></strong>
<dd>
<p>In the absence of <a href="#item_lc_all"><code>LC_ALL</code></a>, <a href="#item_lc_time"><code>LC_TIME</code></a> chooses the date and time
formatting locale. In the absence of both <a href="#item_lc_all"><code>LC_ALL</code></a> and <a href="#item_lc_time"><code>LC_TIME</code></a>,
<a href="#item_lang"><code>LANG</code></a> chooses the date and time formatting locale.</p>
</dd>
</li>
<dt><strong><a name="item_lang">LANG</a></strong>
<dd>
<p><a href="#item_lang"><code>LANG</code></a> is the "catch-all" locale environment variable. If it is set, it
is used as the last resort after the overall <a href="#item_lc_all"><code>LC_ALL</code></a> and the
category-specific <code>LC_...</code>.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="notes">NOTES</a></h1>
<p>
</p>
<h2><a name="backward_compatibility">Backward compatibility</a></h2>
<p>Versions of Perl prior to 5.004 <strong>mostly</strong> ignored locale information,
generally behaving as if something similar to the <code>"C"</code> locale were
always in force, even if the program environment suggested otherwise
(see <a href="#the_setlocale_function">The setlocale function</a>). By default, Perl still behaves this
way for backward compatibility. If you want a Perl application to pay
attention to locale information, you <strong>must</strong> use the <code>use locale</code>
pragma (see <a href="#the_use_locale_pragma">The use locale pragma</a>) to instruct it to do so.</p>
<p>Versions of Perl from 5.002 to 5.003 did use the <a href="#item_lc_ctype"><code>LC_CTYPE</code></a>
information if available; that is, <code>\w</code> did understand what
were the letters according to the locale environment variables.
The problem was that the user had no control over the feature:
if the C library supported locales, Perl used them.</p>
<p>
</p>
<h2><a name="i18n_collate_obsolete">I18N:Collate obsolete</a></h2>
<p>In versions of Perl prior to 5.004, per-locale collation was possible
using the <code>I18N::Collate</code> library module. This module is now mildly
obsolete and should be avoided in new applications. The <a href="#item_lc_collate"><code>LC_COLLATE</code></a>
functionality is now integrated into the Perl core language: One can
use locale-specific scalar data completely normally with <code>use locale</code>,
so there is no longer any need to juggle with the scalar references of
<code>I18N::Collate</code>.</p>
<p>
</p>
<h2><a name="sort_speed_and_memory_use_impacts">Sort speed and memory use impacts</a></h2>
<p>Comparing and sorting by locale is usually slower than the default
sorting; slow-downs of two to four times have been observed. It will
also consume more memory: once a Perl scalar variable has participated
in any string comparison or sorting operation obeying the locale
collation rules, it will take 3-15 times more memory than before. (The
exact multiplier depends on the string's contents, the operating system
and the locale.) These downsides are dictated more by the operating
system's implementation of the locale system than by Perl.</p>
<p>
</p>
<h2><a name="write___and_lc_numeric"><a href="../../lib/Pod/perlfunc.html#item_write"><code>write()</code></a> and LC_NUMERIC</a></h2>
<p>Formats are the only part of Perl that unconditionally use information
from a program's locale; if a program's environment specifies an
LC_NUMERIC locale, it is always used to specify the decimal point
character in formatted output. Formatted output cannot be controlled by
<code>use locale</code> because the pragma is tied to the block structure of the
program, and, for historical reasons, formats exist outside that block
structure.</p>
<p>
</p>
<h2><a name="freely_available_locale_definitions">Freely available locale definitions</a></h2>
<p>There is a
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?