http://qs321.pair.com?node_id=135491


in reply to question on constants

My guess is that you were probably doing something like:

use module_that_tries_to_use_the_constant; use constant CFGDIR=>'/config/';

The problem with that is that perl tries to compile your module first, but doesn't know what main::CFGDIR is at the time since the use constant hasn't happened yet. You can probably get around this by putting the use constant before the other use or doing main::CFGDIR() in the module (this lets perl know that you're trying to call the function by that name; constants are really functions with a specific prototype).

(I'm guessing here. Next time -- give an error message.)

I would note that this is bad coding practice -- a module should be independent of the "main" program. You probably should be passing a configuration directory to the module, instead. (If you really need the speed boost constants give, then you can still get it this way by having this information be passed on the use line, and defining a constant within the module at compile-time based on that...)

Replies are listed 'Best First'.
Re: Re: question on constants
by smackdab (Pilgrim) on Jan 01, 2002 at 09:41 UTC
    Thanks, I did find that putting a '&' infront of the constant, got it to work...I'll also move my config stuff into a module, so that I am not grabbing stuff from the main program...but I am a little lazy ;-) (at time, more than a little!)