我想获取包含模块的完整文件名。考虑这段代码:
package MyTest;
my $path = join '/', split /::/, __PACKAGE__;
$path .= ".pm";
print "$INC{$path}\n";
1;
$ perl -Ipath/to/module -MMyTest -e0
path/to/module/MyTest.pm
它可以在所有平台上运行吗?
perlvar
The hash
%INC
contains entries for each filename included via thedo
,require
, oruse
operators. The key is the filename you specified (with module names converted to pathnames), and the value is the location of the file found.
这些键是否依赖于平台?我应该使用
File::Spec
要不然是啥?至少在 win32 上的 ActivePerl 使用 /
而不是 \
.更新:
%INC
呢?值(value)观?它们是否依赖于平台?
最佳答案
鉴于它是标准模块,请使用 Module::Loaded 中的方法:
sub is_loaded (*) {
my $pm = shift;
my $file = __PACKAGE__->_pm_to_file( $pm ) or return;
return $INC{$file} if exists $INC{$file};
return;
}
sub _pm_to_file {
my $pkg = shift;
my $pm = shift or return;
my $file = join '/', split '::', $pm;
$file .= '.pm';
return $file;
}
关于perl - %INC 的键和值是否依赖于平台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2318746/