xml - 使用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

最佳答案

您在循环内声明了my $writer。该函数在循环外部声明。因此,该函数无法访问该变量。要么将其作为参数传递,要么将其设为全局(不推荐)。您正在使用strictwarnings吗?

https://stackoverflow.com/questions/13273345/

相关文章:

twitter-bootstrap - 导航栏中的两行,一列跨越两行

file - 处理时output.println()中的错误

generics - Ada : invalid prefix in selected compon

c# - NHibernate错误:s

c# - 为什么这一行代码与上面两行不同?

compiler-errors - 为自定义MATLAB工具箱执行编译器脚本时出错

c# - 编译器不一致的可访问性错误,将嵌套类作为函数参数传递

objective-c - 错误Maddage : expected expression? [du

vb.net - 在VB.net故障中引发事件

compilation - javassist编译错误没有这样的类