configuring STD* IO to use locale's encoding?

Discussion in 'Perl Misc' started by Ivan Shmakov, Aug 3, 2013.

  1. Ivan Shmakov

    Ivan Shmakov Guest

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

    --
    FSF associate member #7257
     
    Ivan Shmakov, Aug 3, 2013
    #1
    1. Advertising

  2. Ivan Shmakov <> writes:

    > 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
     
    Peter Makholm, Aug 4, 2013
    #2
    1. Advertising

  3. Ivan Shmakov

    Ivan Shmakov Guest

    >>>>> Peter Makholm <> writes:
    >>>>> Ivan Shmakov <> writes:


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


    >> Doesn't it warrant a pragma, BTW?


    > It seems that you could use


    > use open ':locale';


    BTW, where is this pragma documented?

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


    ... Which is perfectly sensible. Thanks!

    --
    FSF associate member #7257
     
    Ivan Shmakov, Aug 4, 2013
    #3
  4. Ivan Shmakov <> writes:

    > > use open ':locale';

    >
    > 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
     
    Peter Makholm, Aug 4, 2013
    #4
  5. Στις 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', 'ΚαλημέÏα')
     
    George Mpouras, Aug 4, 2013
    #5
  6. On 2013-08-06 02:28, Shmuel Metz <> wrote:
    > In <>, on 08/04/2013
    > at 07:56 AM, Ivan Shmakov <> said:
    >> BTW, where is this pragma documented?

    >
    > Pragmata.


    ???

    hp


    --
    _ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
    |_|_) | Sysadmin WSR | Man feilt solange an seinen Text um, bis
    | | | | die Satzbestandteile des Satzes nicht mehr
    __/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
     
    Peter J. Holzer, Aug 7, 2013
    #6
  7. Ivan Shmakov

    Ivan Shmakov Guest

    >>>>> Peter J Holzer <> writes:
    >>>>> On 2013-08-06 02:28, Shmuel Metz wrote:
    >>>>> Ivan Shmakov <> said:


    >>> BTW, where is this pragma documented?


    >> Pragmata.


    > ???


    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.

    --
    FSF associate member #7257
     
    Ivan Shmakov, Aug 7, 2013
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maurice Hulsman
    Replies:
    1
    Views:
    1,916
    Guus Bosman
    Jul 25, 2004
  2. Replies:
    4
    Views:
    1,047
  3. Gabriel Genellina
    Replies:
    0
    Views:
    737
    Gabriel Genellina
    Feb 18, 2009
  4. zade
    Replies:
    1
    Views:
    641
    James Kanze
    Mar 5, 2010
  5. Sibylle Koczian
    Replies:
    2
    Views:
    1,156
    Sibylle Koczian
    Nov 20, 2010
Loading...

Share This Page