dagolden / Path-Tiny

File path utility
41 stars 59 forks source link

[Feature] `set_parent` method #253

Closed rwp0 closed 2 years ago

rwp0 commented 2 years ago

Currently the parent method returns the parent directory path (optionally to n-th degree with a numeric argument).

Can we have a method similar to child that would set the parent instead?

I needed such a method for my script, and didn't find it reading the documentation.

For instance:

use Path::Tiny;
my $extension = path 'php/20131226/curl.so';
my $path = $extension -> set_parent('/usr/local/lib');
say $path; # /usr/local/lib/php/20131226/curl.so

Since it mostly just prepends (being the reverse of child), shouldn't be hard to implement I think.

Thank you for the attention.

xdg commented 2 years ago

I don't think this needs a new method. There are a couple ways to do it, depending on the semantics you want:

# add the extension as child of the new parent
$path = path("/usr/local/lib")->child($extension);

# root a relative extension under an absolute path
$path = $extension->absolute("/usr/local/lib");