in reply to Global variables question
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Global variables question
by PerlScholar (Acolyte) on Aug 24, 2010 at 14:43 UTC | |
by Marshall (Canon) on Aug 24, 2010 at 16:50 UTC | |
by PerlScholar (Acolyte) on Aug 24, 2010 at 22:36 UTC | |
by Marshall (Canon) on Aug 26, 2010 at 18:43 UTC | |
by PerlScholar (Acolyte) on Aug 31, 2010 at 11:48 UTC |