2013-09-26 34 views
0

您好,我試圖保存一個名爲'chksum'的文件中的哈希引用,並在需要時再檢索它。但它給了我一個錯誤。無法在perl中檢索哈希引用

Test.pl

#!/usr/local/cpanel/3rdparty/bin/perl 
use Storable; 
use File::Find qw(find); 
use Digest::MD5 qw(md5_base64); 
use Data::Dumper; 
#=head 
###################### 
#Collect checksum # 
###################### 
my $files = {}; 
$files_ref = retrieve('checksum'); 
find(sub { 
     my $file = $File::Find::name; 
     return if ! length($file); 
     my ($size, $mtime) = (stat $_)[7, 9]; 
     open (FILE, $file); 
     $chksum = md5_base64(<FILE>); 
     $files->{$file} = { 
      local_is_dir => (-d _ ? 1 : 0), 
      local_size => $size, 
      local_mtime => $mtime, 
      chksum  => $chksum, 
     }; 
     #$ref = $files->{$file} || = {}; 
     my $ref = $files_ref->{$file} ||= {}; 
     if ($chksum != $ref->{chksum}) {print $file;} 
    }, '/root/ftp_kasi/'); 

#print Dumper(\$files); 
store \$files, 'checksum'; 

輸出

[email protected] [~/ftp_kasi]# perl test.pl 
Not a HASH reference at test.pl line 25. 

有人可以幫我出這一點。提前致謝。

+0

'打印自卸車$ files_ref,$文件;'分配之前,檢查。也考慮使用嚴格和警告=> http://stackoverflow.com/questions/8023959/why-use-strict-and-warnings –

+0

謝謝我使用嚴格和警告,但沒有其他錯誤消息。 「不在test.pl第25行的HASH參考」出現在作業行中。因此,打印Dumper $ ref不會產生任何結果。 –

+0

'print Dumper $ files_ref,$ file;'但在$ ref assigment之前。 '$ files_ref'不像它看起來那樣是hashref。 –

回答

2

你存儲參考hashref,當你真正想只用hashref,所以更換

store \$files, 'checksum'; 

store $files, 'checksum'; 
+0

很酷。這是問題。你解決了我的問題。十分感謝.. :) –

0

$ref是空的,因爲||=運營商不會做你認爲它的工作。 那麼用||來代替呢?

+0

我嘗試使用'my $ ref = $ files_ref - > {$ file} || {};」 。但它也給出了同樣的錯誤。 –