我正在尝试按如下方式创建哈希:
my $block_name = pm88_60_464 ;
my %hash_for_tstamp = qw (
layout/${block_name}.proj/tech ap_db_time
rpts/sem_check/sem_check_analysis.rpt sem_analysis
);
my $key;
my $value;
while (($key,$value) = each (%hash_for_tstamp)) {
${$time_summary}{"$value"} = ctime(stat($key)->mtime) ;
}
在上面运行代码时,我遇到了错误。
stat() on unopened filehandle File::stat::layout/${block_name}.proj/tech .
Can't call method "mtime" on an undefined value at audit_automation.pl line 205.
所以我通过这个错误理解的是我们被允许在哈希内部使用变量键。
答案 0 :(得分:4)
问题是变量插值,或者在使用qw//
构造时缺少它。 qw//
内部没有变量插值,如perlop Quote and Quote-like Operators
您可以使用适当的类似引用的构造来解决这种情况;在这种情况下,需要变量插值的双引号。这是一个例子:
my $block_name = "pm88_60_464";
my %hash_for_tstamp = (
"layout/${block_name}.proj/tech", "ap_db_time",
"rpts/sem_check/sem_check_analysis.rpt", "sem_analysis"
);
while ( my ($key,$value) = each (%hash_for_tstamp) ) {
print "($key),($value)\n";
}