How can I output an UTF-8 encoded XML file with the end of a unix line from ActivePerl on Windows?

I am running ActivePerl 5.8.8 on WinXP. I would like to output the XML file as UTF-8 with the end of a UNIX line.

I looked at perldoc for binmode, but I'm not sure about the exact syntax (unless I bark the wrong tree). The following does not do this (sorry my Perl is a learning process!):

sub SaveFile
{
    my($FileName, $Contents) = @_;

    my $File = "SAVE";
    unless( open($File, ">:utf-8 :unix", $FileName) )
    {
        die("Cannot open $FileName");
    }
    print $File @$Contents;

    close($File);
}
+3
source share
2 answers

If $ Contents E still has no character \n, printnot add them for you. You have to do it yourself.

There are a couple of other things that can be problematic; here's how to fix them and why.

IO layers

utf8, :utf8, :utf-8. , -, `` > : utf8: unix ".

Open

open , 'my $fh' ; . , . , or .

, :

sub SaveFile
{
    my($FileName, $Contents) = @_;

    open my $File, ">:utf8:unix", $FileName
        or die "Cannot open $FileName";
    print $File map { "$_\n" } @$Contents;

    close($File);
}

map , , , .

!

+3

, - .

use strict;
use warnings;

sub save_file {
    my ($file_name, $content) = @_;
    open my $fh, ">", $file_name or die $!;
    binmode $fh, ':utf8 :unix';
    print $fh @$content;
    close $fh;
}

# For example.
save_file('foo.txt', [ map "$_\n", qw(foo bar baz)]);

open , undefined open. . .

+1

Source: https://habr.com/ru/post/1743502/


All Articles