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

I'm trying to use spreadsheet using global variables from config file. I get the following error when I run main.pl. Instead if I use 'my <variable name>' things work fine.

The following code gives me "Use of unitialized value in numeric eq (==) at /usr/local/share/perl/5.10.1/Spreadsheet/WriteExcel/Workbook.pm line 1545 during global destruction. (in cleanup) Can't call method "_prepare_image" on unblessed reference at usr/local/share/perl/5.10.1/Spreadsheet/WriteExcel/Workbook.pm line 1546 during global destruction

#!/usr/local/bin/perl #config.pl #Contains all the configuration details use strict; use vars qw ( $workbook $worksheet ); $workbook = 0x0; $worksheet = 0x0; 1;
#!/usr/bin/perl -w #main.pl use strict; use Spreadsheet::WriteExcel; BEGIN {require "config.pl"}; # Create a new workbook called simple.xls and add a worksheet. $workbook = Spreadsheet::WriteExcel->new('simple.xls'); $worksheet = $workbook->add_worksheet(); # The general syntax is write($row, $column, $token). # Note that row and column are zero indexed. # Write some text. $worksheet->write(0, 0, 'Hi Excel!');

For my requirement I should be able to use the vairables in config.pl

Replies are listed 'Best First'.
Re: Global variables issue in spreadsheets
by ww (Archbishop) on Jan 04, 2011 at 23:52 UTC

    Using an earlier Perl

    perl -v This is perl, v5.8.8 built for MSWin32-x86-multi-thread (with 33 registered patches ... Binary build 819 [267479] provided by ActiveState http://www.ActiveSta +te.com Built Aug 29 2006 12:42:41)
    ...and

    Spreadsheet::WriteExcel v 2.25

    ...and specifying a location (not INC) for config.pl, I see a rather different failure; namely:

     (in cleanup) Can't call method "isa" on an undefined value at C:/Perl/site/lib/Spreadsheet/WriteExcel/Workbook.pm line 1366 during global destruction.

    ...and a zero-byte simple.xls created in CWD.

    Version differences may account for the diff... but for a stab in the dark (because no other suggestions have been offered yet), you might want to tell us WHERE config.pl is stored and what version of S::WE you're using. You might also find it worthwhile to check for bug reports on your version of S::WE.

      I used perl 5.10.1 built for i486-linux-gnu-thread-multi. Ubuntu OS. The config.pl and the main.pl are in the same folder.

      Tried the same on a pc having perl 5.12.2 built for x86_64-linux-thread-multi and same result

        • Does your require line in main.pl use the full path to config.pl? I may be wrong, but I believe it should.
        • From what directory do you execute main.pl?
        • Please clarify your statement about using my <$var names>. Do you mean you're not using the config.pl and declaring my $workbook and my $worksheet in main.pl or something else?
        • Since the code you've shown matches what's in the doc for my copy of S::WE (with the minor exception -- at line 15 -- of providing "0" values directly instead of by assigning "0" values to $col and $row), the possibilities include:
          1. Borked installs for both copies (you do have two, right?) of S:WE (a longshot, IMO)
          2. Something you haven't shown/told us (see my prior questions)
        • You might also want to look at lines around 1545-1546 in Workbook.pm to see if that section of the code provides any clues.

        Update: And why, except for the sake of a learning experience, are you using a separate config.pl merely to declare two stock variables? That seems to me more than just a few degrees off vertical; an unnecessary bit of complexity or mis-direction.

Re: Global variables issue in spreadsheets
by Mr. Muskrat (Canon) on Jan 05, 2011 at 22:05 UTC

    When your workbook variable is not declared with my, you need to explicitly close the workbook.

    $workbook->close();

    Spreadsheet::WriteExcel says:

    An explicit close() is required if the file must be closed prior to performing some external action on it such as copying it, reading its size or attaching it to an email. In addition, close() may be required to prevent perl's garbage collector from disposing of the Workbook, Worksheet and Format objects in the wrong order. Situations where this can occur are:

    • If my() was not used to declare the scope of a workbook variable created using new().
    • If the new(), add_worksheet() or add_format() methods are called in subroutines.