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


in reply to Sharing the scalar love?

Here is how I usually do it:

1. Create a separate module with all the shared staff:

package Common; use constant GLOB_DEBUG_MODE = 1; use constant GLOB_SITE_NAME = 'My site'; 1;
2. use Common; from anywhere that I need. For example:
package Main; use Common; sub print_message { my $msg = shift; # Use default site name if no message specified. unless ($msg) { $msg = Common::GLOB_SITE_NAME; } # Prepend with "DEBUG: " if in debug mode. $msg = "DEBUG: $msg" if (Common::GLOB_DEBUG_MODE); print $msg; } 1;

Update: I've answered a bit a wrong question. :) You were asking about global variables, while I described constants. :)

Update: Fixed two misplaced 'Common's. :)

Replies are listed 'Best First'.
Re: Re: Sharing the scalar love?
by jcpunk (Friar) on Dec 22, 2003 at 21:23 UTC
    I'm assuming that the file in "1. Create a separate module with all the shared staff:" is called Config in part 2 which strikes me as odd.... perhaps a typo in "(Config::GLOB_DEBUG_MODE)"? I'm still sorta trying to figure this out. But my confusion says that perhaps "(Common::GLOB_DEBUG_MODE)" is what you meant?
    If that is the case you have helped me out greatly!
    Update: After looking over the rest of the replies I think that probably is the case, but massive brownie points for responding 99% perfectly in under 2 minutes of me posting

    jcpunk
    all code is tested, and doesn't work so there :p (varient on common PM sig for my own ammusment)