I need to
Read a list of the files from in a specified directory
Create an array of hashes describing those files
My program fetches the path to a directory from the command line, opens it and reads its contents. Directories and "dot" files are skipped, and the absolute path to every other entry is printed.
use strict;
use warnings;
use Data::Dumper qw(Dumper);
use File::Spec;
use Digest::SHA qw(sha256_hex);
my $dir = $ARGV[0];
opendir DIR, $dir or die "cannot open dir $dir: $!";
while ( my $file = readdir DIR ) {
next unless ( -f "${ \File::Spec->catfile($dir, $file) }" );
next if ( "$file" =~ m/^\./) ;
print "${ \File::Spec->rel2abs($file) }\n";
}
closedir DIR;
Here I take a single file and create a hash with the path, size, and sha256sum.
my $file = "file1.txt";
my @fileref = (
{
path => "Full path: " . File::Spec->rel2abs($file),
size => "Size (bytes): " . -s $file,
id => "SHA256SUM: " . sha256_hex($file),
},
);
print "\n";
print "$fileref[0]{path}\n";
print "$fileref[0]{size}\n";
print "$fileref[0]{id}\n";
All of this works, but I cannot figure out how to iterate over each file and add it to the array.
This is what I planned
for each file
push file into array
add the path, size, and id key:value pairs to file
repeat
How can I generate the necessary array?
push @array, { path => ... }
– Wumpus Q. Wumbley Dec 8 '17 at 1:14