in reply to Re: Variable declaration in 'required' file doesn't work under 'strict'?
in thread Variable declaration in 'required' file doesn't work under 'strict'?

Rather than using the vars pragma, I would recommend using package variables:

In config.pl:

package MyConfig; our %state_name_for = ( AL => 'Alabama', AK => 'Alaska', AZ => 'Arizona' );

In your main file:

require 'config.pl'; for my $abbrev ( keys %MyConfig::state_name_for ){ print $MyConfig::state_name_for{$abbrev}; }

Using package variables will make your code easier to read, as it will be more obvious where your variables are coming from than if you simply globalize them and will make it less likely that you accidentally update them in your script code.

Better yet, follow The Damian's advice from Perl Best Practices and avoid using package variables, instead, use subroutines or methods in the package containing the variables to access them:

package MyConfig; my %state_name_for = ( AL => 'Alabama', AK => 'Alaska', AZ => 'Arizona' ); sub state_name_for{ return \%state_name_for; }

In your main file:

require 'config.pl'; for my $abbrev ( keys %{MyConfig::state_name_for} ){ print $MyConfig::state_name_for{$abbrev}; }

This will prevent you from accidentally updating your config variables from your script.

Replies are listed 'Best First'.
Re^3: Variable declaration in 'required' file doesn't work under 'strict'?
by shmem (Chancellor) on Mar 12, 2007 at 05:39 UTC
    Rather than using the vars pragma, I would recommend using package variables:
    In what way are variables declared with the vars pragma not package variables?
    This will prevent you from accidentally updating your config variables from your script.
    It will also prevent you from accessing the keys of the %state_name_for hash, since in your last example it is a lexical scoped my variable not visible in config.pl.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      In what way are variables declared with the vars pragma not package variables?

      Sorry, you're right, the vars pragma does indeed make the variables part of the main package. What I meant to suggest is that making them part of an explicit package other than main makes code more readable.

      it will also prevent you from accessing the keys of the %state_name_for hash, since in your last example it is a lexical scoped my variable not visible in config.pl.

      Again, I wasn't clear that I was suggesting that the MyConfig package is declared in the config.pl file. %state_name_for is declared as lexically scoped precisely to avoid making it available from outside of the package. Instead, package users should use the state_name_for subroutine to access it. This will help coders avoid this pitfall:

      if( $MyConfig::state_name_for{AL} = 'Alaska' ){ print "oops! we just assigned Alaska to the AL key!\n"; }
        Instead, package users should use the state_name_for subroutine to access it. This will help coders avoid this pitfall:
        if( $MyConfig::state_name_for{AL} = 'Alaska' ){ print "oops! we just assigned Alaska to the AL key!\n"; }

        But your state_name_for is defined

        sub state_name_for{ return \%state_name_for; }
        That returns a reference to the hash %state_name_for and exposes it to the user just like accessing the hash directly. To protect the hash, put something like this in MyConfig:
        sub get_state_name { $state_name_for{ +shift} }
        and keep %state_name_for a private lexical in MyConfig. Now the user can't change the hash content, accidentally or deliberately. They still can query the hash the way it is intended.

        Anno