2012-01-08 71 views
0

我有一段代码在与所有其他调用相似的数据失败之前使用了60K次。我收到“不是HASH参考”消息。代码如下:即使在取消引用后,Perl哈希引用也会导致错误

sub getRowKey 
{ 
    my ($self,$row) = @_; 
    my @keys = split(/,/,$self->{_key}); 
    my $rowkey = ""; 

    confess "Not a non-reference" if (! defined(ref($row))); 
    confess "no keys found". Dumper($row) if(scalar(@keys) == 0); 
    foreach my $k (@keys) 
    { 

     try 
     { 
      $rowkey .= "," if $rowkey ne ""; 
      $rowkey .= $row->{$k}; 
     } 
     catch Error with 
     { 
      $ex = shift; 
      print "rowkey = '$rowkey' k = '$k'\n"; 
      print Dumper($ex); 
      print Dumper($row); 
      confess "Exception: " . $ex->{-text}; 
     } 
     ; 
    } 
    return $rowkey; 
    } 

当代码看到异常:我得到以下输出:

rowkey = '' k = 'TopicId' 
$VAR1 = bless({ 
      '-file' => 'baseDB.pm', 
      '-text' => 'Not a HASH reference', 
      '-line' => '95', 
      '-package' => 'Error' 
      }, 'Error::Simple'); 
$VAR1 = \{         ## note this is a a reference! 
     'LastReplyId' => 8563, 
     'LastPostDate' => '2006-06-21 13:37:48', 
     'TopicId' => '8563',     ## note this is they name/value pair to be accessed 
     'LastTopicDate' => '2000', 
     'LastReplyDate' => '2006-06-21 13:37:48', 
     'ForumId' => '84', 
     'LastPostData' => '...' 
     } 

谢谢您的帮助和建议。

回答

3

它看起来像$ row不是散列引用,而是对散列引用的引用。看看这个例子:

use Data::Dumper; 

my %h = (a=>1,b=>2); 
my $r = \%h; 
my $rr = \$r; 

print Dumper(%h); 
print Dumper($r); 
print Dumper($rr); 

将返回:

$VAR1 = 'a'; 
$VAR2 = 1; 
$VAR3 = 'b'; 
$VAR4 = 2; 
$VAR1 = { 
      'a' => 1, 
      'b' => 2 
     }; 
$VAR1 = \{ 
      'a' => 1, 
      'b' => 2 
      }; 

为参考基准的样子$VAR1 = \{在你的错误转储。

你可能想尝试通过修改这条线搭上:

confess "Not a non-reference" if (! defined(ref($row))); 

要代替的样子:

confess "Not a non-reference" unless ref($row) eq 'HASH'; 
+0

谢谢。干净的说明显示引用的参考。当我说代码被称为60K次时,我错了。它从一个多态类被称为60K次,我错过了对现在使用该例程的类生成的引用的引用。 – 2012-01-08 04:31:23