在某些情况下,您需要确定Perl的绝对路径名
模块,但您不需要加载Perl模块:
模块,但您不需要加载Perl模块:
use strict; use warnings; my $mod_name = 'My::Module'; my $abs_path = mod_name_to_abs_path( $mod_name ); sub mod_name_to_abs_path { my ( $mod_name ) = @_; my $rel_fn = $mod_name =~ s{::}{/}gr; $rel_fn .= '.pm'; require $rel_fn; return $INC{$rel_fn}; }
上面的代码加载模块(带require).
如何在不使用require的情况下确定模块的绝对路径名?
解决方法
Module::Util
模块提供find_installed函数,它可以完成我认为您需要的功能.
还有面向对象的Module::Info
和Module::Data
模块,它们将做一些表面上相似的事情
该程序显示了所有这三个的使用
use strict; use warnings 'all'; use feature 'say'; use Module::Util 'find_installed'; use Module::Info (); use Module::Data (); say find_installed('Module::Util'); say Module::Info->new_from_module('Module::Info')->file; say Module::Data->new('Module::Data')->path;
产量
C:\Strawberry\perl\site\lib\Module\Util.pm C:\Strawberry\perl\site\lib\Module\Info.pm C:\Strawberry\perl\site\lib\Module\Data.pm