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

后端 未结 23 2781
情深已故
情深已故 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 19:43

    The problem with just using dirname(__FILE__) is that it doesn't follow symlinks. I had to use this for my script to follow the symlink to the actual file location.

    use File::Basename;
    my $script_dir = undef;
    if(-l __FILE__) {
      $script_dir = dirname(readlink(__FILE__));
    }
    else {
      $script_dir = dirname(__FILE__);
    }
    

提交回复
热议问题