in reply to quantum behavior in perl?
I believe one part of your code is actually closer to eval('...$var...') (note the single quotes). And, yes, this is likely a FAQ somewhere.
A reference to a Perl sub "closes over" any lexical variables that are in scope when the reference to the sub is taken. One reference to a simple, named sub is taken and stored in the package's symbol table ("stash") when the sub is compiled. This is how a simple, named sub can access lexical variables declared in their surrounding scope.
For the sake of efficiency, these references only "close over" lexical variables that the subroutine makes use of. Since your mysub() does not make any (direct) use of $var, $var isn't closed over. So when you call mysub(), the reconstruction of the scope to include any closed-over lexicals does not provide access to $var.
If your code really did say eval("...$var..."), then that would get compiled into the equivalent of eval("...".$var."...") so $var would have been directly used by mysub() and so would have been closed over.
Yes, this quirk is considered unfortunate and so there is a desire to "fix" it. But I'm aware of a plan for how it could be reasonably fixed, so I wouldn't hold my breath. You'll need to work around it by mentioning $var directly in mysub(), though you don't have to print it.
- tye
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: quantum behavior in perl? (cheap closures)
by b4swine (Pilgrim) on Aug 18, 2007 at 06:22 UTC |