Y
Yves Martin
Hello,
I'm looking for a way to clean file paths like the following:
~/dir1/../dir2
~/dir1/../dir2/f1
~/dir1/./dir2/f1
/dir1/../dir2
/dir1/../dir2/f1
/dir1/./dir2/f1
I tested Cwd->realpath that reduce ./ and ../ but has many drawbacks:
- it does not understand ~/
- it follows symbolic links
- it checks path over the filesystem, so an unexisting file becomes
undef.
Results I expect are obviously:
~/dir1/../dir2 -> ~/dir2
~/dir1/../dir2/f1 -> ~/dir2/f1
~/dir1/./dir2/f1 -> ~/dir1/dir2/f1
/dir1/../dir2 -> /dir2
/dir1/../dir2/f1 -> /dir2/f1
/dir1/./dir2/f1 -> /dir1/dir2/f1
what ever the filesystem looks like (non existing file or symlink)
and which is multiplatform of course
I'm really demanding but I just ask question before writing the code
myself from File::Spec splitpath, catpath, splitdir, catdir
If there is a simple existing way to do that, please just tell
me. Thank you in advance.
If you just want to check your ideas, here is a short test case:
my @testCase = (
"~/dir1/../dir2",
"~/dir1/../dir2/f1",
"~/dir1/./dir2/f1",
"/dir1/../dir2",
"/dir1/../dir2/f1",
"/dir1/./dir2/f1",
);
print "Result: " . join( " ", map { File::Spec->canonpath($_) } @testCase ) . "\n";
Result: ~/dir1/../dir2 ~/dir1/../dir2/f1 ~/dir1/dir2/f1 /dir1/../dir2 /dir1/../dir2/f1 /dir1/dir2/f1
Regards,
I'm looking for a way to clean file paths like the following:
~/dir1/../dir2
~/dir1/../dir2/f1
~/dir1/./dir2/f1
/dir1/../dir2
/dir1/../dir2/f1
/dir1/./dir2/f1
I tested Cwd->realpath that reduce ./ and ../ but has many drawbacks:
- it does not understand ~/
- it follows symbolic links
- it checks path over the filesystem, so an unexisting file becomes
undef.
Results I expect are obviously:
~/dir1/../dir2 -> ~/dir2
~/dir1/../dir2/f1 -> ~/dir2/f1
~/dir1/./dir2/f1 -> ~/dir1/dir2/f1
/dir1/../dir2 -> /dir2
/dir1/../dir2/f1 -> /dir2/f1
/dir1/./dir2/f1 -> /dir1/dir2/f1
what ever the filesystem looks like (non existing file or symlink)
and which is multiplatform of course
I'm really demanding but I just ask question before writing the code
myself from File::Spec splitpath, catpath, splitdir, catdir
If there is a simple existing way to do that, please just tell
me. Thank you in advance.
If you just want to check your ideas, here is a short test case:
my @testCase = (
"~/dir1/../dir2",
"~/dir1/../dir2/f1",
"~/dir1/./dir2/f1",
"/dir1/../dir2",
"/dir1/../dir2/f1",
"/dir1/./dir2/f1",
);
print "Result: " . join( " ", map { File::Spec->canonpath($_) } @testCase ) . "\n";
Result: ~/dir1/../dir2 ~/dir1/../dir2/f1 ~/dir1/dir2/f1 /dir1/../dir2 /dir1/../dir2/f1 /dir1/dir2/f1
Regards,