in reply to Define constant in loop
Now that everyone has told you why it doesn't work, here's one solution to getting it to work:
The idea is to use $1 as a bareword, and $2 similarly. Note that this doesn't work so hot if $2 could be a string (which it isn't in your case). Unfortunately, it makes all your constants not easily accessable: you'll need to call them like MEMINUSEWAR() (with the parens) or similar. Barewords won't work.if ($_ =~/^(\w+);(\d+);$/ && $_ !~/^\#/) { print "DEBUG: use constant $1 => $2;\n"; eval "use constant $1 => $2"; #...
Another alternative is something like this:
Don't do that either. The best idea is to take all these values and put them in a hash - even a global hash. And access them through that.use strict; require constant; while (<DATA>) { if (/^(\w+);(\d+);$/) { #eval "use constant $1 => $2"; constant->import({$1 => $2}); } } print MEMINUSEWAR(); __DATA__ MEMINUSEWAR;2000000;
|
|---|