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

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

    use strict ; use warnings ; use Cwd 'abs_path';
        sub ResolveMyProductBaseDir { 
    
            # Start - Resolve the ProductBaseDir
            #resolve the run dir where this scripts is placed
            my $ScriptAbsolutPath = abs_path($0) ; 
            #debug print "\$ScriptAbsolutPath is $ScriptAbsolutPath \n" ;
            $ScriptAbsolutPath =~ m/^(.*)(\\|\/)(.*)\.([a-z]*)/; 
            $RunDir = $1 ; 
            #debug print "\$1 is $1 \n" ;
            #change the \'s to /'s if we are on Windows
            $RunDir =~s/\\/\//gi ; 
            my @DirParts = split ('/' , $RunDir) ; 
            for (my $count=0; $count < 4; $count++) {   pop @DirParts ;     }
            my $ProductBaseDir = join ( '/' , @DirParts ) ; 
            # Stop - Resolve the ProductBaseDir
            #debug print "ResolveMyProductBaseDir $ProductBaseDir is $ProductBaseDir \n" ; 
            return $ProductBaseDir ; 
        } #eof sub 
    

提交回复
热议问题