perllocale.html

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

HTML
1,009 行
字号
<p>The <a href="#item_lc_ctype"><code>LC_CTYPE</code></a> locale also provides the map used in transliterating
characters between lower and uppercase.  This affects the case-mapping
functions--lc(), lcfirst, uc(), and ucfirst(); case-mapping
interpolation with <code>\l</code>, <code>\L</code>, <code>\u</code>, or <code>\U</code> in double-quoted strings
and <a href="../../lib/Pod/perlfunc.html#item_s_"><code>s///</code></a> substitutions; and case-independent regular expression
pattern matching using the <code>i</code> modifier.</p>
<p>Finally, <a href="#item_lc_ctype"><code>LC_CTYPE</code></a> affects the POSIX character-class test
functions--isalpha(), islower(), and so on.  For example, if you move
from the &quot;C&quot; locale to a 7-bit Scandinavian one, you may find--possibly
to your surprise--that &quot;|&quot; moves from the <code>ispunct()</code> class to isalpha().</p>
<p><strong>Note:</strong> A broken or malicious <a href="#item_lc_ctype"><code>LC_CTYPE</code></a> locale definition may result
in clearly ineligible characters being considered to be alphanumeric by
your application.  For strict matching of (mundane) letters and
digits--for example, in command strings--locale-aware applications
should use <code>\w</code> inside a <code>no locale</code> block.  See <a href="#security">SECURITY</a>.</p>
<p>
</p>
<h2><a name="category_lc_numeric__numeric_formatting">Category LC_NUMERIC: Numeric Formatting</a></h2>
<p>In the scope of <code>use&nbsp;locale</code>, Perl obeys the <a href="#item_lc_numeric"><code>LC_NUMERIC</code></a> locale
information, which controls an application's idea of how numbers should
be formatted for human readability by the printf(), sprintf(), and
<a href="../../lib/Pod/perlfunc.html#item_write"><code>write()</code></a> functions.  String-to-numeric conversion by the POSIX::strtod()
function is also affected.  In most implementations the only effect is to
change the character used for the decimal point--perhaps from '.'  to ','.
These functions aren't aware of such niceties as thousands separation and
so on.  (See <a href="#the_localeconv_function">The localeconv function</a> if you care about these things.)</p>
<p>Output produced by <a href="../../lib/Pod/perlfunc.html#item_print"><code>print()</code></a> is also affected by the current locale: it
depends on whether <code>use locale</code> or <code>no locale</code> is in effect, and
corresponds to what you'd get from <a href="../../lib/Pod/perlfunc.html#item_printf"><code>printf()</code></a> in the &quot;C&quot; locale.  The
same is true for Perl's internal conversions between numeric and
string formats:</p>
<pre>
        <span class="keyword">use</span> <span class="variable">POSIX</span> <span class="string">qw(strtod)</span><span class="operator">;</span>
        <span class="keyword">use</span> <span class="variable">locale</span><span class="operator">;</span>
</pre>
<pre>
        <span class="variable">$n</span> <span class="operator">=</span> <span class="number">5</span><span class="operator">/</span><span class="number">2</span><span class="operator">;</span>   <span class="comment"># Assign numeric 2.5 to $n</span>
</pre>
<pre>
        <span class="variable">$a</span> <span class="operator">=</span> <span class="string">" $n"</span><span class="operator">;</span> <span class="comment"># Locale-dependent conversion to string</span>
</pre>
<pre>
        <span class="keyword">print</span> <span class="string">"half five is $n\n"</span><span class="operator">;</span>       <span class="comment"># Locale-dependent output</span>
</pre>
<pre>
        <span class="keyword">printf</span> <span class="string">"half five is %g\n"</span><span class="operator">,</span> <span class="variable">$n</span><span class="operator">;</span>  <span class="comment"># Locale-dependent output</span>
</pre>
<pre>
        <span class="keyword">print</span> <span class="string">"DECIMAL POINT IS COMMA\n"</span>
            <span class="keyword">if</span> <span class="variable">$n</span> <span class="operator">==</span> <span class="operator">(</span><span class="variable">strtod</span><span class="operator">(</span><span class="string">"2,5"</span><span class="operator">))</span><span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">;</span> <span class="comment"># Locale-dependent conversion</span>
