home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / RiscOS / APP / DEVS / PERL / RPC106.ZIP / Rpc106 / Docs / perllocale < prev    next >
Text File  |  1997-11-23  |  33KB  |  767 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 very na∩ve to think that `A-Za-z'
  11.     defines all the "letters". Perl is also aware that some
  12.     character other than '.' may be preferred as a decimal point,
  13.     and that output date representations may be language-specific.
  14.     The process of making an application take account of its users'
  15.     preferences in such matters is called internationalization
  16.     (often abbreviated as i18n); telling such an application about a
  17.     particular set of preferences is known as localization (l10n).
  18.  
  19.     Perl can understand language-specific data via the standardized
  20.     (ISO C, XPG4, POSIX 1.c) method called "the locale system". The
  21.     locale system is controlled per application using one pragma,
  22.     one function call, and several environment variables.
  23.  
  24.     NOTE: This feature is new in Perl 5.004, and does not apply
  25.     unless an application specifically requests it - see the section
  26.     on "Backward compatibility". The one exception is that write()
  27.     now always uses the current locale - see the section on "NOTES".
  28.  
  29. PREPARING TO USE LOCALES
  30.     If Perl applications are to be able to understand and present
  31.     your data correctly according a locale of your choice, all of
  32.     the following must be true:
  33.  
  34.     *    Your operating system must support the locale system. If it
  35.     does, you should find that the setlocale() function is a
  36.     documented part of its C library.
  37.  
  38.     *    Definitions for the locales which you use must be installed.
  39.     You, or your system administrator, must make sure that this
  40.     is the case. The available locales, the location in which
  41.     they are kept, and the manner in which they are installed,
  42.     vary from system to system. Some systems provide only a few,
  43.     hard-wired, locales, and do not allow more to be added;
  44.     others allow you to add "canned" locales provided by the
  45.     system supplier; still others allow you or the system
  46.     administrator to define and add arbitrary locales. (You may
  47.     have to ask your supplier to provide canned locales which
  48.     are not delivered with your operating system.) Read your
  49.     system documentation for further illumination.
  50.  
  51.     *    Perl must believe that the locale system is supported. If it
  52.     does, `perl -V:d_setlocale' will say that the value for
  53.     `d_setlocale' is `define'.
  54.  
  55.     If you want a Perl application to process and present your data
  56.     according to a particular locale, the application code should
  57.     include the `use locale' pragma (see the section on "The use
  58.     locale pragma") where appropriate, and at least one of the
  59.     following must be true:
  60.  
  61.     *    The locale-determining environment variables (see the section on
  62.     "ENVIRONMENT") must be correctly set up, either by yourself,
  63.     or by the person who set up your system account, at the time
  64.     the application is started.
  65.  
  66.     *    The application must set its own locale using the method
  67.     described in the section on "The setlocale function".
  68.  
  69. USING LOCALES
  70.   The use locale pragma
  71.  
  72.     By default, Perl ignores the current locale. The `use locale'
  73.     pragma tells Perl to use the current locale for some operations:
  74.  
  75.     *    The comparison operators (`lt', `le', `cmp', `ge', and `gt') and
  76.     the POSIX string collation functions strcoll() and strxfrm()
  77.     use `LC_COLLATE'. sort() is also affected if it is used
  78.     without an explicit comparison function because it uses
  79.     `cmp' by default.
  80.  
  81.     Note: `eq' and `ne' are unaffected by the locale: they
  82.     always perform a byte-by-byte comparison of their scalar
  83.     operands. What's more, if `cmp' finds that its operands are
  84.     equal according to the collation sequence specified by the
  85.     current locale, it goes on to perform a byte-by-byte
  86.     comparison, and only returns *0* (equal) if the operands are
  87.     bit-for-bit identical. If you really want to know whether
  88.     two strings - which `eq' and `cmp' may consider different -
  89.     are equal as far as collation in the locale is concerned,
  90.     see the discussion in the section on "Category LC_COLLATE:
  91.     Collation".
  92.  
  93.     *    Regular expressions and case-modification functions (uc(), lc(),
  94.     ucfirst(), and lcfirst()) use `LC_CTYPE'
  95.  
  96.     *    The formatting functions (printf(), sprintf() and write()) use
  97.     `LC_NUMERIC'
  98.  
  99.     *    The POSIX date formatting function (strftime()) uses `LC_TIME'.
  100.  
  101.     `LC_COLLATE', `LC_CTYPE', and so on, are discussed further in
  102.     the section on "LOCALE CATEGORIES".
  103.  
  104.     The default behavior returns with `no locale' or on reaching the
  105.     end of the enclosing block.
  106.  
  107.     Note that the string result of any operation that uses locale
  108.     information is tainted, as it is possible for a locale to be
  109.     untrustworthy. See the section on "SECURITY".
  110.  
  111.   The setlocale function
  112.  
  113.     You can switch locales as often as you wish at run time with the
  114.     POSIX::setlocale() function:
  115.  
  116.         # This functionality not usable prior to Perl 5.004
  117.         require 5.004;
  118.  
  119.         # Import locale-handling tool set from POSIX module.
  120.         # This example uses: setlocale -- the function call
  121.         #             LC_CTYPE -- explained below
  122.         use POSIX qw(locale_h);
  123.  
  124.         # query and save the old locale
  125.         $old_locale = setlocale(LC_CTYPE);
  126.  
  127.         setlocale(LC_CTYPE, "fr_CA.ISO8859-1");
  128.         # LC_CTYPE now in locale "French, Canada, codeset ISO 8859-1"
  129.  
  130.         setlocale(LC_CTYPE, "");
  131.         # LC_CTYPE now reset to default defined by LC_ALL/LC_CTYPE/LANG
  132.         # environment variables.  See below for documentation.
  133.  
  134.         # restore the old locale
  135.         setlocale(LC_CTYPE, $old_locale);
  136.  
  137.     The first argument of setlocale() gives the category, the second
  138.     the locale. The category tells in what aspect of data processing
  139.     you want to apply locale-specific rules. Category names are
  140.     discussed in the section on "LOCALE CATEGORIES" and the section
  141.     on "ENVIRONMENT". The locale is the name of a collection of
  142.     customization information corresponding to a particular
  143.     combination of language, country or territory, and codeset. Read
  144.     on for hints on the naming of locales: not all systems name
  145.     locales as in the example.
  146.  
  147.     If no second argument is provided, the function returns a string
  148.     naming the current locale for the category. You can use this
  149.     value as the second argument in a subsequent call to
  150.     setlocale(). If a second argument is given and it corresponds to
  151.     a valid locale, the locale for the category is set to that
  152.     value, and the function returns the now-current locale value.
  153.     You can use this in a subsequent call to setlocale(). (In some
  154.     implementations, the return value may sometimes differ from the
  155.     value you gave as the second argument - think of it as an alias
  156.     for the value that you gave.)
  157.  
  158.     As the example shows, if the second argument is an empty string,
  159.     the category's locale is returned to the default specified by
  160.     the corresponding environment variables. Generally, this results
  161.     in a return to the default which was in force when Perl started
  162.     up: changes to the environment made by the application after
  163.     startup may or may not be noticed, depending on the
  164.     implementation of your system's C library.
  165.  
  166.     If the second argument does not correspond to a valid locale,
  167.     the locale for the category is not changed, and the function
  168.     returns *undef*.
  169.  
  170.     For further information about the categories, consult the
  171.     setlocale(3) manpage. For the locales available in your system,
  172.     also consult the setlocale(3) manpage and see whether it leads
  173.     you to the list of the available locales (search for the *SEE
  174.     ALSO* section). If that fails, try the following command lines:
  175.  
  176.         locale -a
  177.  
  178.         nlsinfo
  179.  
  180.         ls /usr/lib/nls/loc
  181.  
  182.         ls /usr/lib/locale
  183.  
  184.         ls /usr/lib/nls
  185.  
  186.     and see whether they list something resembling these
  187.  
  188.         en_US.ISO8859-1    de_DE.ISO8859-1     ru_RU.ISO8859-5
  189.         en_US        de_DE            ru_RU
  190.         en            de            ru
  191.         english        german            russian
  192.         english.iso88591    german.iso88591     russian.iso88595
  193.  
  194.     Sadly, even though the calling interface for setlocale() has
  195.     been standardized, the names of the locales and the directories
  196.     where the configuration is, have not. The basic form of the name
  197.     is *language_country/territory*.*codeset*, but the latter parts
  198.     are not always present.
  199.  
  200.     Two special locales are worth particular mention: "C" and
  201.     "POSIX". Currently these are effectively the same locale: the
  202.     difference is mainly that the first one is defined by the C
  203.     standard and the second by the POSIX standard. What they define
  204.     is the default locale in which every program starts in the
  205.     absence of locale information in its environment. (The default
  206.     default locale, if you will.) Its language is (American) English
  207.     and its character codeset ASCII.
  208.  
  209.     NOTE: Not all systems have the "POSIX" locale (not all systems
  210.     are POSIX-conformant), so use "C" when you need explicitly to
  211.     specify this default locale.
  212.  
  213.   The localeconv function
  214.  
  215.     The POSIX::localeconv() function allows you to get particulars
  216.     of the locale-dependent numeric formatting information specified
  217.     by the current `LC_NUMERIC' and `LC_MONETARY' locales. (If you
  218.     just want the name of the current locale for a particular
  219.     category, use POSIX::setlocale() with a single parameter - see
  220.     the section on "The setlocale function".)
  221.  
  222.         use POSIX qw(locale_h);
  223.  
  224.         # Get a reference to a hash of locale-dependent info
  225.         $locale_values = localeconv();
  226.  
  227.         # Output sorted list of the values
  228.         for (sort keys %$locale_values) {
  229.         printf "%-20s = %s\n", $_, $locale_values->{$_}
  230.         }
  231.  
  232.     localeconv() takes no arguments, and returns a reference to a
  233.     hash. The keys of this hash are formatting variable names such
  234.     as `decimal_point' and `thousands_sep'; the values are the
  235.     corresponding values. See the "localeconv" entry in the POSIX
  236.     (3) manpage for a longer example, which lists all the categories
  237.     an implementation might be expected to provide; some provide
  238.     more and others fewer, however. Note that you don't need `use
  239.     locale': as a function with the job of querying the locale,
  240.     localeconv() always observes the current locale.
  241.  
  242.     Here's a simple-minded example program which rewrites its
  243.     command line parameters as integers formatted correctly in the
  244.     current locale:
  245.  
  246.         # See comments in previous example
  247.         require 5.004;
  248.         use POSIX qw(locale_h);
  249.  
  250.         # Get some of locale's numeric formatting parameters
  251.         my ($thousands_sep, $grouping) =
  252.          @{localeconv()}{'thousands_sep', 'grouping'};
  253.  
  254.         # Apply defaults if values are missing
  255.         $thousands_sep = ',' unless $thousands_sep;
  256.         $grouping = 3 unless $grouping;
  257.  
  258.         # Format command line params for current locale
  259.         for (@ARGV) {
  260.         $_ = int;    # Chop non-integer part
  261.         1 while
  262.         s/(\d)(\d{$grouping}($|$thousands_sep))/$1$thousands_sep$2/;
  263.         print "$_";
  264.         }
  265.         print "\n";
  266.  
  267. LOCALE CATEGORIES
  268.     The subsections which follow describe basic locale categories.
  269.     As well as these, there are some combination categories which
  270.     allow the manipulation of more than one basic category at a
  271.     time. See the section on "ENVIRONMENT" for a discussion of
  272.     these.
  273.  
  274.   Category LC_COLLATE: Collation
  275.  
  276.     When in the scope of `use locale', Perl looks to the
  277.     `LC_COLLATE' environment variable to determine the application's
  278.     notions on the collation (ordering) of characters. ('b' follows
  279.     'a' in Latin alphabets, but where do 'ß' and 'σ' belong?)
  280.  
  281.     Here is a code snippet that will tell you what are the
  282.     alphanumeric characters in the current locale, in the locale
  283.     order:
  284.  
  285.         use locale;
  286.         print +(sort grep /\w/, map { chr() } 0..255), "\n";
  287.  
  288.     Compare this with the characters that you see and their order if
  289.     you state explicitly that the locale should be ignored:
  290.  
  291.         no locale;
  292.         print +(sort grep /\w/, map { chr() } 0..255), "\n";
  293.  
  294.     This machine-native collation (which is what you get unless `use
  295.     locale' has appeared earlier in the same block) must be used for
  296.     sorting raw binary data, whereas the locale-dependent collation
  297.     of the first example is useful for natural text.
  298.  
  299.     As noted in the section on "USING LOCALES", `cmp' compares
  300.     according to the current collation locale when `use locale' is
  301.     in effect, but falls back to a byte-by-byte comparison for
  302.     strings which the locale says are equal. You can use
  303.     POSIX::strcoll() if you don't want this fall-back:
  304.  
  305.         use POSIX qw(strcoll);
  306.         $equal_in_locale =
  307.         !strcoll("space and case ignored", "SpaceAndCaseIgnored");
  308.  
  309.     $equal_in_locale will be true if the collation locale specifies
  310.     a dictionary-like ordering which ignores space characters
  311.     completely, and which folds case.
  312.  
  313.     If you have a single string which you want to check for
  314.     "equality in locale" against several others, you might think you
  315.     could gain a little efficiency by using POSIX::strxfrm() in
  316.     conjunction with `eq':
  317.  
  318.         use POSIX qw(strxfrm);
  319.         $xfrm_string = strxfrm("Mixed-case string");
  320.         print "locale collation ignores spaces\n"
  321.         if $xfrm_string eq strxfrm("Mixed-casestring");
  322.         print "locale collation ignores hyphens\n"
  323.         if $xfrm_string eq strxfrm("Mixedcase string");
  324.         print "locale collation ignores case\n"
  325.         if $xfrm_string eq strxfrm("mixed-case string");
  326.  
  327.     strxfrm() takes a string and maps it into a transformed string
  328.     for use in byte-by-byte comparisons against other transformed
  329.     strings during collation. "Under the hood", locale-affected Perl
  330.     comparison operators call strxfrm() for both their operands,
  331.     then do a byte-by-byte comparison of the transformed strings. By
  332.     calling strxfrm() explicitly, and using a non locale-affected
  333.     comparison, the example attempts to save a couple of
  334.     transformations. In fact, it doesn't save anything: Perl magic
  335.     (see the section on "Magic Variables" in the perlguts manpage)
  336.     creates the transformed version of a string the first time it's
  337.     needed in a comparison, then keeps it around in case it's needed
  338.     again. An example rewritten the easy way with `cmp' runs just
  339.     about as fast. It also copes with null characters embedded in
  340.     strings; if you call strxfrm() directly, it treats the first
  341.     null it finds as a terminator. And don't expect the transformed
  342.     strings it produces to be portable across systems - or even from
  343.     one revision of your operating system to the next. In short,
  344.     don't call strxfrm() directly: let Perl do it for you.
  345.  
  346.     Note: `use locale' isn't shown in some of these examples, as it
  347.     isn't needed: strcoll() and strxfrm() exist only to generate
  348.     locale-dependent results, and so always obey the current
  349.     `LC_COLLATE' locale.
  350.  
  351.   Category LC_CTYPE: Character Types
  352.  
  353.     When in the scope of `use locale', Perl obeys the `LC_CTYPE'
  354.     locale setting. This controls the application's notion of which
  355.     characters are alphabetic. This affects Perl's `\w' regular
  356.     expression metanotation, which stands for alphanumeric
  357.     characters - that is, alphabetic and numeric characters.
  358.     (Consult the perlre manpage for more information about regular
  359.     expressions.) Thanks to `LC_CTYPE', depending on your locale
  360.     setting, characters like 'µ', '≡', '▀', and '°' may be
  361.     understood as `\w' characters.
  362.  
  363.     The `LC_CTYPE' locale also provides the map used in translating
  364.     characters between lower and uppercase. This affects the case-
  365.     mapping functions - lc(), lcfirst, uc() and ucfirst(); case-
  366.     mapping interpolation with `\l', `\L', `\u' or <\U> in double-
  367.     quoted strings and in `s///' substitutions; and case-independent
  368.     regular expression pattern matching using the `i' modifier.
  369.  
  370.     Finally, `LC_CTYPE' affects the POSIX character-class test
  371.     functions - isalpha(), islower() and so on. For example, if you
  372.     move from the "C" locale to a 7-bit Scandinavian one, you may
  373.     find - possibly to your surprise - that "|" moves from the
  374.     ispunct() class to isalpha().
  375.  
  376.     Note: A broken or malicious `LC_CTYPE' locale definition may
  377.     result in clearly ineligible characters being considered to be
  378.     alphanumeric by your application. For strict matching of
  379.     (unaccented) letters and digits - for example, in command
  380.     strings - locale-aware applications should use `\w' inside a `no
  381.     locale' block. See the section on "SECURITY".
  382.  
  383.   Category LC_NUMERIC: Numeric Formatting
  384.  
  385.     When in the scope of `use locale', Perl obeys the `LC_NUMERIC'
  386.     locale information, which controls application's idea of how
  387.     numbers should be formatted for human readability by the
  388.     printf(), sprintf(), and write() functions. String to numeric
  389.     conversion by the POSIX::strtod() function is also affected. In
  390.     most implementations the only effect is to change the character
  391.     used for the decimal point - perhaps from '.' to ',': these
  392.     functions aren't aware of such niceties as thousands separation
  393.     and so on. (See the section on "The localeconv function" if you
  394.     care about these things.)
  395.  
  396.     Note that output produced by print() is never affected by the
  397.     current locale: it is independent of whether `use locale' or `no
  398.     locale' is in effect, and corresponds to what you'd get from
  399.     printf() in the "C" locale. The same is true for Perl's internal
  400.     conversions between numeric and string formats:
  401.  
  402.         use POSIX qw(strtod);
  403.         use locale;
  404.  
  405.         $n = 5/2;    # Assign numeric 2.5 to $n
  406.  
  407.         $a = " $n"; # Locale-independent conversion to string
  408.  
  409.         print "half five is $n\n";         # Locale-independent output
  410.  
  411.         printf "half five is %g\n", $n;  # Locale-dependent output
  412.  
  413.         print "DECIMAL POINT IS COMMA\n"
  414.         if $n == (strtod("2,5"))[0]; # Locale-dependent conversion
  415.  
  416.   Category LC_MONETARY: Formatting of monetary amounts
  417.  
  418.     The C standard defines the `LC_MONETARY' category, but no
  419.     function that is affected by its contents. (Those with
  420.     experience of standards committees will recognize that the
  421.     working group decided to punt on the issue.) Consequently, Perl
  422.     takes no notice of it. If you really want to use `LC_MONETARY',
  423.     you can query its contents - see the section on "The localeconv
  424.     function" - and use the information that it returns in your
  425.     application's own formatting of currency amounts. However, you
  426.     may well find that the information, though voluminous and
  427.     complex, does not quite meet your requirements: currency
  428.     formatting is a hard nut to crack.
  429.  
  430.   LC_TIME
  431.  
  432.     The output produced by POSIX::strftime(), which builds a
  433.     formatted human-readable date/time string, is affected by the
  434.     current `LC_TIME' locale. Thus, in a French locale, the output
  435.     produced by the `%B' format element (full month name) for the
  436.     first month of the year would be "janvier". Here's how to get a
  437.     list of the long month names in the current locale:
  438.  
  439.         use POSIX qw(strftime);
  440.         for (0..11) {
  441.         $long_month_name[$_] =
  442.             strftime("%B", 0, 0, 0, 1, $_, 96);
  443.         }
  444.  
  445.     Note: `use locale' isn't needed in this example: as a function
  446.     which exists only to generate locale-dependent results,
  447.     strftime() always obeys the current `LC_TIME' locale.
  448.  
  449.   Other categories
  450.  
  451.     The remaining locale category, `LC_MESSAGES' (possibly
  452.     supplemented by others in particular implementations) is not
  453.     currently used by Perl - except possibly to affect the behavior
  454.     of library functions called by extensions which are not part of
  455.     the standard Perl distribution.
  456.  
  457. SECURITY
  458.     While the main discussion of Perl security issues can be found
  459.     in the perlsec manpage, a discussion of Perl's locale handling
  460.     would be incomplete if it did not draw your attention to locale-
  461.     dependent security issues. Locales - particularly on systems
  462.     which allow unprivileged users to build their own locales - are
  463.     untrustworthy. A malicious (or just plain broken) locale can
  464.     make a locale-aware application give unexpected results. Here
  465.     are a few possibilities:
  466.  
  467.     *    Regular expression checks for safe file names or mail addresses
  468.     using `\w' may be spoofed by an `LC_CTYPE' locale which
  469.     claims that characters such as ">" and "|" are alphanumeric.
  470.  
  471.     *    String interpolation with case-mapping, as in, say, `$dest =
  472.     "C:\U$name.$ext"', may produce dangerous results if a bogus
  473.     LC_CTYPE case-mapping table is in effect.
  474.  
  475.     *    If the decimal point character in the `LC_NUMERIC' locale is
  476.     surreptitiously changed from a dot to a comma,
  477.     `sprintf("%g", 0.123456e3)' produces a string result of
  478.     "123,456". Many people would interpret this as one hundred
  479.     and twenty-three thousand, four hundred and fifty-six.
  480.  
  481.     *    A sneaky `LC_COLLATE' locale could result in the names of
  482.     students with "D" grades appearing ahead of those with "A"s.
  483.  
  484.     *    An application which takes the trouble to use the information in
  485.     `LC_MONETARY' may format debits as if they were credits and
  486.     vice versa if that locale has been subverted. Or it make may
  487.     make payments in US dollars instead of Hong Kong dollars.
  488.  
  489.     *    The date and day names in dates formatted by strftime() could be
  490.     manipulated to advantage by a malicious user able to subvert
  491.     the `LC_DATE' locale. ("Look - it says I wasn't in the
  492.     building on Sunday.")
  493.  
  494.     Such dangers are not peculiar to the locale system: any aspect
  495.     of an application's environment which may maliciously be
  496.     modified presents similar challenges. Similarly, they are not
  497.     specific to Perl: any programming language which allows you to
  498.     write programs which take account of their environment exposes
  499.     you to these issues.
  500.  
  501.     Perl cannot protect you from all of the possibilities shown in
  502.     the examples - there is no substitute for your own vigilance -
  503.     but, when `use locale' is in effect, Perl uses the tainting
  504.     mechanism (see the perlsec manpage) to mark string results which
  505.     become locale-dependent, and which may be untrustworthy in
  506.     consequence. Here is a summary of the tainting behavior of
  507.     operators and functions which may be affected by the locale:
  508.  
  509.     Comparison operators (`lt', `le', `ge', `gt' and `cmp'):
  510.     Scalar true/false (or less/equal/greater) result is never
  511.     tainted.
  512.  
  513.     Case-mapping interpolation (with `\l', `\L', `\u' or <\U>)
  514.     Result string containing interpolated material is tainted if
  515.     `use locale' is in effect.
  516.  
  517.     Matching operator (`m//'):
  518.     Scalar true/false result never tainted.
  519.  
  520.     Subpatterns, either delivered as an array-context result, or
  521.     as $1 etc. are tainted if `use locale' is in effect, and the
  522.     subpattern regular expression contains `\w' (to match an
  523.     alphanumeric character), `\W' (non-alphanumeric character),
  524.     `\s' (white-space character), or `\S' (non white-space
  525.     character). The matched pattern variable, $&, $` (pre-
  526.     match), $' (post-match), and $+ (last match) are also
  527.     tainted if `use locale' is in effect and the regular
  528.     expression contains `\w', `\W', `\s', or `\S'.
  529.  
  530.     Substitution operator (`s///'):
  531.     Has the same behavior as the match operator. Also, the left
  532.     operand of `=~' becomes tainted when `use locale' in effect,
  533.     if it is modified as a result of a substitution based on a
  534.     regular expression match involving `\w', `\W', `\s', or
  535.     `\S'; or of case-mapping with `\l', `\L',`\u' or <\U>.
  536.  
  537.     In-memory formatting function (sprintf()):
  538.     Result is tainted if "use locale" is in effect.
  539.  
  540.     Output formatting functions (printf() and write()):
  541.     Success/failure result is never tainted.
  542.  
  543.     Case-mapping functions (lc(), lcfirst(), uc(), ucfirst()):
  544.     Results are tainted if `use locale' is in effect.
  545.  
  546.     POSIX locale-dependent functions (localeconv(), strcoll(),
  547.     strftime(), strxfrm()):
  548.     Results are never tainted.
  549.  
  550.     POSIX character class tests (isalnum(), isalpha(), isdigit(),
  551.     isgraph(), islower(), isprint(), ispunct(), isspace(), isupper(),
  552.     isxdigit()):
  553.     True/false results are never tainted.
  554.  
  555.     Three examples illustrate locale-dependent tainting. The first
  556.     program, which ignores its locale, won't run: a value taken
  557.     directly from the command line may not be used to name an output
  558.     file when taint checks are enabled.
  559.  
  560.         #/usr/local/bin/perl -T
  561.         # Run with taint checking
  562.  
  563.         # Command line sanity check omitted...
  564.         $tainted_output_file = shift;
  565.  
  566.         open(F, ">$tainted_output_file")
  567.         or warn "Open of $untainted_output_file failed: $!\n";
  568.  
  569.     The program can be made to run by "laundering" the tainted value
  570.     through a regular expression: the second example - which still
  571.     ignores locale information - runs, creating the file named on
  572.     its command line if it can.
  573.  
  574.         #/usr/local/bin/perl -T
  575.  
  576.         $tainted_output_file = shift;
  577.         $tainted_output_file =~ m%[\w/]+%;
  578.         $untainted_output_file = $&;
  579.  
  580.         open(F, ">$untainted_output_file")
  581.         or warn "Open of $untainted_output_file failed: $!\n";
  582.  
  583.     Compare this with a very similar program which is locale-aware:
  584.  
  585.         #/usr/local/bin/perl -T
  586.  
  587.         $tainted_output_file = shift;
  588.         use locale;
  589.         $tainted_output_file =~ m%[\w/]+%;
  590.         $localized_output_file = $&;
  591.  
  592.         open(F, ">$localized_output_file")
  593.         or warn "Open of $localized_output_file failed: $!\n";
  594.  
  595.     This third program fails to run because $& is tainted: it is the
  596.     result of a match involving `\w' when `use locale' is in effect.
  597.  
  598. ENVIRONMENT
  599.     PERL_BADLANG
  600.         A string that can suppress Perl's warning about
  601.         failed locale settings at startup. Failure can occur
  602.         if the locale support in the operating system is
  603.         lacking (broken) is some way - or if you mistyped
  604.         the name of a locale when you set up your
  605.         environment. If this environment variable is absent,
  606.         or has a value which does not evaluate to integer
  607.         zero - that is "0" or "" - Perl will complain about
  608.         locale setting failures.
  609.  
  610.         NOTE: PERL_BADLANG only gives you a way to hide the
  611.         warning message. The message tells about some
  612.         problem in your system's locale support, and you
  613.         should investigate what the problem is.
  614.  
  615.     The following environment variables are not specific to Perl:
  616.     They are part of the standardized (ISO C, XPG4, POSIX 1.c)
  617.     setlocale() method for controlling an application's opinion on
  618.     data.
  619.  
  620.     LC_ALL    `LC_ALL' is the "override-all" locale environment
  621.         variable. If it is set, it overrides all the rest of
  622.         the locale environment variables.
  623.  
  624.     LC_CTYPE    In the absence of `LC_ALL', `LC_CTYPE' chooses the
  625.         character type locale. In the absence of both
  626.         `LC_ALL' and `LC_CTYPE', `LANG' chooses the
  627.         character type locale.
  628.  
  629.     LC_COLLATE    In the absence of `LC_ALL', `LC_COLLATE' chooses the
  630.         collation (sorting) locale. In the absence of both
  631.         `LC_ALL' and `LC_COLLATE', `LANG' chooses the
  632.         collation locale.
  633.  
  634.     LC_MONETARY In the absence of `LC_ALL', `LC_MONETARY' chooses the
  635.         monetary formatting locale. In the absence of both
  636.         `LC_ALL' and `LC_MONETARY', `LANG' chooses the
  637.         monetary formatting locale.
  638.  
  639.     LC_NUMERIC    In the absence of `LC_ALL', `LC_NUMERIC' chooses the
  640.         numeric format locale. In the absence of both
  641.         `LC_ALL' and `LC_NUMERIC', `LANG' chooses the
  642.         numeric format.
  643.  
  644.     LC_TIME    In the absence of `LC_ALL', `LC_TIME' chooses the date
  645.         and time formatting locale. In the absence of both
  646.         `LC_ALL' and `LC_TIME', `LANG' chooses the date and
  647.         time formatting locale.
  648.  
  649.     LANG    `LANG' is the "catch-all" locale environment variable.
  650.         If it is set, it is used as the last resort after
  651.         the overall `LC_ALL' and the category-specific
  652.         `LC_...'.
  653.  
  654. NOTES
  655.   Backward compatibility
  656.  
  657.     Versions of Perl prior to 5.004 mostly ignored locale
  658.     information, generally behaving as if something similar to the
  659.     `"C"' locale (see the section on "The setlocale function") was
  660.     always in force, even if the program environment suggested
  661.     otherwise. By default, Perl still behaves this way so as to
  662.     maintain backward compatibility. If you want a Perl application
  663.     to pay attention to locale information, you must use the `use
  664.     locale' pragma (see the section on "The use locale Pragma") to
  665.     instruct it to do so.
  666.  
  667.     Versions of Perl from 5.002 to 5.003 did use the `LC_CTYPE'
  668.     information if that was available, that is, `\w' did understand
  669.     what are the letters according to the locale environment
  670.     variables. The problem was that the user had no control over the
  671.     feature: if the C library supported locales, Perl used them.
  672.  
  673.   I18N:Collate obsolete
  674.  
  675.     In versions of Perl prior to 5.004 per-locale collation was
  676.     possible using the `I18N::Collate' library module. This module
  677.     is now mildly obsolete and should be avoided in new
  678.     applications. The `LC_COLLATE' functionality is now integrated
  679.     into the Perl core language: One can use locale-specific scalar
  680.     data completely normally with `use locale', so there is no
  681.     longer any need to juggle with the scalar references of
  682.     `I18N::Collate'.
  683.  
  684.   Sort speed and memory use impacts
  685.  
  686.     Comparing and sorting by locale is usually slower than the
  687.     default sorting; slow-downs of two to four times have been
  688.     observed. It will also consume more memory: once a Perl scalar
  689.     variable has participated in any string comparison or sorting
  690.     operation obeying the locale collation rules, it will take 3-15
  691.     times more memory than before. (The exact multiplier depends on
  692.     the string's contents, the operating system and the locale.)
  693.     These downsides are dictated more by the operating system's
  694.     implementation of the locale system than by Perl.
  695.  
  696.   write() and LC_NUMERIC
  697.  
  698.     Formats are the only part of Perl which unconditionally use
  699.     information from a program's locale; if a program's environment
  700.     specifies an LC_NUMERIC locale, it is always used to specify the
  701.     decimal point character in formatted output. Formatted output
  702.     cannot be controlled by `use locale' because the pragma is tied
  703.     to the block structure of the program, and, for historical
  704.     reasons, formats exist outside that block structure.
  705.  
  706.   Freely available locale definitions
  707.  
  708.     There is a large collection of locale definitions at
  709.     `ftp://dkuug.dk/i18n/WG15-collection'. You should be aware that
  710.     it is unsupported, and is not claimed to be fit for any purpose.
  711.     If your system allows the installation of arbitrary locales, you
  712.     may find the definitions useful as they are, or as a basis for
  713.     the development of your own locales.
  714.  
  715.   I18n and l10n
  716.  
  717.     "Internationalization" is often abbreviated as i18n because its
  718.     first and last letters are separated by eighteen others. (You
  719.     may guess why the internalin ... internaliti ... i18n tends to
  720.     get abbreviated.) In the same way, "localization" is often
  721.     abbreviated to l10n.
  722.  
  723.   An imperfect standard
  724.  
  725.     Internationalization, as defined in the C and POSIX standards,
  726.     can be criticized as incomplete, ungainly, and having too large
  727.     a granularity. (Locales apply to a whole process, when it would
  728.     arguably be more useful to have them apply to a single thread,
  729.     window group, or whatever.) They also have a tendency, like
  730.     standards groups, to divide the world into nations, when we all
  731.     know that the world can equally well be divided into bankers,
  732.     bikers, gamers, and so on. But, for now, it's the only standard
  733.     we've got. This may be construed as a bug.
  734.  
  735. BUGS
  736.   Broken systems
  737.  
  738.     In certain system environments the operating system's locale
  739.     support is broken and cannot be fixed or used by Perl. Such
  740.     deficiencies can and will result in mysterious hangs and/or Perl
  741.     core dumps when the `use locale' is in effect. When confronted
  742.     with such a system, please report in excruciating detail to
  743.     <perlbug@perl.com>, and complain to your vendor: maybe some bug
  744.     fixes exist for these problems in your operating system.
  745.     Sometimes such bug fixes are called an operating system upgrade.
  746.  
  747. SEE ALSO
  748.     the "isalnum" entry in the POSIX (3) manpage, the "isalpha"
  749.     entry in the POSIX (3) manpage, the "isdigit" entry in the POSIX
  750.     (3) manpage, the "isgraph" entry in the POSIX (3) manpage, the
  751.     "islower" entry in the POSIX (3) manpage, the "isprint" entry in
  752.     the POSIX (3) manpage, the "ispunct" entry in the POSIX (3)
  753.     manpage, the "isspace" entry in the POSIX (3) manpage, the
  754.     "isupper" entry in the POSIX (3) manpage, the "isxdigit" entry
  755.     in the POSIX (3) manpage, the "localeconv" entry in the POSIX
  756.     (3) manpage, the "setlocale" entry in the POSIX (3) manpage, the
  757.     "strcoll" entry in the POSIX (3) manpage, the "strftime" entry
  758.     in the POSIX (3) manpage, the "strtod" entry in the POSIX (3)
  759.     manpage, the "strxfrm" entry in the POSIX (3) manpage
  760.  
  761. HISTORY
  762.     Jarkko Hietaniemi's original perli18n.pod heavily hacked by
  763.     Dominic Dunlop, assisted by the perl5-porters.
  764.  
  765.     Last update: Wed Jan 22 11:04:58 EST 1997
  766.  
  767.