BEGIN block and failure to initialize constantsby temporal (Pilgrim)
|on Sep 17, 2012 at 21:06 UTC||Need Help??|
temporal has asked for the
wisdom of the Perl Monks concerning the following question:
I have a Perl module that I'd like to link a config file to. This was slightly trickier than I initially anticipated since I'd like to keep it as portable as possible such that no matter where the module is located it always looks for its config file in the same directory. Piece of cake if I'm calling the module directly or from scripts in the same path, but when I use it in a script located in a different directory it looks for the module's config file in the running script's directory rather than the module's directory.
I'd rather not fiddle with @INC just for a config file. So I came up with something like this:
This code warns me that the constant is uninitialized in the print statement. Hm. So things must be happening in a different order than I thought at compile-time, right?
So my next stab at it:
And now I'm getting what I want: a constant that contains the absolute path of my config file for later use with Config::General and also drops it in @EXPORT_OK in case some calling script ever needs to see it.
So I'm wondering what's really going on under the hood here. Looking at the docs, constants get set at compile-time and I guess since I declare the BEGIN block first it beats the constant declaration to the punch.
Anyway, just curious if this is a classic case of "I'm doing it wrong" or maybe get some tips/suggestions for an intelligent way to implement this.