</pre>
<p>See also <a href="../../I18N/Langinfo.html">the I18N::Langinfo manpage</a> and <code>RADIXCHAR</code>.</p>
<p>
</p>
<h2><a name="category_lc_monetary__formatting_of_monetary_amounts">Category LC_MONETARY: Formatting of monetary amounts</a></h2>
<p>The C standard defines the <a href="#item_lc_monetary"><code>LC_MONETARY</code></a> category, but no function
that is affected by its contents.  (Those with experience of standards
committees will recognize that the working group decided to punt on the
issue.)  Consequently, Perl takes no notice of it.  If you really want
to use <a href="#item_lc_monetary"><code>LC_MONETARY</code></a>, you can query its contents--see 
<a href="#the_localeconv_function">The localeconv function</a>--and use the information that it returns in your 
application's own formatting of currency amounts.  However, you may well 
find that the information, voluminous and complex though it may be, still 
does not quite meet your requirements: currency formatting is a hard nut 
to crack.</p>
<p>See also <a href="../../I18N/Langinfo.html">the I18N::Langinfo manpage</a> and <code>CRNCYSTR</code>.</p>
<p>
</p>
<h2><a name="lc_time">LC_TIME</a></h2>
<p>Output produced by POSIX::strftime(), which builds a formatted
human-readable date/time string, is affected by the current <a href="#item_lc_time"><code>LC_TIME</code></a>
locale.  Thus, in a French locale, the output produced by the <code>%B</code>
format element (full month name) for the first month of the year would
be &quot;janvier&quot;.  Here's how to get a list of long month names in the
current locale:</p>
<pre>
        <span class="keyword">use</span> <span class="variable">POSIX</span> <span class="string">qw(strftime)</span><span class="operator">;</span>
        <span class="keyword">for</span> <span class="operator">(</span><span class="number">0</span><span class="operator">..</span><span class="number">11</span><span class="operator">)</span> <span class="operator">{</span>
            <span class="variable">$long_month_name</span><span class="operator">[</span><span class="variable">$_</span><span class="operator">]</span> <span class="operator">=</span>
                <span class="variable">strftime</span><span class="operator">(</span><span class="string">"%B"</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="variable">$_</span><span class="operator">,</span> <span class="number">96</span><span class="operator">);</span>
        <span class="operator">}</span>
