in reply to Alternative to "use constant" in multiple subroutines

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.

Replies are listed 'Best First'.
Re: Re: Alternative to "use constant" in multiple subroutines
by reds (Novice) on May 01, 2003 at 21:26 UTC
    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.

        Thanks; I don't know why I didn't think of hashes. I probably did, but dismissed them for some dumb reason. I'm going to do it the hash way b/c that Seems Best - but I promise to learn modules, too. *grin*