在shell脚本中调用perl子例程 [英] calling perl subroutine in shell script

查看:75
本文介绍了在shell脚本中调用perl子例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个Perl模块MyModule.pm,它具有一些我想在shell脚本中调用的子例程getText. 我尝试按照以下方式进行操作,但是会出错;

i made a Perl Module MyModule.pm it has some subroutines getText which I wanted to call in a shell script. i tried following manner but it gives error;

SEC_DIR=`perl -MMyModule -e 'getText'`; # line 1
echo $SEC_DIR
exit 0

返回的错误;

Can't locate MyModule.pm in @INC (@INC contains: /usr/lib/perl5/5.10.0/x86_64-linux-thread-multi /usr/lib/perl5/5.10.0 /usr/lib/perl5/site_perl/5.10.0/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.10.0 /usr/lib/perl5/vendor_perl/5.10.0/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.10.0 /usr/lib/perl5/vendor_perl .).

BEGIN失败-编译中止.

BEGIN failed--compilation aborted.

PS:.pm文件和.sh位于同一位置. 我尝试过的一些其他选择;

PS: .pm file and .sh are at same location. some other options I tried;

SEC_DIR=`perl -MMyModule -e '&getText'`;
SEC_DIR=`perl -MMyModule -e 'use MyModule; getText'`;
SEC_DIR=`perl -e 'use MyModule; getText'`;

推荐答案

不知道MyModule.pm中的内容很难说出问题所在.

It's hard to say what went wrong not knowing what's in MyModule.pm.

@INC看起来不错(.在列表中,因此在当前目录中定位MyModule.pm应该没有问题).

@INC looks ok (. is in the list, so there should be no problem with locating MyModule.pm in current directory).

这是一个按照您描述的方式工作的最小示例.希望对您有所帮助.

Here's a minimal example that works the way you described. Hope it helps.

$ cat SomeModule.pm 
package SomeModule;

sub testsub
{
  return "it works\n";
}

1;
$ VAL=`perl -I. -MSomeModule -e 'print SomeModule::testsub'`
$ echo $VAL
it works

另一种加载模块的方法是:

Another way to load the module would be:

$ perl -e 'require "./SomeModule.pm"; print SomeModule::testsub()'
it works

这篇关于在shell脚本中调用perl子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