wrote:
> If I want to have a setup up file (setup.txt), so my perl program can
> read in variables to be used, like $install_dir, or $output_dir, and
> use them across multiple perl programs. How do I read these variables
> in from that file so they can be declared?
There are a few ways of doing this. The way that everyone starts off
assuming they want to do is to populate a file with commands like
my $file = 'input.txt';
my $dir = '/home/mritty';
#etc
This is the WRONG way to do it. These variables will only be visible
while in this file. Any other file that loads this file will not have
access to these variables.
The easiest/"best" way of doing this is to define a module which
exports the variables you want to have access to in the 'main' file,
and import those variables in the main file:
#In file: MyConfig.pm
package MyConfig;
use strict;
use warnings;
use base qw/Exporter/;
our @EXPORT_OK = qw/%config/;
our %config = (
'file'=>'input.txt',
'dir' =>'/home/mritty',
#etc
);
1;
Here I define one hash that contains all the configuration values you
need, rather than separate scalar variables for each value. This is so
you don't need to continually modify the @EXPORT_OK array every time
you add or remove a configuration value.
Then, in the 'main' file:
#!/usr/bin/perl
use strict;
use warnings;
use MyConfig qw/%config/;
print "The full path is: $config{dir}/$config{file}\n";
__END__
For more information on this procedure, please read:
perldoc Exporter
perldoc perlmod
perldoc -f our
perldoc -f use
Hope this helps,
Paul Lalli