Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Global Variable for Worksheet?

by RookiePerl (Initiate)
on Mar 21, 2012 at 15:33 UTC ( [id://960791]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to assign an Excel spreadsheet to a global variable, and I am finding this does not work. If I assign it to a local variable (using the "my" prefix), it works just fine. Am I missing something?

use Spreadsheet::Write; $h=Spreadsheet::Write->new( file => 'sample.xls', format => 'xls', sheet => 'test'); $h->addrow('Hello',{ content => 'there', font_weight => 'bold', font_color => 'red', font_face => 'Times New Roman', font_size => 10, font_style => 'italic', });

Replies are listed 'Best First'.
Re: Global Variable for Worksheet?
by moritz (Cardinal) on Mar 21, 2012 at 15:41 UTC

    The difference is that if the object is in a lexical variable, the DESTROY method is called when it goes out of scope (and no other refereeneces to it exist). This happens at the end of the program. It seems that Spreadsheet::Write uses that to actually write its output file.

    Other methods to trigger it is to remove all references to the object. For example you could add $h = undef at the end of your program.

    But do consider using lexical variables wherever possible, they usually make life easier.

Re: Global Variable for Worksheet?
by jmcnamara (Monsignor) on Mar 21, 2012 at 15:55 UTC

    moritz is correct about the object scope and destruction. You can force a manual destruction by calling the undocumented Spreadsheet::Write::close() method at the end of your program:

    ... $h->close();

    --
    John.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://960791]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-19 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found