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;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