Error - Perl execution failed. Wide character in syswrite

1.4k views Asked by At

I have a perl script which runs a bunch of commands redirects both stdin and stderr into a text file. After the scripts finish, I display the contents of the file to a webpage that invoked it, using cgi.

At least one of the lines wrote to the file contains wide characters, such as:

Saving to: ârabbitmq-server-3.3.5-1.noarch.rpmâ

I've tried to html escape the file when outputting to the webpage, but to no avail:

use CGI;
.
.
.
    my $filename = 'tempoutput.txt';
    open(my $fh, '<:encoding(UTF-8)', $filename)
        or die "Could not open file '$filename' $!";
    while (my $row = <$fh>) {
        chomp $row;
        print $cgi->p("");
        print $cgi->escapeHTML("$row");
    }

I've also tried changing CGI's param which I found in a google, but it also didn't work.

use CGI '-utf8';

Any ideas on what I need to do to make this work?

2

There are 2 answers

0
harvey On

Turn on support for UTF8 printing with binmode STDOUT, ":utf8";.

0
James Oravec On

binmode STDOUT, ":utf8"; didn't work for me directly, but it pointed me in the right direction. I used the binmode on the file handle and that solved the issue:

open(my $fh, '<:encoding(UTF-8)', $filename)
    or die "Could not open file '$filename' $!";
binmode($fh);