gpt4 book ai didi

perl - %INC 的键和值是否依赖于平台?

转载 作者:行者123 更新时间:2023-12-04 15:37:55 27 4
gpt4 key购买 nike

我想获取包含模块的完整文件名。考虑这段代码:

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 the do, require, or useoperators. 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com