configuring STD* IO to use locale's encoding?

I

Ivan Shmakov

Do I understand it correctly that a proper way to configure the
"standard IO" filehandles to use the encoding specified by the
locale currently in effect would be like:

## NB: use locale; affects collation, etc., not IO handles
use Encode::Locale; # for :encoding(locale)

binmode ($_, ":encoding(locale)")
foreach (\*STDERR, \*STDIN, \*STDOUT);

Doesn't it warrant a pragma, BTW?

TIA.

PS. FWIW, print ("\N{U+0432}\N{U+0444}"); now appears to work correctly
with at least two different encodings. And so does warn ().
Also to note is that there seem to be configurations which
result in non-English, -- and non-ASCII, -- $! texts...
 
P

Peter Makholm

Ivan Shmakov said:
binmode ($_, ":encoding(locale)")
foreach (\*STDERR, \*STDIN, \*STDOUT);

Doesn't it warrant a pragma, BTW?

It seems that you could use

use open ':locale';

but it would make all ':encoding(locale)' the default for all
filehandles and not just the STD* ones.

//Makholm
 
P

Peter Makholm

Ivan Shmakov said:
BTW, where is this pragma documented?

As all others pragmas you can find the documentation with 'perldoc
open'. The documentation is also available at

https://metacpan.org/module/open

On perldoc.perl.org the search interface prefers the entry from the
perlfunc manual page. You can view all results and find the
documentation under 'Core modules' or just look for 'Pragmas' in the
sidebar.

Both the open and the binmode entries in the perlfun manpage refers to
the open pragma. In the binmode entry it is quite clearly state:

The :bytes , :crlf , :utf8 , and any other directives of the form
:... , are called I/O layers. The open pragma can be used to establish
default I/O layers. See open.

But the reference in the open entry is a bit more obscure and could
probably use a rewording.

//Makholm
 
G

George Mpouras

Στις 3/8/2013 20:48, ο/η Ivan Shmakov έγÏαψε:
Do I understand it correctly that a proper way to configure the
"standard IO" filehandles to use the encoding specified by the
locale currently in effect would be like:

The following 3 pieces of code are equivalent, make your choises
Assuming that your Perl script is saved as UTF8 file.

# code1
print 'ΚαλημέÏα';

# code2
use feature qw/say/;
use Encode;
use utf8;
binmode STDOUT, ':utf8';
print 'ΚαλημέÏα';

# code2
use feature qw/say/;
use Encode;
use utf8;
print Encode::encode('utf8', 'ΚαλημέÏα')
 
I

Ivan Shmakov


My guess is that "pragmata" is a plural for "pragma."
(Cf. "schemata.")

But should I be interested in /pragmata/ documentation,
I wouldn't have used singulars ("is", "this") in the sentence.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top