在Perl中,如何获取当前执行代码的目录或路径?

时间:2022-05-19 07:23:10

If I am in some library code, how do I determine the path to the file of the code that is currently executing? I know how to get the path of the top perl file by looking at ARGV, but if I load a library, how can that library know which path it is at?

如果我在某些库代码中,如何确定当前正在执行的代码文件的路径?我知道如何通过查看ARGV来获取*perl文件的路径,但是如果我加载一个库,那么该库如何知道它所在的路径?

5 个解决方案

#1


16  

The __FILE__ token will give you the full path including the file name. You can use File::Spec to split it into components:

__FILE__标记将为您提供包含文件名的完整路径。您可以使用File :: Spec将其拆分为组件:

my ($volume, $directory, $file) = File::Spec->splitpath(__FILE__);

#2


13  

The easiest way to find the filename of the current executable is with FindBin:

查找当前可执行文件的文件名的最简单方法是使用FindBin:

use FindBin;
use File::Spec;

print "the directory of my script is: " . $FindBin::Bin . "\n";
print "the base name of my script is: " . $FindBin::Script . "\n";
print "the canonical location of my script is: " . File::Spec->catfile($FindBin::Bin, $FindBin::Script) . "\n";

Internally, you can get at some of this information by looking at $0 (the name of the script as invoked at the command line), and __FILE__, which is the name of the currently-executing file. (See perldoc perlvar.)

在内部,您可以通过查看$ 0(在命令行调用的脚本名称)和__FILE__(这是当前正在执行的文件的名称)来获取其中的一些信息。 (见perldoc perlvar。)

To extract the filename of the currently-executing module, start with examining __PACKAGE__, do some substitution magic and then look up the filename in %INC:

要提取当前正在执行的模块的文件名,首先检查__PACKAGE__,执行一些替换魔术,然后在%INC中查找文件名:

(my $filename = __PACKAGE__ ) =~ s#::#/#g;
$filename .= '.pm';
my $abs_filename = $INC{$filename};

I do this in one of my initialization libraries to find a configuration script in a path relative to the current module (I have several code branches installed side-by-side, each with slightly different configs):

我在我的一个初始化库中执行此操作,以在相对于当前模块的路径中查找配置脚本(我有几个并排安装的代码分支,每个代码分支都有略微不同的配置):

# use the location of the current module as a guide for where to find configs
(my $filename = __PACKAGE__ ) =~ s#::#/#g;
$filename .= '.pm';

(my $path = $INC{$filename}) =~ s#/\Q$filename\E$##g; # strip / and filename
my $abs_config_file = File::Spec->catfile($path, $config_file);
MyApp->initialize($abs_config_file);

#3


1  

Any libraries included via use or require produce an entry in the special %INC hash. (See perlvar).

通过use或require包含的任何库都会在特殊的%INC哈希中生成一个条目。 (见perlvar)。

For example:

例如:

use strict;
use warnings;

use Data::Dumper;

print Dumper \%INC;

This will produce output similar to the following:

这将产生类似于以下的输出:

$VAR1 = {
      'warnings/register.pm' => '/usr/lib/perl5/5.8.8/warnings/register.pm',
      'bytes.pm' => '/usr/lib/perl5/5.8.8/bytes.pm',
      'XSLoader.pm' => '/usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/XSLoader.pm',
      'Carp.pm' => '/usr/lib/perl5/5.8.8/Carp.pm',
      'Exporter.pm' => '/usr/lib/perl5/5.8.8/Exporter.pm',
      'strict.pm' => '/usr/lib/perl5/5.8.8/strict.pm',
      'warnings.pm' => '/usr/lib/perl5/5.8.8/warnings.pm',
      'overload.pm' => '/usr/lib/perl5/5.8.8/overload.pm',
      'Data/Dumper.pm' => '/usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/Data/Dumper.pm'
    };

Additionally, there is a universal __FILE__ constant which will return the current filename. (See also __PACKAGE__).

此外,还有一个通用__FILE__常量,它将返回当前文件名。 (另见__PACKAGE__)。

#4


1  

This is the snippet I usually use to get the path for the executing code.

这是我通常用来获取执行代码的路径的片段。

use Cwd qw/abs_path/;
my ($real_path) = abs_path($0) =~ m/(.*)myscript.pl/i;

