Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Convert to utf8

by Dr Manhattan (Beadle)
on Mar 01, 2013 at 08:36 UTC ( #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 (Priest) 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 (Bishop) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1021185]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2018-01-20 07:30 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (226 votes). Check out past polls.