Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Convert to utf8

by Dr Manhattan (Beadle)
on Mar 01, 2013 at 08:36 UTC ( [id://1021185]=perlquestion: print w/replies, xml ) Need Help??

Dr Manhattan has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

How can I convert a input text file to utf8 encoding?

Replies are listed 'Best First'.
Re: Convert to utf8
by tmharish (Friar) on Mar 01, 2013 at 08:38 UTC
    open(my $fh, "<:encoding(UTF-8)", "filename") || die "can't open UTF-8 encoded filename: $!";

    See doc for open for details.

Re: Convert to utf8
by 2teez (Vicar) on Mar 01, 2013 at 13:03 UTC

    Or you can use:

    • use open qw(:std :utf8);
    • open my $fh,'<', $input_file or die "can't open file:$!"; binmode $fh,":encoding(UTF-8)"; ...

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Convert to utf8
by Anonymous Monk on Mar 01, 2013 at 09:50 UTC
Re: Convert to utf8
by choroba (Cardinal) on Mar 01, 2013 at 18:04 UTC
    Open the file with its original encoding for reading, open a new file with utf-8 for writing. Then just read the input line by line and output each line to the new file:
    my $enc = 'cp1252'; open my $IN, "<:encoding($enc)", 'input.txt' or die $!; open my $OUT, '>:utf8', 'output.txt' or die $!; print $OUT $_ while <$IN>; close $OUT;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1021185]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 00:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found