I think you are relatively new to this, so I will explain my thinking while writing some code for you...
This is the first time that I'd used the MD5 check sum thing. I downloaded and installed the MD5 module and then considered what is the easiest format to use? I figured that hex was. I did a little hacking to see that this produced what I thought that it would. Not to say that base64 is "bad", use what you want all that matters here that you can generate a repeatable string for a file and that is is very unlikely for 2 different files to have the same string.
The next step was to figure out the right data structure. Basically we need file name (which is unique in a directory) and the fancy check sum (MD5)for that file which will be a single string consisting of hex values. A hash table instead of an array seemed appropriate.
Then I made a subroutine that takes pathname and returns the hash of name=>checksum. There are some ways of passing back the return values more efficiently, but here it didn't seem to matter.
I'm not sure what the comparison rules are. If say dirA is considered the "master dir" and you want to know if any files changed in dirB, loop over names in dirA and see if corresponding checksum in dirB matches. Things get more complex if you want to know if any "additional" files are in B that are not in A. Rather than write code, I leave it to you do decide what you need.
#!/usr/bin/perl -w use strict; use Digest::MD5 qw(md5 md5_hex md5_base64); use Data::Dumper; $| =1; #turns off output buffering, useful for debugging #all of these are possible # $digest = md5($data); # $digest = md5_hex($data); #make it easy , use this! # $digest = md5_base64($data); my %dirA = get_chksums("."); #### put real dir name here, #### not "."(current directory) print Dumper \%dirA; my %dirB = get_chksums("."); #### put real dir name here print Dumper \%dirB; ##### ### put some comparison stuff here ##### sub get_chksums { my $path = shift; my %file2cksum; opendir (INDIR, $path) || die "unable to open $path"; my @files = grep {-f "$path/$_"} readdir INDIR; close INDIR; foreach my $file (@files) { open (IN, '<', "$path/$file") || die "unable to open $path/$file"; $file2cksum{$file} = md5_hex(<IN>); # print "$file $file2cksum{$file}\n"; #for debugging... close IN; } return %file2cksum; }
In reply to Re: Global variables question
by Marshall
in thread Global variables question
by PerlScholar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |