calling perl subroutine in shell script

2020-07-27 02:39发布

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

Returned Error;

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 failed--compilation aborted.

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

line 1:

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

1条回答
混吃等死
2楼-- · 2020-07-27 03:31

It's hard to say what went wrong not knowing what's in 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
查看更多
登录 后发表回答