The problem with your original version is that the use constant executes at compile time, but the regular expression executes at run time - i.e. after everything has been compiled - so it's setting the $CONF_PATH variable too late. To see the difference between compile time and run time, run this:
print "World\n"; BEGIN { print "Hello " };
And the use keyword (plus its friend no) runs conceptually in a BEGIN block.
A do { ... } block provides a neat solution:
package mylib; use constant { MODULE_PATH => __FILE__, CONF_PATH => do { $_ = __FILE__; s/pm$/conf/i; $_ }, }; print CONF_PATH;
If your perl is fairly recent, you can do away with do and use /r.
package mylib; use constant { MODULE_PATH => __FILE__, CONF_PATH => (__FILE__ =~ s/pm$/conf/ir), }; print CONF_PATH;
In reply to Re: BEGIN block and failure to initialize constants
by tobyink
in thread BEGIN block and failure to initialize constants
by temporal
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |