http://qs321.pair.com?node_id=11119947


in reply to Re: A locale independent sprintf?
in thread A locale independent sprintf?

Thanks. I had considered overriding the locale in a sub using setlocale, but the setlocale docs indicate that it affects things globally and is not thread safe on some systems.

However, I had not considered using no locale. I'll have to give it a test.

As for activating the locale, there is no locale-setting code in my codebase. In the original case it was caused by the Gtk2 module when it was loaded. Having now side-stepped it for Gtk2, there is always the risk it will manifest if I use some other library. I guess I will have to audit such libs before adding them as dependencies.

Replies are listed 'Best First'.
Re^3: A locale independent sprintf?
by salva (Canon) on Jul 29, 2020 at 08:58 UTC
    The no locale pragma does not change the locale, it just deactivates it.

    Actually, if you look at the code, you will see it disables the internal hint that tells perl builtins to honor the locale configuration. I am sure doing that is completely thread safe.

      Very handy. When applied at the package scope it will also allow code to avoid locale based effects across subs (i.e. more than just sprintf).

        Unless you are putting your packages in blocks, locale does not exist in package scope — it is a lexical pragma, just like strict and warnings. So no locale at file scope will inhibit locale effects for everything after it in the same file, unless countermanded with use locale.