Subject: | Access to Path as List |
Hello there. Thank you so much for Path::Class; it is wonderful to work with, and I wish I'd discovered it sooner.
I've got a feature request: I'd like a method that returns the path as a list of separate path elements. How this would currently help me is explained below, though it'd be a sufficiently general feature that may well have other uses. I'm a little hesitant about it though, cos I'm not sure what the best way of dealing with volumes or absolute paths is -- they don't matter in my particular case, and I'd be happy for a method that simply returns a list of the path components that do exist, and which leaves it up to me to know whether it's an absolute or relative path and whether a volume is needed.
What I'm doing is transplanting a little subdirectory tree from one place to another: given a directory $dir (say /srv/3/www/k/e/ken) and a new base $base (say /home/smylers/dev/users) I'd like to form a new path by sticking the 3 right-most components of $dir on to $base (giving /home/smylers/dev/users/k/e/ken). This can be achieved at the moment with:
my $new_path = dir $base, $dir->relative($dir->parent->parent->parent);
By breaking encapsulation it can also be done with:
my $new_path = dir $base, @{$dir->{dirs}}[-3 .. -1];
The general approach of the latter seems preferable to me -- in particular, the number of levels being transplanted is specified as a constant, 3 (which could easily be changed or replaced by a variable) rather than by having that number of chained method calls (which is more tedious to change and would require a loop if a variable were used). But obviously I don't like breaking encapsulation, hence my desire for a documented method call to get this information as a list.
Does that make sense? Let me know if I can be any help.
Smylers