2012-11-07 64 views
0

貝婁可以找到我正在寫的腳本。我的問題在腳本下面。使用Perl寫入xml時遇到麻煩

use XML::Writer; 
use IO; 

@sortedPLATFORMS = qw(win32_x86 win64_x64 linux_x86 linux_x64 solaris_sparc solaris_sparcv9 aix_rs6000 aix_rs6000_64 hpux_pa-risc hpux_ia64); 
@STARTS = qw(wdf_22_00 wdf_23_00 wdf_00_00 wdf_01_00 wdf_02_00); 

my @waitFors; 

my $thisPlatform; 
my $thisMachine; 
my $thisTask; 
my $thisBuild; 
my $thisCMD; 
my $thisWaitFor; 


foreach my $start(@STARTS) 
{ 
    my $jobFile = "jobs/$start.txt"; 
    my $doc = new IO::File(">$start.xml", 'w'); 
    my $writer = new XML::Writer(OUTPUT => $doc) or die "Cannot open file"; 
    $writer->xmlDecl("UTF-8"); #XML declaration 
    $writer->startTag("$start"); 

    if(open(JOB_FILE,$jobFile)) 
    { 
    while(<JOB_FILE>) 
    { 
     chomp; 
     s-^\s+$--; 
     next unless($_); 
     next if(/^\;/); 
     next if(/\[config\]/); 
     next if(/event_dir\s+\=\s+(.+?)$/); 

     if(/\[(.+?)\]/) 
     { 
      getInfos(); 
     } 
     elsif(/^\s+waitfor\s+\=\s+(.+?)$/) 
     { 
      $thisWaitFor = $1; 
      push(@waitFors, "$thisWaitFor"); 
     } 
     elsif(/^\s+command\s+\=\s+(.+?)$/) 
     { 
      $thisCMD = $1; 
      writeXML(); 
      @waitFors =(); 
     }  
    } 
} 
else 
{ 
    print "something is wrong"; 
} 

$writer->endTag(); 
    $writer->end(); 
    $doc->close(); 
close JOB_FILE; 
} 

,我得到的錯誤是:

Can't call method "characters" on an undefined value at createMachineList.pl 
line 96, <JOB_FILE> line 13. 

凡方法"characters"$writer->characters("\n\n");線是從writeXML()子。

我知道有一個未定義的值傳遞給writeXML()但我不明白爲什麼。

請問任何人都可以解釋一下這個問題?

謝謝 sSmacKk

回答

4

你宣佈my $writer內循環。該函數在循環外聲明。因此,該函數無法訪問該變量。將它作爲參數傳遞,或者使其成爲全局的(不推薦)。您是否在使用strictwarnings

+0

感謝您指出這一點...有時您需要退後一步,重新看看 – sSmacKk