</pre>
<p>Note: <code>use locale</code> isn't needed in this example: as a function that
exists only to generate locale-dependent results, <code>strftime()</code> always
obeys the current <a href="#item_lc_time"><code>LC_TIME</code></a> locale.</p>
<p>See also <a href="../../I18N/Langinfo.html">the I18N::Langinfo manpage</a> and <code>ABDAY_1</code>..<code>ABDAY_7</code>, <code>DAY_1</code>..<code>DAY_7</code>,
<code>ABMON_1</code>..<code>ABMON_12</code>, and <code>ABMON_1</code>..<code>ABMON_12</code>.</p>
<p>
</p>
<h2><a name="other_categories">Other categories</a></h2>
<p>The remaining locale category, <code>LC_MESSAGES</code> (possibly supplemented
by others in particular implementations) is not currently used by
Perl--except possibly to affect the behavior of library functions
called by extensions outside the standard Perl distribution and by the
operating system and its utilities.  Note especially that the string
value of <a href="../../lib/Pod/perlvar.html#item___"><code>$!</code></a> and the error messages given by external utilities may
be changed by <code>LC_MESSAGES</code>.  If you want to have portable error
codes, use <a href="../../lib/Pod/perlvar.html#item___"><code>%!</code></a>.  See <a href="../../lib/Errno.html">the Errno manpage</a>.</p>
<p>
</p>
<hr />
<h1><a name="security">SECURITY</a></h1>
<p>Although the main discussion of Perl security issues can be found in
<a href="../../lib/Pod/perlsec.html">the perlsec manpage</a>, a discussion of Perl's locale handling would be incomplete
if it did not draw your attention to locale-dependent security issues.
Locales--particularly on systems that allow unprivileged users to
build their own locales--are untrustworthy.  A malicious (or just plain
broken) locale can make a locale-aware application give unexpected
results.  Here are a few possibilities:</p>
<ul>
<li>
<p>Regular expression checks for safe file names or mail addresses using
<code>\w</code> may be spoofed by an <a href="#item_lc_ctype"><code>LC_CTYPE</code></a> locale that claims that
characters such as &quot;&gt;&quot; and &quot;|&quot; are alphanumeric.</p>
</li>
<li>
<p>String interpolation with case-mapping, as in, say, <code>$dest =
&quot;C:\U$name.$ext&quot;</code>, may produce dangerous results if a bogus LC_CTYPE
case-mapping table is in effect.</p>
</li>
<li>
<p>A sneaky <a href="#item_lc_collate"><code>LC_COLLATE</code></a> locale could result in the names of students with
&quot;D&quot; grades appearing ahead of those with &quot;A&quot;s.</p>
</li>
<li>
<p>An application that takes the trouble to use information in
<a href="#item_lc_monetary"><code>LC_MONETARY</code></a> may format debits as if they were credits and vice versa
if that locale has been subverted.  Or it might make payments in US
dollars instead of Hong Kong dollars.</p>
</li>
<li>
<p>The date and day names in dates formatted by <code>strftime()</code> could be
manipulated to advantage by a malicious user able to subvert the
<code>LC_DATE</code> locale.  (&quot;Look--it says I wasn't in the building on
Sunday.&quot;)</p>
</li>
</ul>
<p>Such dangers are not peculiar to the locale system: any aspect of an
application's environment which may be modified maliciously presents
similar challenges.  Similarly, they are not specific to Perl: any
programming language that allows you to write programs that take
account of their environment exposes you to these issues.</p>
<p>Perl cannot protect you from all possibilities shown in the
examples--there is no substitute for your own vigilance--but, when
<code>use locale</code> is in effect, Perl uses the tainting mechanism (see
<a href="../../lib/Pod/perlsec.html">the perlsec manpage</a>) to mark string results that become locale-dependent, and
which may be untrustworthy in consequence.  Here is a summary of the
tainting behavior of operators and functions that may be affected by
the locale:</p>
<ul>
<li>
<p><strong>Comparison operators</strong> (<code>lt</code>, <code>le</code>, <code>ge</code>, <code>gt</code> and <code>cmp</code>):</p>
<p>Scalar true/false (or less/equal/greater) result is never tainted.</p>
</li>
<li>
<p><strong>Case-mapping interpolation</strong> (with <code>\l</code>, <code>\L</code>, <code>\u</code> or <code>\U</code>)</p>
<p>Result string containing interpolated material is tainted if
<code>use locale</code> is in effect.</p>
</li>
<li>
<p><strong>Matching operator</strong> (<a href="../../lib/Pod/perlfunc.html#item_m_"><code>m//</code></a>):</p>
<p>Scalar true/false result never tainted.</p>
<p>Subpatterns, either delivered as a list-context result or as $1 etc.
are tainted if <code>use locale</code> is in effect, and the subpattern regular
expression contains <code>\w</code> (to match an alphanumeric character), <code>\W</code>
(non-alphanumeric character), <code>\s</code> (whitespace character), or <code>\S</code>
(non whitespace character).  The matched-pattern variable, $&amp;, $`
(pre-match), $' (post-match), and $+ (last match) are also tainted if
<code>use locale</code> is in effect and the regular expression contains <code>\w</code>,
<code>\W</code>, <code>\s</code>, or <code>\S</code>.</p>
</li>
<li>
<p><strong>Substitution operator</strong> (<a href="../../lib/Pod/perlfunc.html#item_s_"><code>s///</code></a>):</p>
<p>Has the same behavior as the match operator.  Also, the left
operand of <code>=~</code> becomes tainted when <code>use locale</code> in effect
if modified as a result of a substitution based on a regular
expression match involving <code>\w</code>, <code>\W</code>, <code>\s</code>, or <code>\S</code>; or of
case-mapping with <code>\l</code>, <code>\L</code>,<code>\u</code> or <code>\U</code>.</p>
</li>
<li>
<p><strong>Output formatting functions</strong> (printf() and write()):</p>
<p>Results are never tainted because otherwise even output from print,
for example <a href="../../lib/Pod/perlfunc.html#item_print"><code>print(1/7)</code></a>, should be tainted if <code>use locale</code> is in
effect.</p>
</li>
<li>
<p><strong>Case-mapping functions</strong> (lc(), lcfirst(), uc(), ucfirst()):</p>
<p>Results are tainted if <code>use locale</code> is in effect.</p>
</li>
<li>
<p><strong>POSIX locale-dependent functions</strong> (localeconv(), strcoll(),
strftime(), strxfrm()):</p>
<p>Results are never tainted.</p>
</li>
<li>
<p><strong>POSIX character class tests</strong> (isalnum(), isalpha(), isdigit(),
isgraph(), islower(), isprint(), ispunct(), isspace(), isupper(),
isxdigit()):</p>
<p>True/false results are never tainted.</p>
</li>
</ul>
<p>Three examples illustrate locale-dependent tainting.

⌨️ 快捷键说明

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