简体   繁体   English

如何使用-M标志通过命令行中的相对路径加载Perl模块?

[英]How do i use -M flag to load a perl module using its relative path from command line?

I have following code: demo.pm 我有以下代码:demo.pm

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

The file demo.pm is located elsewhere and want to use relative path to fetch the module and run it 文件demo.pm位于其他位置,想要使用相对路径来获取模块并运行它

Using following from linux command throws error: perl -M/sub1/sub2/demo.pm -e 'demo::doit('arg1')' 在Linux命令中使用以下命令会引发错误:perl -M / sub1 / sub2 / demo.pm -e'demo :: doit('arg1')'

Error: 错误:
syntax error at -e line 0, near "use /sub1/" Execution of -e aborted due to compilation errors. -e第0行,“ use / sub1 /”附近的语法错误-e的执行由于编译错误而中止。

If you have an absolute path and you want all scripts to find the module: 如果您有绝对路径,并且希望所有脚本都找到该模块:

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

If you have an absolute path and you want one execution of a script to find the module: 如果您有绝对路径,并且希望执行一次脚本来查找模块:

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

If you have a module that's installed in a directory relative to a script: 如果您有一个模块安装在相对于脚本的目录中:

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

In your specific case: 在您的特定情况下:

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

Avoid using -I . 避免使用-I It doesn't include related arch dirs like PERL5LIB and lib.pm do, so modules with arch-specific components will fail to load. 它不包括诸如PERL5LIB和lib.pm这样的相关PERL5LIB Dirs,因此具有特定于Arch的组件的模块将无法加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM