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 | |
by agianni (Hermit) on Mar 12, 2007 at 12:57 UTC | |
by Anno (Deacon) on Mar 12, 2007 at 13:27 UTC |