H
Henry Law
I'm implementing this in Perl but I recognise that there's a strong
element of language independent program design in the question. Hope
there's enough perlishness to keep me afloat.
I am writing a Perl program which will process a file tree and allow
the user to specify which directories and subdirectories are to be
included or excluded. (Anyone who uses xxcopy in Win will know
immediately what I mean). I plan to have the users describe the files
to include and exclude by means of strict Perl regex's. So a control
file might look something like this
include /a # Do files in a and all subdirs
exclude a/b/~?temp\d* # Except for temp files in a/b
.... and so on. I haven't worked out the full grammar yet (do I allow
indefinite series of include..exclude..include? I don't know). But
I'm having more trouble with conceptualising how to write the program
in Perl. Current idea is to write a recursive function to process all
the files in a single directory, calling itself for sub-directories.
It would slurp in the control file regex's and sort them
alphabetically into two arrays, one for "include" and one for
"exclude", and then implement logic like
$do_this_one = 0;
foreach $regex (@includes) {
if ($current_file =~ $regex) {
$do_this_one = 1;
}
}
foreach $regex (@excludes) {
if ($current_file =~ $regex) {
$do_this_one = 0;
}
}
do_the_stuff() if $do_this_one;
But doing that lot for every file looks very laborious; for example if
the control file is a simple "include /a and all subdirectories" then
I don't want to look at the regex more than once. And it's not very
Perl-ish either, come to that.
Questions:
(1) Is there a module that will help me? Or some code that I could
copy?
(2) If not, is there a better way of structuring the do-we-do-this-one
logic to make it more elegant and efficient?
Henry Law <>< Manchester, England
element of language independent program design in the question. Hope
there's enough perlishness to keep me afloat.
I am writing a Perl program which will process a file tree and allow
the user to specify which directories and subdirectories are to be
included or excluded. (Anyone who uses xxcopy in Win will know
immediately what I mean). I plan to have the users describe the files
to include and exclude by means of strict Perl regex's. So a control
file might look something like this
include /a # Do files in a and all subdirs
exclude a/b/~?temp\d* # Except for temp files in a/b
.... and so on. I haven't worked out the full grammar yet (do I allow
indefinite series of include..exclude..include? I don't know). But
I'm having more trouble with conceptualising how to write the program
in Perl. Current idea is to write a recursive function to process all
the files in a single directory, calling itself for sub-directories.
It would slurp in the control file regex's and sort them
alphabetically into two arrays, one for "include" and one for
"exclude", and then implement logic like
$do_this_one = 0;
foreach $regex (@includes) {
if ($current_file =~ $regex) {
$do_this_one = 1;
}
}
foreach $regex (@excludes) {
if ($current_file =~ $regex) {
$do_this_one = 0;
}
}
do_the_stuff() if $do_this_one;
But doing that lot for every file looks very laborious; for example if
the control file is a simple "include /a and all subdirectories" then
I don't want to look at the regex more than once. And it's not very
Perl-ish either, come to that.
Questions:
(1) Is there a module that will help me? Or some code that I could
copy?
(2) If not, is there a better way of structuring the do-we-do-this-one
logic to make it more elegant and efficient?
Henry Law <>< Manchester, England