How do I get the full path to a Perl script that is executing?

后端 未结 23 2953
情深已故
情深已故 2020-11-28 19:29

I have Perl script and need to determine the full path and filename of the script during execution. I discovered that depending on how you call the script $0 va

23条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 20:04

    None of the "top" answers were right for me. The problem with using FindBin '$Bin' or Cwd is that they return absolute path with all symbolic links resolved. In my case I needed the exact path with symbolic links present - the same as returns Unix command "pwd" and not "pwd -P". The following function provides the solution:

    sub get_script_full_path {
        use File::Basename;
        use File::Spec;
        use Cwd qw(chdir cwd);
        my $curr_dir = cwd();
        chdir(dirname($0));
        my $dir = $ENV{PWD};
        chdir( $curr_dir);
        return File::Spec->catfile($dir, basename($0));
    }
    

提交回复
热议问题