perllocale.pod
来自「ARM上的如果你对底层感兴趣」· POD 代码 · 共 977 行 · 第 1/3 页
POD
977 行
error message. First try fixing locale settings listed first.
Second, if using the listed commands you see something B<exactly>
(prefix matches do not count and case usually counts) like "En_US"
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 L<Fixing system locale configuration>.
=head2 Permanently fixing your locale configuration
This is when you see something like:
perl: warning: Please check that your locale settings:
LC_ALL = "En_US",
LANG = (unset)
are supported and installed on your system.
but then cannot see that "En_US" listed by the above-mentioned
commands. You may see things like "en_US.ISO8859-1", 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 L<Finding
locales> about general rules.
=head2 Permanently fixing system locale configuration
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 L<Finding locales>
section is unfortunately a bit vague about the exact commands and places
because these things are not that standardized.
=head2 The localeconv function
The POSIX::localeconv() function allows you to get particulars of the
locale-dependent numeric formatting information specified by the current
C<LC_NUMERIC> and C<LC_MONETARY> locales. (If you just want the name of
the current locale for a particular category, use POSIX::setlocale()
with a single parameter--see L<The setlocale function>.)
use POSIX qw(locale_h);
# Get a reference to a hash of locale-dependent info
$locale_values = localeconv();
# Output sorted list of the values
for (sort keys %$locale_values) {
printf "%-20s = %s\n", $_, $locale_values->{$_}
}
localeconv() takes no arguments, and returns B<a reference to> a hash.
The keys of this hash are variable names for formatting, such as
C<decimal_point> and C<thousands_sep>. The values are the
corresponding, er, values. See L<POSIX (3)/localeconv> 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 C<use locale>, because localeconv() always observes the
current locale.
Here's a simple-minded example program that rewrites its command-line
parameters as integers correctly formatted in the current locale:
# See comments in previous example
require 5.004;
use POSIX qw(locale_h);
# Get some of locale's numeric formatting parameters
my ($thousands_sep, $grouping) =
@{localeconv()}{'thousands_sep', 'grouping'};
# Apply defaults if values are missing
$thousands_sep = ',' unless $thousands_sep;
# grouping and mon_grouping are packed lists
# of small integers (characters) telling the
# grouping (thousand_seps and mon_thousand_seps
# being the group dividers) of numbers and
# monetary quantities. The integers' meanings:
# 255 means no more grouping, 0 means repeat
# the previous grouping, 1-254 means use that
# as the current grouping. Grouping goes from
# right to left (low to high digits). In the
# below we cheat slightly by never using anything
# else than the first grouping (whatever that is).
if ($grouping) {
@grouping = unpack("C*", $grouping);
} else {
@grouping = (3);
}
# Format command line params for current locale
for (@ARGV) {
$_ = int; # Chop non-integer part
1 while
s/(\d)(\d{$grouping[0]}($|$thousands_sep))/$1$thousands_sep$2/;
print "$_";
}
print "\n";
=head1 LOCALE CATEGORIES
The following subsections describe basic locale categories. Beyond these,
some combination categories allow manipulation of more than one
basic category at a time. See L<"ENVIRONMENT"> for a discussion of these.
=head2 Category LC_COLLATE: Collation
In the scope of S<C<use locale>>, Perl looks to the C<LC_COLLATE>
environment variable to determine the application's notions on collation
(ordering) of characters. For example, 'b' follows 'a' in Latin
alphabets, but where do 'E<aacute>' and 'E<aring>' belong? And while
'color' follows 'chocolate' in English, what about in Spanish?
The following collations all make sense and you may meet any of them
if you "use locale".
A B C D E a b c d e
A a B b C c D d D e
a A b B c C d D e E
a b c d e A B C D E
Here is a code snippet to tell what alphanumeric
characters are in the current locale, in that locale's order:
use locale;
print +(sort grep /\w/, map { chr() } 0..255), "\n";
Compare this with the characters that you see and their order if you
state explicitly that the locale should be ignored:
no locale;
print +(sort grep /\w/, map { chr() } 0..255), "\n";
This machine-native collation (which is what you get unless S<C<use
locale>> 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.
As noted in L<USING LOCALES>, C<cmp> compares according to the current
collation locale when C<use locale> is in effect, but falls back to a
byte-by-byte comparison for strings that the locale says are equal. You
can use POSIX::strcoll() if you don't want this fall-back:
use POSIX qw(strcoll);
$equal_in_locale =
!strcoll("space and case ignored", "SpaceAndCaseIgnored");
$equal_in_locale will be true if the collation locale specifies a
dictionary-like ordering that ignores space characters completely and
which folds case.
If you have a single string that you want to check for "equality in
locale" against several others, you might think you could gain a little
efficiency by using POSIX::strxfrm() in conjunction with C<eq>:
use POSIX qw(strxfrm);
$xfrm_string = strxfrm("Mixed-case string");
print "locale collation ignores spaces\n"
if $xfrm_string eq strxfrm("Mixed-casestring");
print "locale collation ignores hyphens\n"
if $xfrm_string eq strxfrm("Mixedcase string");
print "locale collation ignores case\n"
if $xfrm_string eq strxfrm("mixed-case string");
strxfrm() takes a string and maps it into a transformed string for use
in byte-by-byte comparisons against other transformed strings during
collation. "Under the hood", locale-affected Perl comparison operators
call strxfrm() for both operands, then do a byte-by-byte
comparison of the transformed strings. By calling strxfrm() 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 L<perlguts/Magic Variables>) 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
C<cmp> runs just about as fast. It also copes with null characters
embedded in strings; if you call strxfrm() 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 strxfrm()
directly: let Perl do it for you.
Note: C<use locale> isn't shown in some of these examples because it isn't
needed: strcoll() and strxfrm() exist only to generate locale-dependent
results, and so always obey the current C<LC_COLLATE> locale.
=head2 Category LC_CTYPE: Character Types
In the scope of S<C<use locale>>, Perl obeys the C<LC_CTYPE> locale
setting. This controls the application's notion of which characters are
alphabetic. This affects Perl's C<\w> regular expression metanotation,
which stands for alphanumeric characters--that is, alphabetic and
numeric characters. (Consult L<perlre> for more information about
regular expressions.) Thanks to C<LC_CTYPE>, depending on your locale
setting, characters like 'E<aelig>', 'E<eth>', 'E<szlig>', and
'E<oslash>' may be understood as C<\w> characters.
The C<LC_CTYPE> 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 C<\l>, C<\L>, C<\u>, or C<\U> in double-quoted strings
and C<s///> substitutions; and case-independent regular expression
pattern matching using the C<i> modifier.
Finally, C<LC_CTYPE> affects the POSIX character-class test
functions--isalpha(), islower(), and so on. For example, if you move
from the "C" locale to a 7-bit Scandinavian one, you may find--possibly
to your surprise--that "|" moves from the ispunct() class to isalpha().
B<Note:> A broken or malicious C<LC_CTYPE> 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 C<\w> inside a C<no locale> block. See L<"SECURITY">.
=head2 Category LC_NUMERIC: Numeric Formatting
In the scope of S<C<use locale>>, Perl obeys the C<LC_NUMERIC> locale
information, which controls an application's idea of how numbers should
be formatted for human readability by the printf(), sprintf(), and
write() 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 L<The localeconv function> if you care about these things.)
Output produced by print() is B<never> affected by the
current locale: it is independent of whether C<use locale> or C<no
locale> is in effect, and corresponds to what you'd get from printf()
in the "C" locale. The same is true for Perl's internal conversions
between numeric and string formats:
use POSIX qw(strtod);
use locale;
$n = 5/2; # Assign numeric 2.5 to $n
$a = " $n"; # Locale-independent conversion to string
print "half five is $n\n"; # Locale-independent output
printf "half five is %g\n", $n; # Locale-dependent output
print "DECIMAL POINT IS COMMA\n"
if $n == (strtod("2,5"))[0]; # Locale-dependent conversion
=head2 Category LC_MONETARY: Formatting of monetary amounts
The C standard defines the C<LC_MONETARY> 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 C<LC_MONETARY>, you can query its contents--see L<The localeconv
function>--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.
=head2 LC_TIME
Output produced by POSIX::strftime(), which builds a formatted
human-readable date/time string, is affected by the current C<LC_TIME>
locale. Thus, in a French locale, the output produced by the C<%B>
format element (full month name) for the first month of the year would
be "janvier". Here's how to get a list of long month names in the
current locale:
use POSIX qw(strftime);
for (0..11) {
$long_month_name[$_] =
strftime("%B", 0, 0, 0, 1, $_, 96);
}
Note: C<use locale> isn't needed in this example: as a function that
exists only to generate locale-dependent results, strftime() always
obeys the current C<LC_TIME> locale.
=head2 Other categories
The remaining locale category, C<LC_MESSAGES> (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.
=head1 SECURITY
Although the main discussion of Perl security issues can be found in
L<perlsec>, 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:
=over 4
=item *
Regular expression checks for safe file names or mail addresses using
C<\w> may be spoofed by an C<LC_CTYPE> locale that claims that
characters such as "E<gt>" and "|" are alphanumeric.
=item *
String interpolation with case-mapping, as in, say, C<$dest =
"C:\U$name.$ext">, may produce dangerous results if a bogus LC_CTYPE
case-mapping table is in effect.
=item *
If the decimal point character in the C<LC_NUMERIC> locale is
surreptitiously changed from a dot to a comma, C<sprintf("%g",
0.123456e3)> produces a string result of "123,456". Many people would
interpret this as one hundred and twenty-three thousand, four hundred
and fifty-six.
=item *
A sneaky C<LC_COLLATE> locale could result in the names of students with
"D" grades appearing ahead of those with "A"s.
=item *
An application that takes the trouble to use information in
C<LC_MONETARY> may format debits as if they were credits and vice versa
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?