質問

いPerlスクリプトを見極める必要があるファイル名のフルパスとファイル名のスクリプト中で実行します。という発見によって呼び出すスクリプト $0 異なり、が含まれていることがあり、 fullpath+filename もう filename.での作業ディレクトリでも変化できない方法を考えなければなり確実に取得する fullpath+filename のスクリプト

誰でもって解決するのか

役に立ちましたか?

解決

あしながら、その方法:

  • $0 は、現在実行中のスクリプトとして提供POSIXには、現在の作業ディレクトリの場合はスクリプトは以下CWD
  • また、 cwd(), getcwd()abs_path() により規定されている Cwd モジュールだが、スクリプトが行われているから
  • このモジュール FindBin を提供 $Bin & $RealBin 変数 通常 のパスは、実行中のスクリプトこのモジュールも提供 $Script & $RealScript その名前をスクリプト
  • __FILE__ では、実際のファイルこのPerlインタプリタを扱時の編成を含む完全なパスです。

見ているが、第三$0, は、 Cwd モジュール FindBin モジュール)による不具合が発生した場合 mod_perl 筆者は、役に立た出力など '.' または空の文字列が返されます。このような雰囲気の場合は、利用 __FILE__ とのパスを取得してからその利用 File::Basename モジュール:

use File::Basename;
my $dirname = dirname(__FILE__);

他のヒント

$0は通常のプログラムできるようになりました。

use Cwd 'abs_path';
print abs_path($0);

しんでいただけるようにすることabs_path知をご利用の場合相対または絶対パスです。

更新 誰でもこれを読んで、読んでおくべきで描きの答えします。のではないのです。

Use File::Spec;
File::Spec->rel2abs( __FILE__ );

http://perldoc.perl.org/File/Spec/Unix.html

と思い、モジュール探しでしたがFindBin:

#!/usr/bin/perl
use FindBin;

$0 = "stealth";
print "The actual path to this is: $FindBin::Bin/$FindBin::Script\n";

を使用できる FindBin, Cwd, ファイル::Basename, 組み合わせます。それらはすべての拠点の分布Perl IIRC.

使用したCwdの過去

Cwd:

use Cwd qw(abs_path);
my $path = abs_path($0);
print "$path\n";

の絶対パス $0 または __FILE__ あると考えている。唯一のトラブルがあった chdir()$0 した相対--それは絶対パスを BEGIN{} めの防止に驚くことで生きています。

FindBin を行い、grovelの $PATH 何かに一致する basename($0), が、時があるかも"私が長崎に来る度に雨が降(具体的には:ファイルが"歩く"のcwd.)

File::FuFile::Fu->program_nameFile::Fu->program_dir ます。

一部の短背景:

残念ながらのUnix APIはどんなプログラムをフルパス実行形式にまとめたものです。このプログラムの実行を自分でいたい分野が正常に行えるプログラムです。があり、全ての答えは、様々な問題れます。がないにもかかわらず全体を検索ファイルシステムは常に仕事を行なっているのでまだ実行に移動または削除されます。

ながらも手放したくないのPerl実行可能であるか実際に走行すが、スクリプトです。とPerlのニーズを知り、スクリプトでティへ簡単にアクセスできます。それは、この __FILE__, な $0 ですから、UnixのAPIとなります。ここでは相対パスでマークの提案とcanonizeで File::Spec->rel2abs( __FILE__ );

してください:

$ENV{'SCRIPT_NAME'}

または

use FindBin '$Bin';
print "The script is located in $Bin.\n";

でもどう生かすかにかかっていられているというものCGIは行われているから通常のシェルなど。

を取得するためのディレクトリのパスを含む私のスクリプトを使って組み合わせの回答されています。

#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;
use File::Basename;

my $dir = dirname(File::Spec->rel2abs(__FILE__));

perlfaq8 回答が非常に似ているか、というの rel2abs() 機能 $0.この機能はファイル::仕様

また外部モジュールと一線できるファイル名は相対パスです。ご利用の場合はモジュールを適応させる必要があるからの相対パスのスクリプトディレクトリの相対パスは必要なものではありません。

$0 =~ m/(.+)[\/\\](.+)$/;
print "full path: $1, file name: $2\n";
#!/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

さんがあるのではないでしょうか?

my $thisfile = $1 if $0 =~
/\\([^\\]*)$|\/([^\/]*)$/;

print "You are running $thisfile
now.\n";

出力のようなものです:

You are running MyFileName.pl now.

を作成するための製品は、Windows、Unix.

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 

の問題 __FILE__ するということが印刷のコアモジュール".〉"オプションコードをどうぞ"パスは必ずしもの".cgi"または".pl"スクリプトパスが走っています。うことなんでしょうけれどによって異なり目標です。

るように思えま Cwd で更新する必要があるということfor mod_perl.ここでは私の提案:

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;

ください追加ご提案します。

ずに外部モジュールの場合シェルの作品もは'../':

my $self = `pwd`;
chomp $self;
$self .='/'.$1 if $0 =~/([^\/]*)$/; #keep the filename only
print "self=$self\n";

試験:

$ /my/temp/Host$ perl ./host-mod.pl 
self=/my/temp/Host/host-mod.pl

$ /my/temp/Host$ ./host-mod.pl 
self=/my/temp/Host/host-mod.pl

$ /my/temp/Host$ ../Host/./host-mod.pl 
self=/my/temp/Host/host-mod.pl

の問題だけを使用 dirname(__FILE__) なフォロsymlinks.使いいただけるこの私のスクリプトのシンボリックリンクを実際のファイルの場所です。

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

すべての図書館-フリーソリューションになった以上の数の書き方にパス(../または/bla/x/../bin/./x/..することになりました。私の溶液は下のようになります。ませていただきました。ゴ:いfaintestうため、私の交換。私が取得し、偽り"./" や"../".万谷塘文化公園などの見所もそうでなかなか強いです。

  my $callpath = $0;
  my $pwd = `pwd`; chomp($pwd);

  # if called relative -> add pwd in front
  if ($callpath !~ /^\//) { $callpath = $pwd."/".$callpath; }  

  # do the cleanup
  $callpath =~ s!^\./!!;                          # starts with ./ -> drop
  $callpath =~ s!/\./!/!g;                        # /./ -> /
  $callpath =~ s!/\./!/!g;                        # /./ -> /        (twice)

  $callpath =~ s!/[^/]+/\.\./!/!g;                # /xxx/../ -> /
  $callpath =~ s!/[^/]+/\.\./!/!g;                # /xxx/../ -> /   (twice)

  my $calldir = $callpath;
  $calldir =~ s/(.*)\/([^\/]+)/$1/;

なし"に"答えは正しかった。題につFindBin'$Bin'Cwdは、その絶対パスを返しすべてのシンボリックリンクを解消します。私の場合は、正確なパスをシンボリックリンクを現在のものと同じで返しますUnixコマンド"pwd"ではなく、"pwd-P".以下の機能を解

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));
}

何が悪い $^X ?

#!/usr/bin/env perl<br>
print "This is executed by $^X\n";

てくださいフルパスPerlバイナリを使っています。

Evert

には*nixり、の"whereis"コマンドは、検索お$PATHを探してバイナリを指定した名前です。ま$0が含まれていないファイル名のフルパス名、whereis$scriptname存の結果を変数にすべきだが、スクリプトがあります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top