Zippy1970
Zippy1970

Reputation: 681

Perl: Get (unresolved) path of symbolic link

I have a perl script whose path is /scripts/original/ascript.pl

A symbolic link to this script also exists: /scripts/linked/ascript.pl

In ascript.pl, I need the path of where the script was called from (so either /scripts/original or /scripts/linked).

abs_path() always returns the resolved location:

use strict;
use Cwd qw(abs_path);

print abs_path($0); # Always prints /scripts/original/ascript.pl

How can I get the full unresolved path?

Upvotes: 1

Views: 323

Answers (1)

Håkon Hægland
Håkon Hægland

Reputation: 40748

You could use Cwd::getcwd() to get the unresolved path to script. But this has already been implemented in a more robust and general way in FindBin, so we do not have to reinvent the wheel:

use FindBin;
print '$Bin: ', $FindBin::Bin, "\n";
print '$Script: ', $FindBin::Script, "\n";

Output:

$Bin: /scripts/linked
$Script: ascript.pl

You check out the source of FindBin here.

Upvotes: 3

Related Questions