i got the following function and dont know how to put the output into a variable.
sub checkFiles {
# Declaration
my $origDir="/home/hbo/test/chksum/";
my $tmpDir="/home/hbo/test/tmp/";
# get directory inventory
opendir( DIR, $origDir);
my @files = sort ( grep { !/^\.|\.\.}$/ } readdir(DIR) );
closedir(DIR);
foreach my $file (@files) {
if ( !-r $origDir.$file) { print $origDir.$file, "does not exist"; next;}
# open filehandles
open my $a_fh, '<', $origDir.$file or die "$origDir.$file: $!";
open my $b_fh, '<', $tmpDir.$file or die "$tmpDir.$file: $!";
# map difference
my %tmpDirFile;
@tmpDirFile{map { unpack 'A*', $_ } <$b_fh>} = ();
# print difference
while (<$a_fh>) {
print unless exists $tmpDirFile{unpack 'A*', $_};
}
close $a_fh;
close $b_fh;
}
}
The Line i got the questions is "print unless exists $tmpDirFile{unpack 'A*', $_};" i want to put this output into a variable like an array where i can decide between "changed" or "deleted" or "new". A short summary what my script will do: it builds a md5 sum of a directory, check if the directory differs from the version before and print the differences with flags like "new", "deleted", "changed". And yes I don't want to use additional libraries.
The output on console is:
40567504a8a2f9f665695a49035b381b /home/hbo/test/somedir/some/some.conf
Now I want to show if the file has changed, deleted or is new. Because of this i need to put the output into a variable. Can someone help me?
You can use a hash to do that. It's a bit like counting, just that you don't simply count, but keep a list of files in each key. It would look like this:
Create that hash (or in my example a hash reference) outside the loop, and then push into the array ref in the appropriate key. You don't even need to create the array ref, autovivification will take care of that for you.