Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open an XML file in utf-16 encoding in Perl? [closed]

Tags:

perl

I would love to help with expressing that I've been here for a few weeks.

I'm trying to open an XML file in Perl in encoding utf-16.

I am able to create the file in utf-8 but it does not suit me and more than that it causes me problems.

like image 957
Eden ohana Avatar asked Nov 19 '25 12:11

Eden ohana


1 Answers

Please see if the following sample code complies with your requirements

use strict;
use warnings;

use Encode qw/encode decode/;

my $filename = 'utf16_1.txt';

open my($out), '>:encoding(UTF-16LE)', $filename
    or die "Couldn't open $filename";

my $string = 'Sample data';

print $out $string; 

close($out);

other variation

use strict;
use warnings;

use Encode qw/encode decode/;

my $filename = 'utf16_2.txt';

open my($out), '>:raw', $filename
    or die "Couldn't open $filename";

my $string = 'Sample data';

print $out encode("UTF-16LE", $string); 

close($out);
like image 83
Polar Bear Avatar answered Nov 22 '25 04:11

Polar Bear



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!