Perl CGI::Application writing to a document file

108 Views Asked by At

I am trying to generate a file in perl 5.8 which reads from an xml file( that has utf-8 encoding) and generates a word document. The code works fine when I run it at the command line.

Same code when run from the CGI::Application it ignores unicode characters and misses the annotations.

Here's the code:

$template->process("$dir/$input",$vars, \$content )
                                || return "Template Error: " . $template->error();


                    open PBH, ">:encoding(UTF-8)", $doc_file or die $!;
                    print PBH $content;
                    close PBH;

I tried setting binmode but the problem seems to be with CGI::Application ignoring utf encoding while writing to the document file.

I have the below code when run as a perl script works fine generating a word document with proper french accents.

Perl Script

#!/usr/local/bin/perl 
use XYZ; 
my $x = new XYZ(); 
$x->gen_pb(); 
1;

And the module XYZ has code like below:

    package XYZ; 
   sub gen_pb { 
    $template->process("$dir/$input",$vars, \$content, 
       {binmode => ':encod +ing(utf8)'} ) || return "Template Error: " .    $template->error();      unlink($doc_file) if(-e $doc_file);  
    open(FHPB,">$doc_file"); 
    binmode( FHPB, ":encoding(UTF-8)" ); 
    print FHPB $content; close FHPB; 
}

But the same module when called from a CGI generates the word document but without the french accents.

Perl CGI Program

#!/usr/local/bin/perl 
use CGI::Application; 
use XYZ; 
.... 
my $x = new XYZ(); 
$x->gen_pb(); 
0

There are 0 best solutions below