reds has asked for the wisdom of the Perl Monks concerning the following question:

Greetings fellow monks.

So, I just learned that you can't declare the same constant multiple times in the same script; only one constant of the same name per module. Well, I've never made a module - and don't think it would "keep it simple, stupid" - so is there some alternative to keep the code readable?

Or, should I just suck it up and replace all the CONSTANTNAME with their actual values?
Or, is it not so bad to turn various subroutines into mini-modules?

What does thou wisdom forsee as the best solution?
  • Comment on Alternative to "use constant" in multiple subroutines

Replies are listed 'Best First'.
Re: Alternative to "use constant" in multiple subroutines
by dragonchild (Archbishop) on May 01, 2003 at 21:18 UTC
    Of course, this leads to the question of "Why are you changing the value of a constant?" Or, "Why are you using the same constant name twice?" I mean, either it's constant for the whole bleeping world or it's not!

    If you find you absolutely must use the same constant name twice for different values, you kinda have two options:

    1. Use different modules. I highly recommend this option.
    2. Use array constants.
      use constant FOO => [qw(a b c)]; use constant FIRST => (0); use constant SECOND => (1); print FOO->[FIRST], $/; print FOO->[SECOND], $/;
    Personally, the second is very ugly and prone to error. But, TMTOWTDI, I guess.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Thanks. I'll go look up how to make a module; I've never done that before.

      The reason the constant changes is as such: I'm parsing several different types of files based on how I'm called. The data is in rows & columns with different column headers between the files. So, depending on which subroutine I'm in (based on how I'm called) the constant TIME (referring to the column number) can take on different values.
        There's a much better way to do that. Use hashes. That's the canonical way to associate a string (like a column name) with a number (like a column number). :-)

        Of course, this isn't a reason not to learn modules. You should do that because it's the next step.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Alternative to "use constant" in multiple subroutines
by Corion (Patriarch) on May 01, 2003 at 21:14 UTC

    What is the sense of a constant, if it has different values, depending on the place ?

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web