Dear Monks,
I finished a script, after thinking of multiple ways to approach this, that does what I need and creates my directories and files I need for some tests that I run. Mostly I need to directories and files for detection, it's ok for some of the files to be 0 length they just need to be there; which is where the empty FD's come from. Some I copy for other tests, those files are local and are copied in because they have data we need. Rather than copy the directory trees (in total there are 16 directory trees, but I don't show them all here) I came up with the following.
First I generated all the arrays that list the files and directories I need, m, d and f are described further on, here are two examples of the arrays I created.
my @configapp1 = ("m:configapp1",
"d:conf",
"f:dactest1",
"f:conf,4K_logging.properties",
"f:conf,logging.properties",
my @configbbb = ("m:configbbb",
"f:b1.0",
"f:bb1.0.config");
I have an array of references to the arrays so I can make sure each one is done.
my @allDACDirs = (\@configapp1, \@configbbb);
Going through the array of references I build what I need:
# Determine the size of the array so we hit everything
my $array_no = scalar(@allDACDirs);
for( my $i = 0; $i < $array_no; $i++)
{
my $curr_dir;
my $entry;
# Read the array and populate
foreach $entry (@{$allDACDirs[$i]})
{
# Review value whether its a dir or file
my($chk_val, $filename) = split(/:/,$entry);
# m means we need to make a new root directory
if ($chk_val eq "m")
{
mkdir("$location/$filename",0755) or
warn "Could not mkdir: $!\n";
$curr_dir = "$location/$filename";
}
# d means a directory under root (like bin or lib)
elsif ($chk_val eq "d")
{
mkdir("$curr_dir/$filename",0755) or
warn "Could not mkdir: $!\n";
}
# f means a file has to be made
elsif ($chk_val eq "f")
{
# If there is a , we need a sub_dir, then make
# file in that directory
if ($filename =~ /,/)
{
my($dir_name, $file) = split(/,/,$filename);
if (!-d "$curr_dir/$dir_name")
{
mkdir("$curr_dir/$dir_name",0755) or
warn "Could not mkdir: $!\n";
}
# If file exists in current directory then copy
if (-e "$local_dir/files/$file")
{
copy("$local_dir/files/$file", "$curr_dir/$dir_nam
+e/$file") or
die "Could not copy $filename: $!\n";
}
else
{
# Else create the shell for the file
open(FD,">$curr_dir/$dir_name/$file") ||
die "Could not make $file: $!\n";
}
}
else
{
# If file exists in current directory then copy
if (-e "$local_dir/files/$filename")
{
copy("$local_dir/files/$filename", "$curr_dir/$fil
+ename") or
die "Could not copy $filename: $!\n";
}
else
{
# Else create the shell for the file
open(FD,">$curr_dir/$filename") ||
die "Could not make $filename: $!\n";
}
}
}
}
}
My question is more educational, with the numerous methods to handle things in Perl what are some of the ways this could have been done differently? I tried doing this with hashes or arrays of hashes without getting what I wanted, the array of arrays I was able to understand and follow. Or maybe I'm not good at following depth in multi-dimensional arrays. I'm sure some things could be more elegant as well, and it'd be a good learning opportunity for me to see what I could have done simpler, or better.
Thanks!