0

我之前曾问过这个问题,但没有得到满意的答案,因为我没有包含要查看的正确示例。

我有多个使用 xsl 打印这些脚本的 xml 输出的脚本。我想编写一个调用这些脚本并结合输出和打印一个输出的主脚本。有人可以帮我吗?

#Example Script 1

use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
use Getopt::Long;

my $output = '';
my $debug = 0;
my $path;
GetOptions('path=s' => \$path,'output=s' => \$output, 'debug=i' => \$d
+ebug);

if($output eq ''){
    die ("parameter --output=s is missing");
}     
open my $xmloutput, ">", $outputFile or die "can not open $outputFile 
+";
 print $xmloutput "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-s
+tylesheet type=\"text/xsl\" href=\"book.xsl\"?>\n<Books>\n";

my $parser = new XML::Simple;
my $data = $parser->XMLin("$path");
print $xmloutput " <bookDetails> \n";
print $xmloutput "  <bookName>$data</bookName> \n";
print $xmloutput " </bookDetails> \n";
print $xmloutput " </Books> \n";
close $xmloutput;

例 2

EXAMPLE 2
use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
use Getopt::Long;

my $output = '';
my $debug = 0;
my $path;
GetOptions('path=s' => \$path,'output=s' => \$output, 'debug=i' => \$d
+ebug);

if($output eq ''){
    die ("parameter --output=s is missing");
}     
open my $xmloutput, ">", $outputFile or die "can not open $outputFile 
+";
 print $xmloutput "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-s
+tylesheet type=\"text/xsl\" href=\"Piano.xsl\"?>\n<Piano>\n";

my $parser = new XML::Simple;
my $data = $parser->XMLin("$path");
print $xmloutput " <PianoDetails> \n";
print $xmloutput "  <PianoName>$data</PianoName> \n";
print $xmloutput " </PianoDetails> \n";
print $xmloutput " </Piano> \n";
close $xmloutput;

这就是我正在尝试的。

  my $output = '';


   my $example1 = `perl script1.pl --path=c:/cygwin/home/username/directory --debug=1`;
    my $example2 = `perl script2.pl --path=c:/cygwin/home/username/directory --debug=1`;

 open my $finaloutput, ">" $output or die "cant open the file";
  print $finaloutput "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet     
          type=\"text/xsl\" href=\"final.xsl\"?>\n<OutputDestination>\n";

   my @attachscript  = ($example1, $example2);

   for my $attached (@attachscript) {
     print $finaloutput "<outputgoes_here_from_script> $attached   
                         </outputgoes_here_from_script>
    }

     PRINT $finaloutput "</OutputDestination>"
     close $finaloutput;

有没有更好的方法来做到这一点?

4

1 回答 1

0

如果 script1 和 script2 不能转换为 Perl 模块并在包装器中直接调用(作为函数)(或者甚至只是重写为单个新脚本),我会考虑在包装器中这样做:

open (STDOUT, ">$output") or die "Can't open STDOUT: $!";
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet type=\"text/xsl\" href=\"final.xsl\"?>\n<OutputDestination>\n";
print "<outputgoes_here_from_script>";
`perl script1.pl --path=c:/cygwin/home/username/directory --debug=1`;
print "</outputgoes_here_from_script>";
print "<outputgoes_here_from_script>";
`perl script2.pl --path=c:/cygwin/home/username/directory --debug=1`;
print "</outputgoes_here_from_script>";
print "</OutputDestination>";
close(STDOUT);

您可能需要在反引号系统调用之前进行“打印”,但请先尝试这种方式。但是,它与您的方法没有太大区别,只是跳过了保存每个脚本的输出的需要。

于 2012-06-20T17:32:03.073 回答