in reply to variables not being set from a configuration file
$ cat dbconnect.txt # set up the database connection variables username = foo password = somepassword host = localhost database = adb table = notactual $ $ cat ./770979.pl #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my %db_data; my $file = 'dbconnect.txt'; open my $fh, '<', $file or die "Can not open file $file: $!"; while (<$fh>) { chomp; next if /\s*#/; # Ignore comments if (/=/) { my ($k, $v) = split /\s*=\s*/; $db_data{$k} = $v; } } close $fh; print Dumper(\%db_data); $ ./770979.pl $VAR1 = { 'database' => 'adb', 'password' => 'somepassword', 'table' => 'notactual', 'host' => 'localhost', 'username' => 'foo' };
Yet another alternative approach is to use one of the CPAN modules, such as Config::Simple.
|
|---|