如何使用 -M 标志从命令行使用其相对路径加载 perl 模块?



我有以下代码: demo.pm

#! user/local/bin/perl
use strict;
use warnings;
package demo;
sub doit{
print("Inside DoItn");
my $a = shift();
my $b = shift();
print("$an");
}

文件 demo.pm 位于其他位置,并希望使用相对路径来获取模块并运行它

从 linux 命令使用以下会引发错误:

perl -M/sub1/sub2/demo.pm -e 'demo::doit('arg1')'

错误:

syntax error at -e line 0, near "use /sub1/"
Execution of -e aborted due to compilation errors.

如果你有一个绝对路径,并且你希望所有脚本都找到该模块:

export PERL5LIB="${PERL5LIB:+"$PERL5LIB:"}/path/to/lib"  # In your login script.
script

如果你有一个绝对路径,并且你想要一个脚本的执行来查找模块:

PERL5LIB="${PERL5LIB:+"$PERL5LIB:"}/path/to/lib" script

如果模块安装在相对于脚本的目录中:

use FindBin qw( $RealBin );
use lib "$RealBin/../lib";

在您的具体情况中:

perl -Mlib=/sub1/sub2 -Mdemo -e'demo::doit("arg1")'

避免使用-I。它不像PERL5LIB和 lib.pm 那样包含相关的架构目录,因此具有特定于架构的组件的模块将无法加载。

最新更新