1

I've got a perl CGI script running on an Apache server. The script is, among other things, supposed to display some XML that is generated from input. Using the XML::Writer module, I've created a string scalar containing the right XML, but I can't seem to figure out how to post it back to the browser. Currently my function looks like this:

sub display_xml {
    # input variables here
    my $output = '';
    my $writer = XML::Writer->new(
                   OUTPUT=>\$output,
                   DATA_MODE => 1,
                   DATA_INDENT =>1
    };
    $writer->xmlDecl('UTF-8');
    $writer->startTag('response');
    #omitted for brevity
    $writer->endTag('response');
    $writer->end();
}

Can anyone help me with this? Printing $output to STDOUT doesn't work, and I didn't see any functions in CGI.pm to do this (print $cgi->header('text/xml'); works, but I can't figure out how to print the body).

4

2 に答える 2

2

実際にCGIを使わなくてもできます。

print "Content-Type: text/xml\r\n";   # header tells client you send XML
print "\r\n";                         # empty line is required between headers
                                      #   and body
print $output;                        # the body: XML
于 2011-08-10T17:08:30.020 に答える
1

CGI は比較的単純なプロトコルで、スクリプトの標準出力をクライアント マシンに送信します。必要なのは、出力の先頭にヘッダーを配置することだけです。これは私のために働く:

use CGI qw(:standard);
use XML::Writer;

my $output = '';
my $writer = XML::Writer->new(
    OUTPUT      => \$output,
    DATA_MODE   => 1,
    DATA_INDENT => 1
);
$writer->xmlDecl('UTF-8');
$writer->startTag('response');

#omitted for brevity
$writer->endTag('response');
$writer->end();

print header('text/xml'), $output;

また、最初にシバン行を配置し、スクリプトを実行可能にして、サーバーが実行方法を認識していることを確認してください。

#!perl
于 2011-08-11T03:59:19.453 に答える