mprogan365 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, this is my first post on SoPW and I am a novice PERL user so this may well be an easy fix, but I sure can't seem to figure it out. I appreciate any help.

My issue is this, I am writing a program that will read excel spreadsheet files and take specific cells in those spreadsheets and put them in a separate spreadsheet that I am writing in the program. I am using CPAN modules Spreadsheet::Read and Spreadsheet::WriteExcel. However when I run this program I get to the part where I am actually writing the excel output file and I run into this error:

"Can't call method 'write' on an undefined value at bluehillcomp.plx line 94, <STDIN> line 6."

and heres the code at that part:
<# Write 00 temperature data into file:
$read = 4+(($day-1)*58);
$temp = $xls->1{("B", $read)};
$row = $day + 1;
my $worksheet->write($row,$col,$temp); # Line 94 !
$day = $day + 1; >

So I thought this was an issue with me not declaring the variable "$worksheet", but looking at earlier code I think I did declare it:
< my $workbook = ();
my $worksheet = ();
# Create output file with user filename:
if ($outfile ne "d") {
my $workbook = Spreadsheet::WriteExcel->new($outfile);
my $worksheet = $workbook->add_worksheet();
print "What should we label the column containing the data?\n";
my $colname = <STDIN>;
chomp($colname);
$worksheet->write(0,"A",$colname);
} >

Any ideas?

Replies are listed 'Best First'.
Re: Issue with Spreadsheet::WriteExcel
by toolic (Bishop) on Aug 24, 2010 at 16:25 UTC
    My guess is that at least one of the 3 variables passed to the write function is undefined. Check each with print or defined:
    print "$row\n"; print "$col\n"; print "$temp\n"; if (defined $col) { print "col is defined: $col\n"; } else { print "col is not defined\n"; }

    That is a strange usage of my, by the way.

    Use code tags: Writeup Formatting Tips

      I think the message means $worksheet is undefined, not one of the parameters.

      And I think the real problem is because of all the extra my's in there.

      my $workbook; # No need for the = () my $worksheet; # No need for the = () # Create output file with user filename: if ($outfile ne "d") { $workbook = Spreadsheet::WriteExcel->new($outfile); # No my, or $wo +rkbook will go out of scope at the end of the if block $worksheet = $workbook->add_worksheet(); # same here print "What should we label the column containing the data?\n"; my $colname = <STDIN>; chomp($colname); $worksheet->write(0,"A",$colname); } # Other code here I assume # Write 00 temperature data into file: $read = 4+(($day-1)*58); $temp = $xls->1{("B", $read)}; $row = $day + 1; $worksheet->write($row,$col,$temp); # No my $day = $day + 1;

      Now $worksheet won't go out of scope after the if block, and the later code can still use it.

        Problem solved! thanks a lot folks
      Thanks for the advice. A couple of my variables were undefined because I declared them like so " my $row = ( );" which I guess PERL doesn't like.

      However, I still get the same error at the same line. So I tried to redefine "my $workbook = 0;" and "my $worksheet = 0;" since they were both set " = ( ); ". and it still doesn't work.

      Is there another way I should define "my $worksheet" (other than = 0) if it is doing this: ?
      < my $worksheet = $workbook->add_worksheet("data"); > and
      < $worksheet->write($row,$col,$temp); >

Re: Issue with Spreadsheet::WriteExcel
by BrimBorium (Friar) on Aug 24, 2010 at 19:48 UTC