If you run perl and your script from the command-line interface in a DOS window or a Terminal in linux and print to STDIN then maybe the DOS or Terminal can't print those characters. I think that depends on the platform. However if you print something encoded as utf-8 to a file and read it with an editor or viewer that can read utf-8 encoded files you should see the characters you expect.

Can you post a simple script that demonstrates the problem so we can reproduce the problematic output?

script.pl

use Encode;
open (FILEHANDLE, '<specialchar.txt');
$file = <FILEHANDLE>;
$f = decode_utf8($file);
open (FF, '>print.txt');
print FF $f;

print.txt

� or �

ya you are right , the encoding for my notepad was ansi, therefore I was getting error.

Thanks..

Edited 4 Years Ago by PhoenixInsilico

Hi Phoenixlnsilico,
Your posted code could also be written like so,

my $input_file='specialchar.txt';

open my $fh2, '>:encoding(UTF-8)', 'print.txt'
  or die "can't open file: $!";    # file to write to
open my $fh, '<:encoding(UTF-8)', $input_file
  or die "can't open $input_file: $!";    # file to read from
binmode $fh,":encoding(UTF-8)";
while (<$fh>) {
    chomp;
    print {$fh2} $_,$/;
}
close $fh  or die "can't close file: $!";
close $fh2 or die "can't close file: $!";

That should give you what you want!
Hope this helps

This article has been dead for over six months. Start a new discussion instead.