0

Hi I am trying to save a hash reference in file named 'chksum' and retrieve it later when needed. But it gives me an error.

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';

Output

root@- [~/ftp_kasi]# perl test.pl 
Not a HASH reference at test.pl line 25.

Can somebody help me out on this. Thanks in advance.

Kasinath Kottukkal
  • 2,471
  • 4
  • 22
  • 28
  • `print Dumper $files_ref, $file;` before assignment to inspect. Also consider using strict and warnings => http://stackoverflow.com/questions/8023959/why-use-strict-and-warnings – mpapec Sep 26 '13 at 06:43
  • Thanks I used strict and warnings but no other error messages. "Not a HASH reference at test.pl line 25" appears on the assignment line. Therefore print Dumper $ref not yielding any results. – Kasinath Kottukkal Sep 26 '13 at 06:50
  • `print Dumper $files_ref, $file;` but before $ref assigment. `$files_ref` is not hashref as it seems. – mpapec Sep 26 '13 at 06:51
  • I am able to print $files_ref using Dumper. But when I print just any key $files_ref it gives me Not a HASH reference error. Can I not print it using print $files_ref->{'key'} – Kasinath Kottukkal Sep 26 '13 at 06:57
  • update your question with output of `print Dumper $files_ref, $file;` – mpapec Sep 26 '13 at 06:58
  • I reduced code length so that problem can be highlighted. use Storable; use Data::Dumper; my $config = { host => '168.144.97.101', }; store \$config, 'stalk.txt'; $ref = retrieve ("stalk.txt"); print $config->{'host'}; print "\n"; print Dumper $ref; print $ref->{'host'}; print "\n"; Output ====== 168.144.97.101 $VAR1 = \{ 'host' => '168.144.97.101' }; Not a HASH reference at stalk.pl line 11. – Kasinath Kottukkal Sep 26 '13 at 07:04
  • You can see that once I can not print just one key value even after successfully retrieving hash from file. (I am able to print $ref using Dump)@mpapec – Kasinath Kottukkal Sep 26 '13 at 07:05

2 Answers2

2

You're storing reference to hashref, when you actually want to use hashref only, so replace

store \$files, 'checksum';

with

store $files, 'checksum';
mpapec
  • 50,217
  • 8
  • 67
  • 127
0

$ref is empty because the ||= operator does not do what you think it does. What about using || instead?

rurban
  • 4,025
  • 24
  • 27