Now that everyone has told you why it doesn't work, here's one solution to getting it to work:

if ($_ =~/^(\w+);(\d+);$/ && $_ !~/^\#/) { print "DEBUG: use constant $1 => $2;\n"; eval "use constant $1 => $2"; #...
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.

Another alternative is something like this:

use strict; require constant; while (<DATA>) { if (/^(\w+);(\d+);$/) { #eval "use constant $1 => $2"; constant->import({$1 => $2}); } } print MEMINUSEWAR(); __DATA__ MEMINUSEWAR;2000000;
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.


In reply to Re: Define constant in loop by Tanktalus
in thread Define constant in loop by penantes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.