This puts the actual directory path into $real_path. I usually also perform chdir $real_path after this to make sure my code is actually working out of the directory it should be (usually when I'm writing a Windows service using Win32::Daemon).

这会将实际目录路径放入$ real_path。我通常也会在此之后执行chdir $ real_path,以确保我的代码实际上是在应该的目录之外(通常在我使用Win32 :: Daemon编写Windows服务时)。

The abs_path subroutine I've exported gives the path to whatever file (name/handle) you supply as the argument. In this case I've supplied $0 which is the name of the Perl script being executed.

我导出的abs_path子例程给出了作为参数提供的任何文件(名称/句柄)的路径。在这种情况下,我提供了$ 0,这是正在执行的Perl脚本的名称。

I would recommend checking out Cwd on Cpan for a little more guidance.

我建议在Cpan上查看Cwd以获得更多指导。

#5


-4  

Easiest way:

最简单的方法:

print $ENV{'PWD'};

#1


16  

The __FILE__ token will give you the full path including the file name. You can use File::Spec to split it into components:

__FILE__标记将为您提供包含文件名的完整路径。您可以使用File :: Spec将其拆分为组件:

my ($volume, $directory, $file) = File::Spec->splitpath(__FILE__);

#2


13  

The easiest way to find the filename of the current executable is with FindBin:

查找当前可执行文件的文件名的最简单方法是使用FindBin:

use FindBin;
use File::Spec;

print "the directory of my script is: " . $FindBin::Bin . "\n";
print "the base name of my script is: " . $FindBin::Script . "\n";
print "the canonical location of my script is: " . File::Spec->catfile($FindBin::Bin, $FindBin::Script) . "\n";

Internally, you can get at some of this information by looking at $0 (the name of the script as invoked at the command line), and __FILE__, which is the name of the currently-executing file. (See perldoc perlvar.)

在内部,您可以通过查看$ 0(在命令行调用的脚本名称)和__FILE__(这是当前正在执行的文件的名称)来获取其中的一些信息。 (见perldoc perlvar。)

To extract the filename of the currently-executing module, start with examining __PACKAGE__, do some substitution magic and then look up the filename in %INC:

要提取当前正在执行的模块的文件名,首先检查__PACKAGE__,执行一些替换魔术,然后在%INC中查找文件名:

(my $filename = __PACKAGE__ ) =~ s#::#/#g;
$filename .= '.pm';
my $abs_filename = $INC{$filename};

I do this in one of my initialization libraries to find a configuration script in a path relative to the current module (I have several code branches installed side-by-side, each with slightly different configs):

我在我的一个初始化库中执行此操作,以在相对于当前模块的路径中查找配置脚本(我有几个并排安装的代码分支,每个代码分支都有略微不同的配置):

# use the location of the current module as a guide for where to find configs
(my $filename = __PACKAGE__ ) =~ s#::#/#g;
$filename .= '.pm';

(my $path = $INC{$filename}) =~ s#/\Q$filename\E$##g; # strip / and filename
my $abs_config_file = File::Spec->catfile($path, $config_file);
MyApp->initialize($abs_config_file);

#3


1  

Any libraries included via use or require produce an entry in the special %INC hash. (See perlvar).

通过use或require包含的任何库都会在特殊的%INC哈希中生成一个条目。 (见perlvar)。

For example:

例如:

use strict;
use warnings;

use Data::Dumper;

print Dumper \%INC;

This will produce output similar to the following:

这将产生类似于以下的输出:

$VAR1 = {
      'warnings/register.pm' => '/usr/lib/perl5/5.8.8/warnings/register.pm',
      'bytes.pm' => '/usr/lib/perl5/5.8.8/bytes.pm',
      'XSLoader.pm' => '/usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/XSLoader.pm',
      'Carp.pm' => '/usr/lib/perl5/5.8.8/Carp.pm',
      'Exporter.pm' => '/usr/lib/perl5/5.8.8/Exporter.pm',
      'strict.pm' => '/usr/lib/perl5/5.8.8/strict.pm',
      'warnings.pm' => '/usr/lib/perl5/5.8.8/warnings.pm',
      'overload.pm' => '/usr/lib/perl5/5.8.8/overload.pm',
      'Data/Dumper.pm' => '/usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/Data/Dumper.pm'
    };

Additionally, there is a universal __FILE__ constant which will return the current filename. (See also __PACKAGE__).

此外,还有一个通用__FILE__常量,它将返回当前文件名。 (另见__PACKAGE__)。

#4


1  

This is the snippet I usually use to get the path for the executing code.

这是我通常用来获取执行代码的路径的片段。

use Cwd qw/abs_path/;
my ($real_path) = abs_path($0) =~ m/(.*)myscript.pl/i;

This puts the actual directory path into $real_path. I usually also perform chdir $real_path after this to make sure my code is actually working out of the directory it should be (usually when I'm writing a Windows service using Win32::Daemon).

这会将实际目录路径放入$ real_path。我通常也会在此之后执行chdir $ real_path,以确保我的代码实际上是在应该的目录之外(通常在我使用Win32 :: Daemon编写Windows服务时)。

The abs_path subroutine I've exported gives the path to whatever file (name/handle) you supply as the argument. In this case I've supplied $0 which is the name of the Perl script being executed.

我导出的abs_path子例程给出了作为参数提供的任何文件(名称/句柄)的路径。在这种情况下,我提供了$ 0,这是正在执行的Perl脚本的名称。

I would recommend checking out Cwd on Cpan for a little more guidance.

我建议在Cpan上查看Cwd以获得更多指导。

#5


-4  

Easiest way:

最简单的方法:

print $ENV{'PWD'};