Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: A locale independent sprintf?

by salva (Abbot)
on Jul 29, 2020 at 08:05 UTC ( #11119943=note: print w/replies, xml ) Need Help??


in reply to A locale independent sprintf?

But the locale settings should not affect sprintf unless you explicitly activate it with a use locale pragma.

If you have it activated, you can disable it just for the sprintf call as follows:

my $foo = do { no locale; sprintf ... };

Replies are listed 'Best First'.
Re^2: A locale independent sprintf?
by swl (Priest) on Jul 29, 2020 at 08:48 UTC

    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.

      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).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11119943]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2020-10-28 02:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (259 votes). Check out past polls.

    Notices?