In a bat file I can copy a tree (copy x y /s) and do a recursive delete against a glob, also pruning now-empty directories (del *.obj /sxyqz).
I was thinking about why Perl doesn't have single commands for these. The closest are components File::Find to go through the tree, File::Copy which will do an efficient OS-native copy if it can, File::Spec to separate out the directory from the file name proper, and File::Path to create subdirectories more than one level deep.
Now what I really want isn't to copy everything and then prune based on patterns, but to respect the patterns during the copy to only take what I need.
The logic is the core of my daily work backup system.
So, can I generalize that to be a good reusable module?
Here are my ideas:
By default, it simply copies an entire tree from source to dest locations. But, you can give it filter rules saying those you want or those to skip.
As an object, a list of rules as individual subs (or other objects) would be maintained, and individual rules can be added one at a time. That is, as opposed to a single callback that has to know everything.
A common case would be a pattern that matches the file name. So, give it a qr pattern object directly. Anything more complex, give it a sub that takes parameters for source and dest and other useful information.
A rule can tell the scanner to stop evaluating rules and KEEP or stop evaluating rules and SKIP, or continue with the next rule.
For subs, have different return codes. For the simple case of a qr//, how do I specify if that should be a pattern to keep or to skip? See the example below for one idea.
For a single callable function, it could take all the information in the parameter list, create the object, and immediatly invoke it. Say, dest, source, and any number of rules and options.
So, a rule is either a ref to a sub, or a pair (this is nicely extensible), and if it's a scalar string then it's an option flag not a rule.File::CopyTree ($dest, $src, [skip => qr/\.obj$/], &foo, 'CaseInsensit +ive');
In my backup program, I can skip whole directories. That can be done by having the same rule return the skip code when fed the name of a subdirectory, but I think it's better to have different rule callbacks since otherwise they would all begin with such a test anyway. But, that complicates how they are specified because you have more choices. In the object form, different add-rule methods would be simple. In the single-function form, perhaps different keywords in the pair form for "skip" and "skip-dir"?
I'll probably get back to this in a few days. Any thoughts or ideas on what I have so far?
—John
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: module design musings: File::CopyTree
by broquaint (Abbot) on Sep 26, 2002 at 23:32 UTC | |
by John M. Dlugosz (Monsignor) on Sep 27, 2002 at 14:37 UTC | |
|
Re: module design musings: File::CopyTree
by Aristotle (Chancellor) on Sep 27, 2002 at 17:37 UTC | |
by John M. Dlugosz (Monsignor) on Oct 03, 2002 at 16:16 UTC |