home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / Perl / Non-RPC / Docs / perllocale < prev    next >
Text File  |  1999-04-17  |  43KB  |  992 lines

  1. NAME
  2.     perllocale - Perl locale handling (internationalization and
  3.     localization)
  4.  
  5. DESCRIPTION
  6.     Perl supports language-specific notions of data such as "is this
  7.     a letter", "what is the uppercase equivalent of this letter",
  8.     and "which of these letters comes first". These are important
  9.     issues, especially for languages other than English--but also
  10.     for English: it would be naïve to imagine that `A-Za-z' defines
  11.     all the "letters" needed to write in English. Perl is also aware
  12.     that some character other than '.' may be preferred as a decimal
  13.     point, and that output date representations may be language-
  14.     specific. The process of making an application take account of
  15.     its users' preferences in such matters is called
  16.     internationalization (often abbreviated as i18n); telling such
  17.     an application about a particular set of preferences is known as
  18.     localization (l10n).
  19.  
  20.     Perl can understand language-specific data via the standardized
  21.     (ISO C, XPG4, POSIX 1.c) method called "the locale system". The
  22.     locale system is controlled per application using one pragma,
  23.     one function call, and several environment variables.
  24.  
  25.     NOTE: This feature is new in Perl 5.004, and does not apply
  26.     unless an application specifically requests it--see the Backward
  27.     compatibility manpage. The one exception is that write() now
  28.     always uses the current locale - see the section on "NOTES".
  29.  
  30. PREPARING TO USE LOCALES
  31.     If Perl applications are to understand and present your data
  32.     correctly according a locale of your choice, all of the
  33.     following must be true:
  34.  
  35.     *   Your operating system must support the locale system. If it
  36.         does, you should find that the setlocale() function is a
  37.         documented part of its C library.
  38.  
  39.     *   Definitions for locales that you use must be installed. You, or
  40.         your system administrator, must make sure that this is the
  41.         case. The available locales, the location in which they are
  42.         kept, and the manner in which they are installed all vary
  43.         from system to system. Some systems provide only a few,
  44.         hard-wired locales and do not allow more to be added. Others
  45.         allow you to add "canned" locales provided by the system
  46.         supplier. Still others allow you or the system administrator
  47.         to define and add arbitrary locales. (You may have to ask
  48.         your supplier to provide canned locales that are not
  49.         delivered with your operating system.) Read your system
  50.         documentation for further illumination.
  51.  
  52.     *   Perl must believe that the locale system is supported. If it
  53.         does, `perl -V:d_setlocale' will say that the value for
  54.         `d_setlocale' is `define'.
  55.  
  56.  
  57.     If you want a Perl application to process and present your data
  58.     according to a particular locale, the application code should
  59.     include the `use locale' pragma (see the The use locale pragma
  60.     manpage) where appropriate, and at least one of the following
  61.     must be true:
  62.  
  63.     *   The locale-determining environment variables (see the section on
  64.         "ENVIRONMENT") must be correctly set up at the time the
  65.         application is started, either by yourself or by whoever set
  66.         up your system account.
  67.  
  68.     *   The application must set its own locale using the method
  69.         described in the The setlocale function manpage.
  70.  
  71.  
  72. USING LOCALES
  73.   The use locale pragma
  74.  
  75.     By default, Perl ignores the current locale. The `use locale'
  76.     pragma tells Perl to use the current locale for some operations:
  77.  
  78.     *   The comparison operators (`lt', `le', `cmp', `ge', and `gt') and
  79.         the POSIX string collation functions strcoll() and strxfrm()
  80.         use `LC_COLLATE'. sort() is also affected if used without an
  81.         explicit comparison function, because it uses `cmp' by
  82.         default.
  83.  
  84.         Note: `eq' and `ne' are unaffected by locale: they always
  85.         perform a byte-by-byte comparison of their scalar operands.
  86.         What's more, if `cmp' finds that its operands are equal
  87.         according to the collation sequence specified by the current
  88.         locale, it goes on to perform a byte-by-byte comparison, and
  89.         only returns *0* (equal) if the operands are bit-for-bit
  90.         identical. If you really want to know whether two strings--
  91.         which `eq' and `cmp' may consider different--are equal as
  92.         far as collation in the locale is concerned, see the
  93.         discussion in the Category LC_COLLATE: Collation manpage.
  94.  
  95.     *   Regular expressions and case-modification functions (uc(), lc(),
  96.         ucfirst(), and lcfirst()) use `LC_CTYPE'
  97.  
  98.     *   The formatting functions (printf(), sprintf() and write()) use
  99.         `LC_NUMERIC'
  100.  
  101.     *   The POSIX date formatting function (strftime()) uses `LC_TIME'.
  102.  
  103.  
  104.     `LC_COLLATE', `LC_CTYPE', and so on, are discussed further in
  105.     the LOCALE CATEGORIES manpage.
  106.  
  107.     The default behavior is restored with the `no locale' pragma, or
  108.     upon reaching the end of block enclosing `use locale'.
  109.  
  110.     The string result of any operation that uses locale information
  111.     is tainted, as it is possible for a locale to be untrustworthy.
  112.     See the section on "SECURITY".
  113.  
  114.   The setlocale function
  115.  
  116.     You can switch locales as often as you wish at run time with the
  117.     POSIX::setlocale() function:
  118.  
  119.             # This functionality not usable prior to Perl 5.004
  120.             require 5.004;
  121.  
  122.             # Import locale-handling tool set from POSIX module.
  123.             # This example uses: setlocale -- the function call
  124.             #                    LC_CTYPE -- explained below
  125.             use POSIX qw(locale_h);
  126.  
  127.             # query and save the old locale
  128.             $old_locale = setlocale(LC_CTYPE);
  129.  
  130.             setlocale(LC_CTYPE, "fr_CA.ISO8859-1");
  131.             # LC_CTYPE now in locale "French, Canada, codeset ISO 8859-1"
  132.  
  133.             setlocale(LC_CTYPE, "");
  134.             # LC_CTYPE now reset to default defined by LC_ALL/LC_CTYPE/LANG
  135.             # environment variables.  See below for documentation.
  136.  
  137.             # restore the old locale
  138.             setlocale(LC_CTYPE, $old_locale);
  139.  
  140.  
  141.     The first argument of setlocale() gives the category, the second
  142.     the locale. The category tells in what aspect of data processing
  143.     you want to apply locale-specific rules. Category names are
  144.     discussed in the LOCALE CATEGORIES manpage and the section on
  145.     "ENVIRONMENT". The locale is the name of a collection of
  146.     customization information corresponding to a particular
  147.     combination of language, country or territory, and codeset. Read
  148.     on for hints on the naming of locales: not all systems name
  149.     locales as in the example.
  150.  
  151.     If no second argument is provided and the category is something
  152.     else than LC_ALL, the function returns a string naming the
  153.     current locale for the category. You can use this value as the
  154.     second argument in a subsequent call to setlocale().
  155.  
  156.     If no second argument is provided and the category is LC_ALL,
  157.     the result is implementation-dependent. It may be a string of
  158.     concatenated locales names (separator also implementation-
  159.     dependent) or a single locale name. Please consult your the
  160.     setlocale(3) manpage for details.
  161.  
  162.     If a second argument is given and it corresponds to a valid
  163.     locale, the locale for the category is set to that value, and
  164.     the function returns the now-current locale value. You can then
  165.     use this in yet another call to setlocale(). (In some
  166.     implementations, the return value may sometimes differ from the
  167.     value you gave as the second argument--think of it as an alias
  168.     for the value you gave.)
  169.  
  170.     As the example shows, if the second argument is an empty string,
  171.     the category's locale is returned to the default specified by
  172.     the corresponding environment variables. Generally, this results
  173.     in a return to the default that was in force when Perl started
  174.     up: changes to the environment made by the application after
  175.     startup may or may not be noticed, depending on your system's C
  176.     library.
  177.  
  178.     If the second argument does not correspond to a valid locale,
  179.     the locale for the category is not changed, and the function
  180.     returns *undef*.
  181.  
  182.     For further information about the categories, consult the
  183.     setlocale(3) manpage.
  184.  
  185.   Finding locales
  186.  
  187.     For locales available in your system, consult also the
  188.     setlocale(3) manpage to see whether it leads to the list of
  189.     available locales (search for the *SEE ALSO* section). If that
  190.     fails, try the following command lines:
  191.  
  192.             locale -a
  193.  
  194.             nlsinfo
  195.  
  196.             ls /usr/lib/nls/loc
  197.  
  198.             ls /usr/lib/locale
  199.  
  200.             ls /usr/lib/nls
  201.  
  202.         ls /usr/share/locale
  203.  
  204.  
  205.     and see whether they list something resembling these
  206.  
  207.             en_US.ISO8859-1     de_DE.ISO8859-1     ru_RU.ISO8859-5
  208.             en_US.iso88591      de_DE.iso88591      ru_RU.iso88595
  209.             en_US               de_DE               ru_RU
  210.             en                  de                  ru
  211.             english             german              russian
  212.             english.iso88591    german.iso88591     russian.iso88595
  213.             english.roman8                          russian.koi8r
  214.  
  215.  
  216.     Sadly, even though the calling interface for setlocale() has
  217.     been standardized, names of locales and the directories where
  218.     the configuration resides have not been. The basic form of the
  219.     name is *language_territory*.*codeset*, but the latter parts
  220.     after *language* are not always present. The *language* and
  221.     *country* are usually from the standards ISO 3166 and ISO 639,
  222.     the two-letter abbreviations for the countries and the languages
  223.     of the world, respectively. The *codeset* part often mentions
  224.     some ISO 8859 character set, the Latin codesets. For example,
  225.     `ISO 8859-1' is the so-called "Western European codeset" that
  226.     can be used to encode most Western European languages
  227.     adequately. Again, there are several ways to write even the name
  228.     of that one standard. Lamentably.
  229.  
  230.     Two special locales are worth particular mention: "C" and
  231.     "POSIX". Currently these are effectively the same locale: the
  232.     difference is mainly that the first one is defined by the C
  233.     standard, the second by the POSIX standard. They define the
  234.     default locale in which every program starts in the absence of
  235.     locale information in its environment. (The *default* default
  236.     locale, if you will.) Its language is (American) English and its
  237.     character codeset ASCII.
  238.  
  239.     NOTE: Not all systems have the "POSIX" locale (not all systems
  240.     are POSIX-conformant), so use "C" when you need explicitly to
  241.     specify this default locale.
  242.  
  243.   LOCALE PROBLEMS
  244.  
  245.     You may encounter the following warning message at Perl startup:
  246.  
  247.         perl: warning: Setting locale failed.
  248.         perl: warning: Please check that your locale settings:
  249.                 LC_ALL = "En_US",
  250.                 LANG = (unset)
  251.             are supported and installed on your system.
  252.         perl: warning: Falling back to the standard locale ("C").
  253.  
  254.  
  255.     This means that your locale settings had LC_ALL set to "En_US"
  256.     and LANG exists but has no value. Perl tried to believe you but
  257.     could not. Instead, Perl gave up and fell back to the "C"
  258.     locale, the default locale that is supposed to work no matter
  259.     what. This usually means your locale settings were wrong, they
  260.     mention locales your system has never heard of, or the locale
  261.     installation in your system has problems (for example, some
  262.     system files are broken or missing). There are quick and
  263.     temporary fixes to these problems, as well as more thorough and
  264.     lasting fixes.
  265.  
  266.   Temporarily fixing locale problems
  267.  
  268.     The two quickest fixes are either to render Perl silent about
  269.     any locale inconsistencies or to run Perl under the default
  270.     locale "C".
  271.  
  272.     Perl's moaning about locale problems can be silenced by setting
  273.     the environment variable PERL_BADLANG to a zero value, for
  274.     example "0". This method really just sweeps the problem under
  275.     the carpet: you tell Perl to shut up even when Perl sees that
  276.     something is wrong. Do not be surprised if later something
  277.     locale-dependent misbehaves.
  278.  
  279.     Perl can be run under the "C" locale by setting the environment
  280.     variable LC_ALL to "C". This method is perhaps a bit more
  281.     civilized than the PERL_BADLANG approach, but setting LC_ALL (or
  282.     other locale variables) may affect other programs as well, not
  283.     just Perl. In particular, external programs run from within Perl
  284.     will see these changes. If you make the new settings permanent
  285.     (read on), all programs you run see the changes. See the
  286.     ENVIRONMENT manpage for for the full list of relevant
  287.     environment variables and the USING LOCALES manpage for their
  288.     effects in Perl. Effects in other programs are easily deducible.
  289.     For example, the variable LC_COLLATE may well affect your sort
  290.     program (or whatever the program that arranges `records'
  291.     alphabetically in your system is called).
  292.  
  293.     You can test out changing these variables temporarily, and if
  294.     the new settings seem to help, put those settings into your
  295.     shell startup files. Consult your local documentation for the
  296.     exact details. For in Bourne-like shells (sh, ksh, bash, zsh):
  297.  
  298.         LC_ALL=en_US.ISO8859-1
  299.         export LC_ALL
  300.  
  301.  
  302.     This assumes that we saw the locale "en_US.ISO8859-1" using the
  303.     commands discussed above. We decided to try that instead of the
  304.     above faulty locale "En_US"--and in Cshish shells (csh, tcsh)
  305.  
  306.         setenv LC_ALL en_US.ISO8859-1
  307.  
  308.  
  309.     If you do not know what shell you have, consult your local
  310.     helpdesk or the equivalent.
  311.  
  312.   Permanently fixing locale problems
  313.  
  314.     The slower but superior fixes are when you may be able to
  315.     yourself fix the misconfiguration of your own environment
  316.     variables. The mis(sing)configuration of the whole system's
  317.     locales usually requires the help of your friendly system
  318.     administrator.
  319.  
  320.     First, see earlier in this document about the Finding locales
  321.     manpage. That tells how to find which locales are really
  322.     supported--and more importantly, installed--on your system. In
  323.     our example error message, environment variables affecting the
  324.     locale are listed in the order of decreasing importance (and
  325.     unset variables do not matter). Therefore, having LC_ALL set to
  326.     "En_US" must have been the bad choice, as shown by the error
  327.     message. First try fixing locale settings listed first.
  328.  
  329.     Second, if using the listed commands you see something exactly
  330.     (prefix matches do not count and case usually counts) like
  331.     "En_US" without the quotes, then you should be okay because you
  332.     are using a locale name that should be installed and available
  333.     in your system. In this case, see the Permanently fixing system
  334.     locale configuration manpage.
  335.  
  336.   Permanently fixing your locale configuration
  337.  
  338.     This is when you see something like:
  339.  
  340.         perl: warning: Please check that your locale settings:
  341.                 LC_ALL = "En_US",
  342.                 LANG = (unset)
  343.             are supported and installed on your system.
  344.  
  345.  
  346.     but then cannot see that "En_US" listed by the above-mentioned
  347.     commands. You may see things like "en_US.ISO8859-1", but that
  348.     isn't the same. In this case, try running under a locale that
  349.     you can list and which somehow matches what you tried. The rules
  350.     for matching locale names are a bit vague because
  351.     standardization is weak in this area. See again the the Finding
  352.     locales manpage about general rules.
  353.  
  354.   Fixing system locale configuration
  355.  
  356.     Contact a system administrator (preferably your own) and report
  357.     the exact error message you get, and ask them to read this same
  358.     documentation you are now reading. They should be able to check
  359.     whether there is something wrong with the locale configuration
  360.     of the system. The the Finding locales manpage section is
  361.     unfortunately a bit vague about the exact commands and places
  362.     because these things are not that standardized.
  363.  
  364.   The localeconv function
  365.  
  366.     The POSIX::localeconv() function allows you to get particulars
  367.     of the locale-dependent numeric formatting information specified
  368.     by the current `LC_NUMERIC' and `LC_MONETARY' locales. (If you
  369.     just want the name of the current locale for a particular
  370.     category, use POSIX::setlocale() with a single parameter--see
  371.     the The setlocale function manpage.)
  372.  
  373.             use POSIX qw(locale_h);
  374.  
  375.             # Get a reference to a hash of locale-dependent info
  376.             $locale_values = localeconv();
  377.  
  378.             # Output sorted list of the values
  379.             for (sort keys %$locale_values) {
  380.                 printf "%-20s = %s\n", $_, $locale_values->{$_}
  381.             }
  382.  
  383.  
  384.     localeconv() takes no arguments, and returns a reference to a
  385.     hash. The keys of this hash are variable names for formatting,
  386.     such as `decimal_point' and `thousands_sep'. The values are the
  387.     corresponding, er, values. See the "localeconv" entry in the
  388.     POSIX (3) manpage for a longer example listing the categories an
  389.     implementation might be expected to provide; some provide more
  390.     and others fewer. You don't need an explicit `use locale',
  391.     because localeconv() always observes the current locale.
  392.  
  393.     Here's a simple-minded example program that rewrites its
  394.     command-line parameters as integers correctly formatted in the
  395.     current locale:
  396.  
  397.             # See comments in previous example
  398.             require 5.004;
  399.             use POSIX qw(locale_h);
  400.  
  401.             # Get some of locale's numeric formatting parameters
  402.             my ($thousands_sep, $grouping) =
  403.                  @{localeconv()}{'thousands_sep', 'grouping'};
  404.  
  405.             # Apply defaults if values are missing
  406.             $thousands_sep = ',' unless $thousands_sep;
  407.  
  408.         # grouping and mon_grouping are packed lists
  409.         # of small integers (characters) telling the
  410.         # grouping (thousand_seps and mon_thousand_seps
  411.         # being the group dividers) of numbers and
  412.         # monetary quantities.  The integers' meanings:
  413.         # 255 means no more grouping, 0 means repeat
  414.         # the previous grouping, 1-254 means use that
  415.         # as the current grouping.  Grouping goes from
  416.         # right to left (low to high digits).  In the
  417.         # below we cheat slightly by never using anything
  418.         # else than the first grouping (whatever that is).
  419.         if ($grouping) {
  420.             @grouping = unpack("C*", $grouping);
  421.         } else {
  422.             @grouping = (3);
  423.         }
  424.  
  425.             # Format command line params for current locale
  426.             for (@ARGV) {
  427.                 $_ = int;    # Chop non-integer part
  428.                 1 while
  429.                 s/(\d)(\d{$grouping[0]}($|$thousands_sep))/$1$thousands_sep$2/;
  430.                 print "$_";
  431.             }
  432.             print "\n";
  433.  
  434.  
  435. LOCALE CATEGORIES
  436.     The following subsections describe basic locale categories.
  437.     Beyond these, some combination categories allow manipulation of
  438.     more than one basic category at a time. See the section on
  439.     "ENVIRONMENT" for a discussion of these.
  440.  
  441.   Category LC_COLLATE: Collation
  442.  
  443.     In the scope of `use locale', Perl looks to the `LC_COLLATE'
  444.     environment variable to determine the application's notions on
  445.     collation (ordering) of characters. For example, 'b' follows 'a'
  446.     in Latin alphabets, but where do 'á' and 'å' belong? And while
  447.     'color' follows 'chocolate' in English, what about in Spanish?
  448.  
  449.     The following collations all make sense and you may meet any of
  450.     them if you "use locale".
  451.  
  452.         A B C D E a b c d e
  453.         A a B b C c D d D e
  454.         a A b B c C d D e E
  455.         a b c d e A B C D E
  456.  
  457.  
  458.     Here is a code snippet to tell what alphanumeric characters are
  459.     in the current locale, in that locale's order:
  460.  
  461.             use locale;
  462.             print +(sort grep /\w/, map { chr() } 0..255), "\n";
  463.  
  464.  
  465.     Compare this with the characters that you see and their order if
  466.     you state explicitly that the locale should be ignored:
  467.  
  468.             no locale;
  469.             print +(sort grep /\w/, map { chr() } 0..255), "\n";
  470.  
  471.  
  472.     This machine-native collation (which is what you get unless `use
  473.     locale' has appeared earlier in the same block) must be used for
  474.     sorting raw binary data, whereas the locale-dependent collation
  475.     of the first example is useful for natural text.
  476.  
  477.     As noted in the USING LOCALES manpage, `cmp' compares according
  478.     to the current collation locale when `use locale' is in effect,
  479.     but falls back to a byte-by-byte comparison for strings that the
  480.     locale says are equal. You can use POSIX::strcoll() if you don't
  481.     want this fall-back:
  482.  
  483.             use POSIX qw(strcoll);
  484.             $equal_in_locale =
  485.                 !strcoll("space and case ignored", "SpaceAndCaseIgnored");
  486.  
  487.  
  488.     $equal_in_locale will be true if the collation locale specifies
  489.     a dictionary-like ordering that ignores space characters
  490.     completely and which folds case.
  491.  
  492.     If you have a single string that you want to check for "equality
  493.     in locale" against several others, you might think you could
  494.     gain a little efficiency by using POSIX::strxfrm() in
  495.     conjunction with `eq':
  496.  
  497.             use POSIX qw(strxfrm);
  498.             $xfrm_string = strxfrm("Mixed-case string");
  499.             print "locale collation ignores spaces\n"
  500.                 if $xfrm_string eq strxfrm("Mixed-casestring");
  501.             print "locale collation ignores hyphens\n"
  502.                 if $xfrm_string eq strxfrm("Mixedcase string");
  503.             print "locale collation ignores case\n"
  504.                 if $xfrm_string eq strxfrm("mixed-case string");
  505.  
  506.  
  507.     strxfrm() takes a string and maps it into a transformed string
  508.     for use in byte-by-byte comparisons against other transformed
  509.     strings during collation. "Under the hood", locale-affected Perl
  510.     comparison operators call strxfrm() for both operands, then do a
  511.     byte-by-byte comparison of the transformed strings. By calling
  512.     strxfrm() explicitly and using a non locale-affected comparison,
  513.     the example attempts to save a couple of transformations. But in
  514.     fact, it doesn't save anything: Perl magic (see the "Magic
  515.     Variables" entry in the perlguts manpage) creates the
  516.     transformed version of a string the first time it's needed in a
  517.     comparison, then keeps this version around in case it's needed
  518.     again. An example rewritten the easy way with `cmp' runs just
  519.     about as fast. It also copes with null characters embedded in
  520.     strings; if you call strxfrm() directly, it treats the first
  521.     null it finds as a terminator. don't expect the transformed
  522.     strings it produces to be portable across systems--or even from
  523.     one revision of your operating system to the next. In short,
  524.     don't call strxfrm() directly: let Perl do it for you.
  525.  
  526.     Note: `use locale' isn't shown in some of these examples because
  527.     it isn't needed: strcoll() and strxfrm() exist only to generate
  528.     locale-dependent results, and so always obey the current
  529.     `LC_COLLATE' locale.
  530.  
  531.   Category LC_CTYPE: Character Types
  532.  
  533.     In the scope of `use locale', Perl obeys the `LC_CTYPE' locale
  534.     setting. This controls the application's notion of which
  535.     characters are alphabetic. This affects Perl's `\w' regular
  536.     expression metanotation, which stands for alphanumeric
  537.     characters--that is, alphabetic and numeric characters. (Consult
  538.     the perlre manpage for more information about regular
  539.     expressions.) Thanks to `LC_CTYPE', depending on your locale
  540.     setting, characters like 'æ', 'ð', 'ß', and 'ø' may be
  541.     understood as `\w' characters.
  542.  
  543.     The `LC_CTYPE' locale also provides the map used in
  544.     transliterating characters between lower and uppercase. This
  545.     affects the case-mapping functions--lc(), lcfirst, uc(), and
  546.     ucfirst(); case-mapping interpolation with `\l', `\L', `\u', or
  547.     `\U' in double-quoted strings and `s///' substitutions; and
  548.     case-independent regular expression pattern matching using the
  549.     `i' modifier.
  550.  
  551.     Finally, `LC_CTYPE' affects the POSIX character-class test
  552.     functions--isalpha(), islower(), and so on. For example, if you
  553.     move from the "C" locale to a 7-bit Scandinavian one, you may
  554.     find--possibly to your surprise--that "|" moves from the
  555.     ispunct() class to isalpha().
  556.  
  557.     Note: A broken or malicious `LC_CTYPE' locale definition may
  558.     result in clearly ineligible characters being considered to be
  559.     alphanumeric by your application. For strict matching of
  560.     (mundane) letters and digits--for example, in command strings--
  561.     locale-aware applications should use `\w' inside a `no locale'
  562.     block. See the section on "SECURITY".
  563.  
  564.   Category LC_NUMERIC: Numeric Formatting
  565.  
  566.     In the scope of `use locale', Perl obeys the `LC_NUMERIC' locale
  567.     information, which controls an application's idea of how numbers
  568.     should be formatted for human readability by the printf(),
  569.     sprintf(), and write() functions. String-to-numeric conversion
  570.     by the POSIX::strtod() function is also affected. In most
  571.     implementations the only effect is to change the character used
  572.     for the decimal point--perhaps from '.' to ','. These functions
  573.     aren't aware of such niceties as thousands separation and so on.
  574.     (See the The localeconv function manpage if you care about these
  575.     things.)
  576.  
  577.     Output produced by print() is never affected by the current
  578.     locale: it is independent of whether `use locale' or `no locale'
  579.     is in effect, and corresponds to what you'd get from printf() in
  580.     the "C" locale. The same is true for Perl's internal conversions
  581.     between numeric and string formats:
  582.  
  583.             use POSIX qw(strtod);
  584.             use locale;
  585.  
  586.             $n = 5/2;   # Assign numeric 2.5 to $n
  587.  
  588.             $a = " $n"; # Locale-independent conversion to string
  589.  
  590.             print "half five is $n\n";       # Locale-independent output
  591.  
  592.             printf "half five is %g\n", $n;  # Locale-dependent output
  593.  
  594.             print "DECIMAL POINT IS COMMA\n"
  595.                 if $n == (strtod("2,5"))[0]; # Locale-dependent conversion
  596.  
  597.  
  598.   Category LC_MONETARY: Formatting of monetary amounts
  599.  
  600.     The C standard defines the `LC_MONETARY' category, but no
  601.     function that is affected by its contents. (Those with
  602.     experience of standards committees will recognize that the
  603.     working group decided to punt on the issue.) Consequently, Perl
  604.     takes no notice of it. If you really want to use `LC_MONETARY',
  605.     you can query its contents--see the The localeconv function
  606.     manpage--and use the information that it returns in your
  607.     application's own formatting of currency amounts. However, you
  608.     may well find that the information, voluminous and complex
  609.     though it may be, still does not quite meet your requirements:
  610.     currency formatting is a hard nut to crack.
  611.  
  612.   LC_TIME
  613.  
  614.     Output produced by POSIX::strftime(), which builds a formatted
  615.     human-readable date/time string, is affected by the current
  616.     `LC_TIME' locale. Thus, in a French locale, the output produced
  617.     by the `%B' format element (full month name) for the first month
  618.     of the year would be "janvier". Here's how to get a list of long
  619.     month names in the current locale:
  620.  
  621.             use POSIX qw(strftime);
  622.             for (0..11) {
  623.                 $long_month_name[$_] =
  624.                     strftime("%B", 0, 0, 0, 1, $_, 96);
  625.             }
  626.  
  627.  
  628.     Note: `use locale' isn't needed in this example: as a function
  629.     that exists only to generate locale-dependent results,
  630.     strftime() always obeys the current `LC_TIME' locale.
  631.  
  632.   Other categories
  633.  
  634.     The remaining locale category, `LC_MESSAGES' (possibly
  635.     supplemented by others in particular implementations) is not
  636.     currently used by Perl--except possibly to affect the behavior
  637.     of library functions called by extensions outside the standard
  638.     Perl distribution.
  639.  
  640. SECURITY
  641.     Although the main discussion of Perl security issues can be
  642.     found in the perlsec manpage, a discussion of Perl's locale
  643.     handling would be incomplete if it did not draw your attention
  644.     to locale-dependent security issues. Locales--particularly on
  645.     systems that allow unprivileged users to build their own
  646.     locales--are untrustworthy. A malicious (or just plain broken)
  647.     locale can make a locale-aware application give unexpected
  648.     results. Here are a few possibilities:
  649.  
  650.     *   Regular expression checks for safe file names or mail addresses
  651.         using `\w' may be spoofed by an `LC_CTYPE' locale that
  652.         claims that characters such as ">" and "|" are alphanumeric.
  653.  
  654.     *   String interpolation with case-mapping, as in, say, `$dest =
  655.         "C:\U$name.$ext"', may produce dangerous results if a bogus
  656.         LC_CTYPE case-mapping table is in effect.
  657.  
  658.     *   If the decimal point character in the `LC_NUMERIC' locale is
  659.         surreptitiously changed from a dot to a comma,
  660.         `sprintf("%g", 0.123456e3)' produces a string result of
  661.         "123,456". Many people would interpret this as one hundred
  662.         and twenty-three thousand, four hundred and fifty-six.
  663.  
  664.     *   A sneaky `LC_COLLATE' locale could result in the names of
  665.         students with "D" grades appearing ahead of those with "A"s.
  666.  
  667.     *   An application that takes the trouble to use information in
  668.         `LC_MONETARY' may format debits as if they were credits and
  669.         vice versa if that locale has been subverted. Or it might
  670.         make payments in US dollars instead of Hong Kong dollars.
  671.  
  672.     *   The date and day names in dates formatted by strftime() could be
  673.         manipulated to advantage by a malicious user able to subvert
  674.         the `LC_DATE' locale. ("Look--it says I wasn't in the
  675.         building on Sunday.")
  676.  
  677.  
  678.     Such dangers are not peculiar to the locale system: any aspect
  679.     of an application's environment which may be modified
  680.     maliciously presents similar challenges. Similarly, they are not
  681.     specific to Perl: any programming language that allows you to
  682.     write programs that take account of their environment exposes
  683.     you to these issues.
  684.  
  685.     Perl cannot protect you from all possibilities shown in the
  686.     examples--there is no substitute for your own vigilance--but,
  687.     when `use locale' is in effect, Perl uses the tainting mechanism
  688.     (see the perlsec manpage) to mark string results that become
  689.     locale-dependent, and which may be untrustworthy in consequence.
  690.     Here is a summary of the tainting behavior of operators and
  691.     functions that may be affected by the locale:
  692.  
  693.     Comparison operators (`lt', `le', `ge', `gt' and `cmp'):
  694.         Scalar true/false (or less/equal/greater) result is never
  695.         tainted.
  696.  
  697.     Case-mapping interpolation (with `\l', `\L', `\u' or `\U')
  698.         Result string containing interpolated material is tainted if
  699.         `use locale' is in effect.
  700.  
  701.     Matching operator (`m//'):
  702.         Scalar true/false result never tainted.
  703.  
  704.         Subpatterns, either delivered as a list-context result or as
  705.         $1 etc. are tainted if `use locale' is in effect, and the
  706.         subpattern regular expression contains `\w' (to match an
  707.         alphanumeric character), `\W' (non-alphanumeric character),
  708.         `\s' (white-space character), or `\S' (non white-space
  709.         character). The matched-pattern variable, $&, $` (pre-
  710.         match), $' (post-match), and $+ (last match) are also
  711.         tainted if `use locale' is in effect and the regular
  712.         expression contains `\w', `\W', `\s', or `\S'.
  713.  
  714.     Substitution operator (`s///'):
  715.         Has the same behavior as the match operator. Also, the left
  716.         operand of `=~' becomes tainted when `use locale' in effect
  717.         if modified as a result of a substitution based on a regular
  718.         expression match involving `\w', `\W', `\s', or `\S'; or of
  719.         case-mapping with `\l', `\L',`\u' or `\U'.
  720.  
  721.     In-memory formatting function (sprintf()):
  722.         Result is tainted if `use locale' is in effect.
  723.  
  724.     Output formatting functions (printf() and write()):
  725.         Success/failure result is never tainted.
  726.  
  727.     Case-mapping functions (lc(), lcfirst(), uc(), ucfirst()):
  728.         Results are tainted if `use locale' is in effect.
  729.  
  730.     POSIX locale-dependent functions (localeconv(), strcoll(),
  731.     strftime(), strxfrm()):
  732.         Results are never tainted.
  733.  
  734.     POSIX character class tests (isalnum(), isalpha(), isdigit(),
  735.     isgraph(), islower(), isprint(), ispunct(), isspace(), isupper(),
  736.     isxdigit()):
  737.         True/false results are never tainted.
  738.  
  739.  
  740.     Three examples illustrate locale-dependent tainting. The first
  741.     program, which ignores its locale, won't run: a value taken
  742.     directly from the command line may not be used to name an output
  743.     file when taint checks are enabled.
  744.  
  745.             #/usr/local/bin/perl -T
  746.             # Run with taint checking
  747.  
  748.             # Command line sanity check omitted...
  749.             $tainted_output_file = shift;
  750.  
  751.             open(F, ">$tainted_output_file")
  752.                 or warn "Open of $untainted_output_file failed: $!\n";
  753.  
  754.  
  755.     The program can be made to run by "laundering" the tainted value
  756.     through a regular expression: the second example--which still
  757.     ignores locale information--runs, creating the file named on its
  758.     command line if it can.
  759.  
  760.             #/usr/local/bin/perl -T
  761.  
  762.             $tainted_output_file = shift;
  763.             $tainted_output_file =~ m%[\w/]+%;
  764.             $untainted_output_file = $&;
  765.  
  766.             open(F, ">$untainted_output_file")
  767.                 or warn "Open of $untainted_output_file failed: $!\n";
  768.  
  769.  
  770.     Compare this with a similar but locale-aware program:
  771.  
  772.             #/usr/local/bin/perl -T
  773.  
  774.             $tainted_output_file = shift;
  775.             use locale;
  776.             $tainted_output_file =~ m%[\w/]+%;
  777.             $localized_output_file = $&;
  778.  
  779.             open(F, ">$localized_output_file")
  780.                 or warn "Open of $localized_output_file failed: $!\n";
  781.  
  782.  
  783.     This third program fails to run because $& is tainted: it is the
  784.     result of a match involving `\w' while `use locale' is in
  785.     effect.
  786.  
  787. ENVIRONMENT
  788.     PERL_BADLANG
  789.                 A string that can suppress Perl's warning about
  790.                 failed locale settings at startup. Failure can occur
  791.                 if the locale support in the operating system is
  792.                 lacking (broken) in some way--or if you mistyped the
  793.                 name of a locale when you set up your environment.
  794.                 If this environment variable is absent, or has a
  795.                 value that does not evaluate to integer zero--that
  796.                 is, "0" or ""-- Perl will complain about locale
  797.                 setting failures.
  798.  
  799.                 NOTE: PERL_BADLANG only gives you a way to hide the
  800.                 warning message. The message tells about some
  801.                 problem in your system's locale support, and you
  802.                 should investigate what the problem is.
  803.  
  804.  
  805.     The following environment variables are not specific to Perl:
  806.     They are part of the standardized (ISO C, XPG4, POSIX 1.c)
  807.     setlocale() method for controlling an application's opinion on
  808.     data.
  809.  
  810.     LC_ALL      `LC_ALL' is the "override-all" locale environment
  811.                 variable. If set, it overrides all the rest of the
  812.                 locale environment variables.
  813.  
  814.     LANGUAGE    NOTE: `LANGUAGE' is a GNU extension, it affects you only
  815.                 if you are using the GNU libc. This is the case if
  816.                 you are using e.g. Linux. If you are using
  817.                 "commercial" UNIXes you are most probably *not*
  818.                 using GNU libc and you can ignore `LANGUAGE'.
  819.  
  820.                 However, in the case you are using `LANGUAGE': it
  821.                 affects the language of informational, warning, and
  822.                 error messages output by commands (in other words,
  823.                 it's like `LC_MESSAGES') but it has higher priority
  824.                 than the LC_ALL manpage. Moreover, it's not a single
  825.                 value but instead a "path" (":"-separated list) of
  826.                 *languages* (not locales). See the GNU `gettext'
  827.                 library documentation for more information.
  828.  
  829.     LC_CTYPE    In the absence of `LC_ALL', `LC_CTYPE' chooses the
  830.                 character type locale. In the absence of both
  831.                 `LC_ALL' and `LC_CTYPE', `LANG' chooses the
  832.                 character type locale.
  833.  
  834.     LC_COLLATE  In the absence of `LC_ALL', `LC_COLLATE' chooses the
  835.                 collation (sorting) locale. In the absence of both
  836.                 `LC_ALL' and `LC_COLLATE', `LANG' chooses the
  837.                 collation locale.
  838.  
  839.     LC_MONETARY In the absence of `LC_ALL', `LC_MONETARY' chooses the
  840.                 monetary formatting locale. In the absence of both
  841.                 `LC_ALL' and `LC_MONETARY', `LANG' chooses the
  842.                 monetary formatting locale.
  843.  
  844.     LC_NUMERIC  In the absence of `LC_ALL', `LC_NUMERIC' chooses the
  845.                 numeric format locale. In the absence of both
  846.                 `LC_ALL' and `LC_NUMERIC', `LANG' chooses the
  847.                 numeric format.
  848.  
  849.     LC_TIME     In the absence of `LC_ALL', `LC_TIME' chooses the date
  850.                 and time formatting locale. In the absence of both
  851.                 `LC_ALL' and `LC_TIME', `LANG' chooses the date and
  852.                 time formatting locale.
  853.  
  854.     LANG        `LANG' is the "catch-all" locale environment variable.
  855.                 If it is set, it is used as the last resort after
  856.                 the overall `LC_ALL' and the category-specific
  857.                 `LC_...'.
  858.  
  859.  
  860. NOTES
  861.   Backward compatibility
  862.  
  863.     Versions of Perl prior to 5.004 mostly ignored locale
  864.     information, generally behaving as if something similar to the
  865.     `"C"' locale were always in force, even if the program
  866.     environment suggested otherwise (see the The setlocale function
  867.     manpage). By default, Perl still behaves this way for backward
  868.     compatibility. If you want a Perl application to pay attention
  869.     to locale information, you must use the `use locale' pragma (see
  870.     the The use locale pragma manpage) to instruct it to do so.
  871.  
  872.     Versions of Perl from 5.002 to 5.003 did use the `LC_CTYPE'
  873.     information if available; that is, `\w' did understand what were
  874.     the letters according to the locale environment variables. The
  875.     problem was that the user had no control over the feature: if
  876.     the C library supported locales, Perl used them.
  877.  
  878.   I18N:Collate obsolete
  879.  
  880.     In versions of Perl prior to 5.004, per-locale collation was
  881.     possible using the `I18N::Collate' library module. This module
  882.     is now mildly obsolete and should be avoided in new
  883.     applications. The `LC_COLLATE' functionality is now integrated
  884.     into the Perl core language: One can use locale-specific scalar
  885.     data completely normally with `use locale', so there is no
  886.     longer any need to juggle with the scalar references of
  887.     `I18N::Collate'.
  888.  
  889.   Sort speed and memory use impacts
  890.  
  891.     Comparing and sorting by locale is usually slower than the
  892.     default sorting; slow-downs of two to four times have been
  893.     observed. It will also consume more memory: once a Perl scalar
  894.     variable has participated in any string comparison or sorting
  895.     operation obeying the locale collation rules, it will take 3-15
  896.     times more memory than before. (The exact multiplier depends on
  897.     the string's contents, the operating system and the locale.)
  898.     These downsides are dictated more by the operating system's
  899.     implementation of the locale system than by Perl.
  900.  
  901.   write() and LC_NUMERIC
  902.  
  903.     Formats are the only part of Perl that unconditionally use
  904.     information from a program's locale; if a program's environment
  905.     specifies an LC_NUMERIC locale, it is always used to specify the
  906.     decimal point character in formatted output. Formatted output
  907.     cannot be controlled by `use locale' because the pragma is tied
  908.     to the block structure of the program, and, for historical
  909.     reasons, formats exist outside that block structure.
  910.  
  911.   Freely available locale definitions
  912.  
  913.     There is a large collection of locale definitions at
  914.     `ftp://dkuug.dk/i18n/WG15-collection'. You should be aware that
  915.     it is unsupported, and is not claimed to be fit for any purpose.
  916.     If your system allows installation of arbitrary locales, you may
  917.     find the definitions useful as they are, or as a basis for the
  918.     development of your own locales.
  919.  
  920.   I18n and l10n
  921.  
  922.     "Internationalization" is often abbreviated as i18n because its
  923.     first and last letters are separated by eighteen others. (You
  924.     may guess why the internalin ... internaliti ... i18n tends to
  925.     get abbreviated.) In the same way, "localization" is often
  926.     abbreviated to l10n.
  927.  
  928.   An imperfect standard
  929.  
  930.     Internationalization, as defined in the C and POSIX standards,
  931.     can be criticized as incomplete, ungainly, and having too large
  932.     a granularity. (Locales apply to a whole process, when it would
  933.     arguably be more useful to have them apply to a single thread,
  934.     window group, or whatever.) They also have a tendency, like
  935.     standards groups, to divide the world into nations, when we all
  936.     know that the world can equally well be divided into bankers,
  937.     bikers, gamers, and so on. But, for now, it's the only standard
  938.     we've got. This may be construed as a bug.
  939.  
  940. BUGS
  941.   Broken systems
  942.  
  943.     In certain systems, the operating system's locale support is
  944.     broken and cannot be fixed or used by Perl. Such deficiencies
  945.     can and will result in mysterious hangs and/or Perl core dumps
  946.     when the `use locale' is in effect. When confronted with such a
  947.     system, please report in excruciating detail to
  948.     <perlbug@perl.com>, and complain to your vendor: bug fixes may
  949.     exist for these problems in your operating system. Sometimes
  950.     such bug fixes are called an operating system upgrade.
  951.  
  952. SEE ALSO
  953.     the "isalnum" entry in the POSIX (3) manpage
  954.  
  955.     the "isalpha" entry in the POSIX (3) manpage
  956.  
  957.     the "isdigit" entry in the POSIX (3) manpage
  958.  
  959.     the "isgraph" entry in the POSIX (3) manpage
  960.  
  961.     the "islower" entry in the POSIX (3) manpage
  962.  
  963.     the "isprint" entry in the POSIX (3) manpage,
  964.  
  965.     the "ispunct" entry in the POSIX (3) manpage
  966.  
  967.     the "isspace" entry in the POSIX (3) manpage
  968.  
  969.     the "isupper" entry in the POSIX (3) manpage,
  970.  
  971.     the "isxdigit" entry in the POSIX (3) manpage
  972.  
  973.     the "localeconv" entry in the POSIX (3) manpage
  974.  
  975.     the "setlocale" entry in the POSIX (3) manpage,
  976.  
  977.     the "strcoll" entry in the POSIX (3) manpage
  978.  
  979.     the "strftime" entry in the POSIX (3) manpage
  980.  
  981.     the "strtod" entry in the POSIX (3) manpage,
  982.  
  983.     the "strxfrm" entry in the POSIX (3) manpage
  984.  
  985. HISTORY
  986.     Jarkko Hietaniemi's original perli18n.pod heavily hacked by
  987.     Dominic Dunlop, assisted by the perl5-porters. Prose worked over
  988.     a bit by Tom Christiansen.
  989.  
  990.     Last update: Thu Jun 11 08:44:13 MDT 1998
  991.  
  992.