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

后端 未结 23 2963
情深已故
情深已故 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:54

    The problem with __FILE__ is that it will print the core module ".pm" path not necessarily the ".cgi" or ".pl" script path that is running. I guess it depends on what your goal is.

    It seems to me that Cwd just needs to be updated for mod_perl. Here is my suggestion:

    my $path;
    
    use File::Basename;
    my $file = basename($ENV{SCRIPT_NAME});
    
    if (exists $ENV{MOD_PERL} && ($ENV{MOD_PERL_API_VERSION} < 2)) {
      if ($^O =~/Win/) {
        $path = `echo %cd%`;
        chop $path;
        $path =~ s!\\!/!g;
        $path .= $ENV{SCRIPT_NAME};
      }
      else {
        $path = `pwd`;
        $path .= "/$file";
      }
      # add support for other operating systems
    }
    else {
      require Cwd;
      $path = Cwd::getcwd()."/$file";
    }
    print $path;
    

    Please add any suggestions.

提交回复
热议问题