How can I get the target of a symlink in Perl 6?
Asked Answered
O

2

9

You can create a symlink in Perl 6:

my $symlink-path = ...;
$target.symlink: $symlink-path;

Given just the symlink how can you get the target path?

$symlink-path.IO.????

I'm looking for the exact string that is the target, not any interpretation of that (such as .resolve).

Overstate answered 11/6, 2018 at 23:52 Comment(0)
I
5

There is no equivalent in Perl 6 for that to my knowledge.

The closest thing to a solution, is to zef install P5readlink (https://modules.raku.org/dist/P5readlink) and use readlink like you would in Perl 5.

Irascible answered 12/6, 2018 at 15:27 Comment(0)
M
3

Method resolve works:

 my $target       = "../tmp/file".IO; 
 my $symlink-path = "files".IO;
 $target.symlink: $symlink-path;

 say $symlink-path.resolve;
 say $symlink-path.resolve(:completely) ~~ $target.resolve(:completely);
Molecule answered 12/6, 2018 at 3:32 Comment(3)
I want the target, not the interpreted target string. Note that :completely requires the target to exist. That's not a requirement of symlinks though. Furthermore, this has to work even if you don't have one of those components in your program.Overstate
resolve returns an IO.Path object that represents the target, not a string, which is what you asked for in the OP. :completely does not require the target to exist. It will resolve as much as possible, but throw eventually an exception, because you can't have an IO::Path of an object that does not actually exist.Tsushima
@Tsushima Aiui, when target exists, .resolve may work on both symlink and target and produce same path. But when you don't have a reference to the target and it does not exist on file system, then the current .resolve doesn't help. The exception thrown when :completely is specified only includes the .resolve invocant, i.e. the original symlink path, in the exception payload. It might be cool if it also or instead provided part it managed to resolve and part it failed on. But brian's saying that still does not cover his use case. (Lizmat's P5readlink seems like a good answer instead.)Hallerson

© 2022 - 2024 — McMap. All rights reserved.