+#ifdef USE_PERLIO
+ {
+ /* Set PL_utf8locale to TRUE if using PerlIO _and_
+ any of the following are true:
+ - nl_langinfo(CODESET) contains /^utf-?8/i
+ - $ENV{LC_ALL} contains /^utf-?8/i
+ - $ENV{LC_CTYPE} contains /^utf-?8/i
+ - $ENV{LANG} contains /^utf-?8/i
+ The LC_ALL, LC_CTYPE, LANG obey the usual override
+ hierarchy of locale environment variables. (LANGUAGE
+ affects only LC_MESSAGES only under glibc.) (If present,
+ it overrides LC_MESSAGES for GNU gettext, and it also
+ can have more than one locale, separated by spaces,
+ in case you need to know.)
+ If PL_utf8locale and PL_unicode (set by -C or by $ENV{PERL_UNICODE})
+ are true, perl.c:S_parse_body() will turn on the PerlIO :utf8 layer
+ on STDIN, STDOUT, STDERR, _and_ the default open discipline.
+ */
+ bool utf8locale = FALSE;
+ char *codeset = NULL;
+#if defined(HAS_NL_LANGINFO) && defined(CODESET)
+ codeset = nl_langinfo(CODESET);
+#endif
+ if (codeset)
+ utf8locale = (foldEQ(codeset, STR_WITH_LEN("UTF-8"))
+ || foldEQ(codeset, STR_WITH_LEN("UTF8") ));
+#if defined(USE_LOCALE)
+ else { /* nl_langinfo(CODESET) is supposed to correctly
+ * interpret the locale environment variables,
+ * but just in case it fails, let's do this manually. */
+ if (lang)
+ utf8locale = (foldEQ(lang, STR_WITH_LEN("UTF-8"))
+ || foldEQ(lang, STR_WITH_LEN("UTF8") ));