2

This is my first attempt and I'm trying to get the basics down of using Excel::Writer::XLSX. I can't seem to get my script to even compile.

#!C:\Perl\bin
#excel::writer attempt
#allows IR-Serial-Parts tracking

use strict;
use warnings;
use POSIX qw(strftime);
use Excel::Writer::XLSX;


my $ref = strftime '%Y-%m-%d', locatime();      #create the datestamp
my $file = "$ref.xlsx";
my $workbook = Excel::Writer::XLSX->new('$file');   #declare it outside of if loop preventing original issue.
#if(-e $file){
#       my $workbook = Excel::Writer::XLSX->open('$file');  #open existing  excel file
#}
#else{
#       my $workbook = Excel::Writer::XLSX->new('$file');   #open new Excel   if the date on comp has changed
#}

$worksheet = $workbook->add_worksheet("Tracking");
$worksheet->write( 'A1', 'Hi Excel!');

I haven't even had to do anything yet I simply just wanted to test writing to a cell in an spreadsheet, and I can't even manage to do that. I feel like I'm making this to hard right now. Here's what the shell returns.

Global symbol "$worksheet" requires explicit package name at writexcel.pl         line 20.
Global symbol "$workbook" requires explicit package name at writexcel.pl line   20
.
Global symbol "$worksheet" requires explicit package name at writexcel.pl      line 21.
Execution of writexcel.pl aborted due to compilation errors.
Press any key to continue . . .

Now that the original question is answered.

Undefined subroutine &main::locatime called at writexcel.pl line 11.
Press any key to continue . . .

my new error. I think it may have something to do with strftime but I'm not quite sure.

4
  • 2
    Have you tried declaring $workbook outside of the if/else loops?
    – fugu
    Mar 25, 2015 at 13:41
  • That actually fixed my error, thanks for pointing out that obvious flaw I can't believe I didn't see it. Now however I'm getting an issue with my localtime().
    – nulltron
    Mar 25, 2015 at 13:48
  • 2
    It is localtime. There is l missing. Mar 25, 2015 at 13:52
  • @Hynek -Pichi- Vychodil thanks! I feel utterly scatter brained today
    – nulltron
    Mar 25, 2015 at 13:55

3 Answers 3

4
my $workbook = Excel::Writer::XLSX->new('$file');

This line will not make a file with the name stored in $file variable but file with exact name $file.

3
  • Thank you, I discovered this as well. Do you know of any way to name the file as the days date? Or do you think I could have just one .xlsx file and add worksheets with the localtime() as the name of the worksheet?
    – nulltron
    Mar 25, 2015 at 14:22
  • @HeadyGains - It's best to ask a new question if you have other problems unrelated to your original problem. If nothing else, it allows others with similar problems to easily find solutions
    – fugu
    Mar 25, 2015 at 14:26
  • @fugu my apologies will do.
    – nulltron
    Mar 25, 2015 at 14:26
3

Try declaring the $workbook variable outside of your loops. Also, as you're using strict you need to declare $worksheet using my:

use strict;
use warninigs;

my $workbook;

if(-e $file){
        $workbook = Excel::Writer::XLSX->open($file);  
}
else{
        $workbook = Excel::Writer::XLSX->new($file); 
}

my $worksheet = $workbook->add_worksheet('Tracking');
$worksheet->write( 'A1', 'Hi Excel!');
2
  • although if i don't declare my $worksheet = $workbook->add_worksheet('Tracking'); within my loop it causes add_worksheet() to be undef. I assume this is because the $workbook value doesn't exist outside the loop
    – nulltron
    Mar 25, 2015 at 14:24
  • 1
    You can do it easier by my $method = -e $file ? 'open' : 'new'; my $workbook = Excel::Writer::XLSX->$method($file); but unfortunatelly I didn't found open method in Excel::Writer::XLSX documentation. Mar 25, 2015 at 14:28
0

Declare $workbook and $worksheet with "my" outside the loop

Refer https://github.com/AarthiRT/Excel_Writer_XLSX

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.