Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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?

Comment on Convert to utf8
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 Anonymous Monk on Mar 01, 2013 at 09:50 UTC
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 choroba (Canon) 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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2015-07-07 06:06 GMT
Find Nodes?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...

    Results (87 votes), past polls