Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello ,
How to output , save (in .txt files) Text in specific
encoding type like UTF-8 , UTF-16 etc…
anything rather than binmode(); for output ???
Thanks in advance for your answers

Replies are listed 'Best First'.
Re: Saving Encoded Text
by graff (Chancellor) on Mar 30, 2005 at 16:03 UTC
    anything rather than binmode(); for output ???

    Er, no. You got a problem with that?

    There is at least one syntactic variant on the same basic activity (which is really a matter of specifying a PerlIO layer for output to a file handle), using the "open" statement in perl 5.8.x:

    open( $fh, $mode, $name ) # where $mode can be something like: # ">:utf8", ">:encoding(UTF-16BE)", ">:encoding(cp1252)", and so on
    Refer to "perldoc Encode" for a way to get the list of available encodings.

    Also, if you're dealing with just utf8 encoding, there's the "-C" option flag (see "perldoc perlrun" in 5.8.x). You really only need to specify the ":encoding(...)" on a file handle for non-utf8 (non-ascii) character data, because the various choices with "-C" cover all scenarios for utf8 i/o.

    update: From the way you stated your question, I don't know if you understood how "binmode()" is used to control character encoding on file handles. It's exactly equivalent to the "mode" usage in the "open" statement, minus the angle brackets -- e.g.:  binmode( $fh, ":encoding(UTF-16BE)" );

Re: Saving Encoded Text
by dmorelli (Scribe) on Mar 30, 2005 at 15:02 UTC
    I did some quick searching and found a lot of information on encoding in perldoc perlunicode

    I don't really understand much of this myself yet, but it looks like with UTF-16 you need to be mindful of the endianness.

    Apologies for the RTFM-like response. ;)

Re: Saving Encoded Text
by tlm (Prior) on Mar 30, 2005 at 15:03 UTC

    Check out PerlIO.

    the lowliest monk