3

I want to get the absolute path of the perl script I executed , for example

my current working directory is /home/Program/A/B and there is a perl script in /home/Program/A/sort.pl

When I under the directory of /home/Program/A/B and I type perl ../sort.pl , I want to get the

absolute path of sort.pl which is /home/Program/A/sort.pl

I mean no matter what my current working directory is , I want to get the absolute path of the perl

script is , How to achieve that?

thanks

user2131116
  • 2,761
  • 6
  • 26
  • 33
  • 1
    Do you want the script to be able to get its own path? Do you want the shell to tell you where the script is? Do you want to be able to run the script without having to give the path? – antiduh Jun 29 '13 at 02:33
  • @antiduh I know I should give the path so I can run the script ..But when I use indirect path to indicate where the script is , I want to get its absolute path ... – user2131116 Jun 29 '13 at 02:38
  • 1
    possible duplicate of [How do I get the full path to a Perl script that is executing?](http://stackoverflow.com/questions/84932/how-do-i-get-the-full-path-to-a-perl-script-that-is-executing) – EvilTeach Jun 29 '13 at 02:40
  • The **history** shell command will tell you what scripts you ran, but it will only provide the commands typed by the user and not the working directory or absolute names if only relative names were used. However, since you know part of the script name it should be possible to find the scripts again with the **which**, **locate**, or **find** shell commands. – Paul Jun 29 '13 at 02:59

2 Answers2

6

Here’s what you want:

use FindBin qw($RealScript);

That gives you the full path with all the symlinks fixed.

tchrist
  • 78,834
  • 30
  • 123
  • 180
4

The Cwd module has a useful function for doing this:

use Cwd qw(abs_path);
print abs_path(__FILE__), "\n";
dms
  • 797
  • 1
  • 5
  • 15