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

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

    #!/usr/bin/perl -w
    use strict;
    
    
    my $path = $0;
    $path =~ s/\.\///g;
    if ($path =~ /\//){
      if ($path =~ /^\//){
        $path =~ /^((\/[^\/]+){1,}\/)[^\/]+$/;
        $path = $1;
        }
      else {
        $path =~ /^(([^\/]+\/){1,})[^\/]+$/;
        my $path_b = $1;
        my $path_a = `pwd`;
        chop($path_a);
        $path = $path_a."/".$path_b;
        }
      }
    else{
      $path = `pwd`;
      chop($path);
      $path.="/";
      }
    $path =~ s/\/\//\//g;
    
    
    
    print "\n$path\n";
    

    :DD

提交回复
热议问题