2012-06-19 73 views
0

我之前問過這個問題,但沒有得到滿意的答案,因爲我沒有包含正確的示例。從多個perl腳本打印一個xml輸出,打印不同的xml輸出

我有多個使用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; 

有沒有更好的方法來做到這一點?

+0

你可以舉一個如何調用這些腳本的例子嗎? – jimtut

+0

你好@jimtut我已經添加到頂部,我正在嘗試做什麼。 – Maxyie

回答

0

如果腳本1和腳本2無法轉換爲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); 

在回調系統調用之前,您可能需要「打印」,但先嚐試這種方式。但是,它與您的方法沒有多大區別,只是省略了保存每個腳本輸出的需要。