我有一个配置文件。
然后读取配置文件的脚本:
我遇到了一个奇怪的错误 - 哈希分配中有奇数个元素。
dbUser=customer
dbPass=passwrd
dbSid=customer.shadow
passwdFile=/production/etc-user
tmpUsers=tmpuser
htpasswd=/usr/local/apache2/bin/htpasswd
然后读取配置文件的脚本:
#!/usr/local/bin/perl
use warnings;
use strict;
readConfig();
# reads in a config file
sub readConfig {
my $configFile = $ARGV[0];
my %prefs ="" ;
open (CONFIG, "< $configFile") or die "Cannot open $configFile for reading: $!\n";
while (<CONFIG>) {
chomp; # kill newlines
s/#.*//; # ignore comments
s/^\s+//; # ignore leading whitespace
s/\s+$//; # ignore trailing whitespace
next unless length;
my($var,$value) = split(/\s*=\s*/, $_, 2);
$prefs{$var} = $value;
print "$var : $prefs{$var}\n";
}
}
我遇到了一个奇怪的错误 - 哈希分配中有奇数个元素。
bash-3.00$ /tmp/config_foo /production/cfg/users.cfg
Odd number of elements in hash assignment at /tmp/config_foo line 10.
dbUser : customer
dbPass : passwd
dbSid : customer.shadow
passwdFile : /production/etc-users1
tmpUsers : tmpuser
htpasswd : /usr/local/apache2/bin/htpasswd
bash-3.00$
bash-3.00$