perl – 在不使用require的情况下确定模块的绝对路径
发布时间:2020-12-16 06:09:52 所属栏目:大数据 来源:网络整理
导读:在某些情况下,您需要确定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_
在某些情况下,您需要确定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函数,它可以完成我认为您需要的功能.
还有面向对象的 该程序显示了所有这三个的使用 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:StrawberryperlsitelibModuleUtil.pm C:StrawberryperlsitelibModuleInfo.pm C:StrawberryperlsitelibModuleData.pm (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |