Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.93k stars 549 forks source link

[feature] lexically-scoped setlocale() #18476

Open khwilliamson opened 3 years ago

khwilliamson commented 3 years ago

@pali has requested a lexically scoped setlocale() that would be in effect only for the scope in which it occurs.

This sounds useful to me, and not that hard to implement.

What do you think, and what should the API be?

Leont commented 3 years ago

Would it truly be lexical, or would it be dynamically scoped?

khwilliamson commented 3 years ago

On 1/14/21 12:54 PM, Leon Timmermans wrote:

Would it truly be lexical, or would it be dynamically scoped?

I think it would be best to make it fully lexical

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Perl/perl5/issues/18476#issuecomment-760439582, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA2DH77WMLFSKANOKVZZE3SZ5DYRANCNFSM4WB2V6LA.

pali commented 3 years ago

What about following API?

{
    use locale 'en_US';
    @sorted = sort @array;
    $upper = uc $string;
}

Only code inside of the block would use locale changed to 'en_US'.

And true lexical implementation would be the best. There are POSIX _l functions for it (e.g. toupper_l()) and also there is CORE module Unicode::Collate::Locale. But you know.

khwilliamson commented 3 years ago

On 1/17/21 7:32 AM, pali wrote:

What about following API?

{ use locale'en_US'; @sorted =sort @array; $upper =uc $string; }

I would want to make it easy for someone to specify the locale at runtime.

Perhaps something like use locale 'lexset'; setlocale(LC_ALL, $ARGV[1]); @sorted = sort @array; $upper = uc $string;

Only code inside of the block would use locale changed to 'en_US'.

And true lexical implementation would be the best. There are POSIX |_l| functions for it (e.g. |toupper_l()|) and also there is CORE module Unicode::Collate::Locale https://metacpan.org/pod/Unicode::Collate::Locale. But you know.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Perl/perl5/issues/18476#issuecomment-761821789, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA2DH7OKFI2YBEZWKXORD3S2LYGZANCNFSM4WB2V6LA.

pali commented 3 years ago

Perhaps something like

use locale 'lexset';
setlocale(LC_ALL, $ARGV[1]);
@sorted = sort @array;
$upper = uc $string;

This has problems with begin blocks. use locale 'lexset'; is same as: BEGIN { require locale; locale->import('lexset') }.

So there is problem how would be following code handled?

use locale 'lexset';
setlocale(LC_ALL, $ARGV[1]);
BEGIN { @sorted = sort @array };

According to which rules would be @array sorted? At the time of calling sort function, lexset is already set, but $ARGV[1] was not initialized yet. I think this would lead to another issues of understanding how and when to call or set functions...

With use locale 'en_US'; solution there is not such issue.