I am looking for simple & proper way to obtain absolute path of symlink's immediate target (not final target), in perl.
My Example Tree:
|-- dir1
| |-- link1 -> ../dir2/link2
|-- dir2
| `-- link2 -> ../dir3/file.txt
|-- dir3
| `-- file.txt
`-- test
`-- script.pl
Code1:
my $link = dirname($ARGV[0]).'/'.readlink($ARGV[0]);
print rel2abs($link), "\n";
Cmd: ./script.pl ../dir1/link1
Result: /tmp/perl_script_test/test/../dir1/../dir2/link2
Expected: /tmp/perl_script_test/dir2/link2
I am looking for absolute path of symlink's immediate target without ../
I tried abs_path(), realpath() but they all give path to final target (file.txt)
Is there any build-in function that does what I want to do I need to split the path and do manual concatenation ?
--
Mohan G
File::Path's
absolutedoesn't do any file system checks.Say the CWD is
/tmp/perl_script_test/testSay the CWD is
/tmp/perl_script_testYou've added to the following to your question:
You can't safely do that without possibly resolving symlinks. For example,
is not necessarily equivalent to
because
could be a symlink.
The following might be sufficient for you:
Say the CWD is
/tmp/perl_script_test/testYou will only get the above output if none of the following are symlinks:
(You could make it work even if the first two are symlinks, but that would take even more work.)