After a few Super Searches, I figured out a few ways to set a file-scoped variable within said BEGIN block, and I'm wondering which is the Best Way, or if it's just a matter of programmer preference.
It's more a programmer preference. I try to avoid setting lexicals in BEGIN blocks. If you do set a lexical, make sure it is declared before the BEGIN block, because otherwise, the variable will be lost forever (unless used in a closure like your get_obj).
I find this odd, since a "use vars" variable works, but "our" doesn't. Aren't the two the same thing?
The vars pragma is not block-scoped, but file scoped (this is documented, use the link). If you use "our" inside the block, you can use the variable unqualified (qualified is $Package::variable) in the block, but not outside of it. If you use "use vars" inside the block, you can use the variable everywhere in the file. You could move the "our" statement to some place before the block, or repeat the "our" elsewhere. The same variable will be used (a second "our $foo" does not create a new $foo), as it is a normal global.
- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.
In reply to Re: Setting lexicals in a BEGIN block.
by Juerd
in thread Setting lexicals in a BEGIN block.
by mephit
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |