Is "use vars" safe to use? The current Perl documentation says that's obsolete, and "our" is supposed to be used instead.
My interpretion of "our" is that it's roughly analogous to "extern" in C/C++. I.e., it doesn't allocate space for a variable, but rather brings a variable defined elsewhere into the current scope. But I'm not sure if that's really accurate.
To refresh my memory, I dug out an old Perl script I wrote a couple of years ago to see how I handled this situation. Here's an extract that illustrates the method. Some of this I had to figure out by trial and error, and may not be the best way of doing it.
fsConfig.pm - configuration module - defines class fsConfig
package fsConfig;
use strict;
use re 'taint';
# constants
use constant MAX_ID => 999_999; # warning - don't change this
use constant FTP_TIMEOUT => 15; # seconds
use constant MAX_SEARCH_RESULTS => 100; # max number of items returned by search
# class constructor
sub load {
my $invocant = shift;
my $self = {
...
'submissions_mode' => 'restricted',
...
};
my $class = ref($invocant) || $invocant;
return bless($self, $class);
}
# accessor methods
...
sub submissions_mode {
my $self = shift;
return $self->{'submissions_mode'};
}
...
1;
main.cgi - example module that accesses configuration parameters
#!/usr/bin/perl -wT
use strict;
use re 'taint'; # suppress untainting by regexes
use fsConfig();
# instantiate configuration object and store it in a global variable
$::Config = fsConfig::->load() or die 'Failed to load configuration data';
# suppress warning 'name used only once' (?)
our $Config;
...
# example of accessing configuration constant:
print "The FTP-timeout parameter is " . $Config->FTP_TIMEOUT . "n";
# example of accessing configuration variable:
print "The submissions-mode is " . $Config->submissions_mode() . "n";
...
Critique is welcome
