Question

Bellow you can find the script that I am writing. My question is below the the script.

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;
}

The error that I am getting is:

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

Where the method "characters" is a $writer->characters("\n\n"); line which is from the writeXML() sub.

I understand that there is an undefined value passed to writeXML() but i don't understand why.

Could anybody please shed some light on this issue?

Thank you, sSmacKk

Was it helpful?

Solution

You declared my $writer inside the loop. The function is declared outside the loop. Therefore, the function has no access to the variable. Either pass it as an argument, or make it global (not recommended). Are you using strict and warnings?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top