Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: variable scopes in Excel::Writer::XLSX

by johnrcomeau (Novice)
on May 25, 2013 at 14:09 UTC ( [id://1035263]=note: print w/replies, xml ) Need Help??


in reply to Re: variable scopes in Excel::Writer::XLSX
in thread variable scopes in Excel::Writer::XLSX

People seem to be misunderstanding the point of my question. If you look at this code, you can see that there is a single line that may optionally be executed: the line that assigns $book to the static variable $closure_workbook. It's not worth explaining why, but I want to store $book in a static variable. And I see no reason that the one extra assignment should cause an error.
use strict; use Excel::Writer::XLSX; &func(@ARGV); { my $closure_workbook; sub func { my $file = 'perl.xlsx'; my $book = Excel::Writer::XLSX->new($file); if (@_) { $closure_workbook = $book; } my $worksheet = $book->add_worksheet(); $worksheet->write(0, 0, 'Hi Excel!'); } }

Replies are listed 'Best First'.
Re^3: variable scopes in Excel::Writer::XLSX
by AnomalousMonk (Archbishop) on May 25, 2013 at 19:05 UTC
    If you look at this code...

    By "this code", I assume you mean the code posted at the end of the Re^2: variable scopes in Excel::Writer::XLSX node, and that this code is all that is needed to produce the error you describe. (I'm not in a position right now to run the code; all I can do is literally 'look' at it.)

    ... I see no reason that the one extra assignment should cause an error.

    Nor do I. The  $closure_workbook variable is never touched except to assign it a value in certain circumstances. It is never otherwise accessed within  func() and no other function than  func() can possibly access this lexical variable (unless there's something you're not showing us). The next step is to eliminate the
        if (@_) {
            $closure_workbook = $book;
        }
    block of code (leaving the  my $closure_workbook; statement just for grins) and see if the error still occurs. My bet is it will. This means the root cause of your problem is entirely unconnected to anything you're doing with this 'closure' variable.

    For a while, I had my suspicions about the  &func(@ARGV); ampersand invocation of  func(), but I can't see this has any relevance.

    Update: Oops... Should have refreshed the thread before posting. I see from Re^4: variable scopes in Excel::Writer::XLSX that the problem is resolved. At any rate, the problem had, indeed, nothing to do with anything that was happening with the 'closure' variable.

Re^3: variable scopes in Excel::Writer::XLSX
by Anonymous Monk on May 25, 2013 at 15:01 UTC

    People seem to be misunderstanding the point of my question..
    Because what you intended is not clear. Closures in perl are not written like that. And you must have reason for what you are doing. You are calling a variable that is outside a function within the function, to assign to it an object. To what end? Oh I just want to call it. Is that not funny? Please take a look at the various suggestions already given. The solutions you wanted might be somewhere in the middle, if not already given.

      I misused the word 'closure' in my original post. I wanted to assign the workbook to a static variable. I believe that the structure I used is a commonly idiom in Perl for making a variable static.

      I never figured out why the error was occurring, but it disappeared when I explicitly called $workbook->close instead of letting the destructor handle it when the program exits. The workbook was also saved correctly.

        I misused the word 'closure' in my original post. I wanted to assign the workbook to a static variable. I believe that the structure I used is a commonly idiom in Perl for making a variable static.

        Yes that is old idiom, and yes, it is called a closure (Closure on Closures) because func closes over $closure_workbook, a lexical variable declared in outer scope.

        The new idiom is state / use feature qw' state ';

        OTOH, its not really static without this  unless(  $closure_workbook ) { if( @_ ) ... }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-18 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found