in reply to Re^2: How not to use "our"?
in thread How not to use "our"?

If you think about it, a package symbol table (stash) *is* a hash, so the individual package variables within a package who's only purpose is to hold constants or configuration parameters is already organised as you would like it.

If you then avoid exporting the individual variables and use them as $config::one, $config::two etc. then you've got the best of both worlds.

The variables are nicely organised, and you also get typo checking at compile time:

>perl -c -wE"package x;our $fred=50;package main; print $x::fred,$x::d +erf" Name "x::derf" used only once: possible typo at -e line 1. -e syntax OK

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^4: How not to use "our"?
by Anonymous Monk on Nov 30, 2010 at 10:46 UTC

    Need a little clarification. So it's better to export a collective hash (reference) or individual variables (as suggested by JavaFan)?

      's better to export a collective hash (reference) or individual variables (as suggested by JavaFan)?

      Personally, I wouldn't export either.

      As I attempted to show above, I'd use individual package (our) variables, and use them via fully qualified references:

      package MyConfig; our $one = ...; our $two = ...;
      use MyConfig; ... if( $x <= $MyConfig::one ) { ...

      All the variable are nicely contained within the package stash (symbol table hash); warnings are issued for typos; and where they come from is clearly defined. And by not importing them, you avoid namespace clashes.

      If they are truly constants, then I use constant and avail myself of the optimisations that brings.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Ah, I understand now. Many thanks :)

        With your approach - declaring variables with "our" - would it lead to problems with mod_perl? I've never used mod_perl before, but I read that you can't have globals when using mod_perl.

        Suppose I export the hashed variables (as in my approach), would that cause problems in a mod_perl environment?