0

I have created a Pl file for writing the content in txt file in perl, But when we open the file in Ubuntu system it shows the content in UTF8 and in windows system the content is shown in ANSI format and the text is broken in txt file.

I want to make the file UTF8 default open in Windows system, so that the characters should not be broken.

Below is the my code

my $filename = "123.txt";
my $content = "ब लोगों के लिए एक समा sum";
open FILE, ">", $filename;
print FILE $content;
close FILE;
exit;
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
sumit
  • 1

1 Answers1

0

See also Encode all standard output as utf-8 and Perl Unicode Cookbook: The Standard Preamble

use utf8;
use strict;
use warnings;
use open ':std', ':encoding(UTF-8)';

my $filename = "123.txt";
my $content = "ब लोगों के लिए एक समा sum";
open(my $FILE, ">", $filename)
   or die("can't open \"$filename\": $!\n");

print $FILE $content;
ikegami
  • 367,544
  • 15
  • 269
  • 518
palik
  • 2,425
  • 23
  • 31
  • Still on windows system in notepad++ it is opening in ANSI format, when we manually make the file UTF8 from options than only the content is visible in original format – sumit May 23 '17 at 12:56
  • It's a pity, I haven't windows to test it – palik May 23 '17 at 13:09
  • @sumit Which version of Windows are you using? This works perfectly well on Windows 10 for me. – Sinan Ünür May 23 '17 at 13:32
  • I am using Windows 7 – sumit May 23 '17 at 14:17
  • This code works perfectly fine on Windows or otherwise, as long as the source code is encoded using UTF-8. – ikegami May 23 '17 at 15:28
  • Re "*when we manually make the file UTF8 from options than only the content is visible in original format*", I'm don't know what you mean by "original format", but if you're saying you see `ब लोगों के लिए एक समा sum` when you tell Notepad++ the file is UTF-8, then the program worked. – ikegami May 23 '17 at 15:33