Perl哈希表中元素数量为奇数。

3
我有一个配置文件。
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$
3个回答

7
my %prefs = "";

引发错误的是这行代码。哈希表应该用由偶数元素组成的列表进行初始化。你可以这样编写:

my %prefs = ();

但是没有必要这样做,因为…
my %prefs;

做相同的事情。


6

请使用my %prefs;

当你说:

my %prefs ="" ;

你正在创建一个具有以下结构的哈希表。
$VAR1 = {
          '' => undef
        };

这里是使用您子程序中代码的小演示:

use warnings;                                              
use strict;        

my %prefs;    

while (<DATA>) {                                                                
    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";                                              
}                                                                               


__DATA__                                                                        
dbUser=customer                                                                 
dbPass=passwrd                                                                  
dbSid=customer.shadow                                                           
passwdFile=/production/etc-user                                                 
tmpUsers=tmpuser                                                                
htpasswd=/usr/local/apache2/bin/htpasswd  

它会输出什么。
dbUser : customer
dbPass : passwrd
dbSid : customer.shadow
passwdFile : /production/etc-user
tmpUsers : tmpuser
htpasswd : /usr/local/apache2/bin/htpasswd

3

当赋值一个空哈希时,请使用()

因此,只需更改这一行:

my %prefs ="" ;

转换为:

my %prefs = ();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接