my $dir = "/Path/to/a/data/directory";
my %hash;
$hash{$_} = hashify_file( $_ ) for ls_dir( $dir );
sub ls_dir {
my $dir = shift;
opendir my $dh, $dir
or die "Can't opendir '$dir': $!\n";
return map { "$dir/$_" }
grep { !/^\./ && !/\~$/ } readdir $dh;
}
sub hashify_file {
my $file = shift;
open my $fh, '<', $file
or die "Can't read '$file': $!\n";
my $out;
%{$out} = ( %{$out}, %{hashify_line( $_ )} ) for <$fh>;
close $fh or die "Can't close '$file': $!\n";
return $out;
}
sub hashify_line {
my $line = shift;
chomp $line;
$line =~ s/^\s+//;
return {} if ( $line =~ /^\#/ || $line =~ /^\s*$/ );
my ( $key, @values ) = split /\t/, $line;
return { $key => \@values };
}
####
my $dir = "/Path/to/a/data/directory";
my %hash;
open my $grep_fh, '-|', "grep '^' $dir/* /dev/null"
or die "Can't grep: $!\n";
while ( my $line = <$grep_fh> ) {
$line =~ s/^([^:])://;
my $file = $1;
next if $file =~ /\~$/;
%{$hash{$file}} = (%{$hash{$file}}, %{hashify_line( $line )});
}
close $grep_fh or die "Error closing grep pipe: $!\n";
##
##
my $cmd = "find $dir -type f -print | xargs grep '^' /dev/null";
open my $grep_fh, '-|', $cmd
or die "Can't grep: $!\n";