calling perl subroutine in shell script - perl

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'`;

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

Related

How to call a Perl program from a csh script

I have this simple code to execute, but have failed.
a.csh
#! /bin/csh -f
`perl a.pl`
a.pl
#use perl
eval 'exec perl -w -S $0 ${1+"$#"}'
if 0;
use Cwd;
print "here.\n";
When I run a.csh, it reports an error as below.
here: Command not found
I am not sure what that means; any suggestion is welcome.
The backticks in your program are returning the output of the perl script. Then csh tries to execute that output as csh. toolic's comment telling you to remove the backticks is correct: perl a.pl. Reading more about how backticks work might be useful.

Routine in module is Undefined subroutine in perl

I have just installed the Crypt::Random module as well as all the dependencies such as Math::Pari. There are three routines in Crypt::Random and I don't know why perl is calling them 'undefined subroutine'. Thanks to whoever knows what is wrong. Here are the routines (specifically defined in the module), and I chose small arguments for them to see if they work:
C:\Users\Jlinne\Documents>perl -MCrypt::Random -E "say makerandom(100)"
Undefined subroutine &main::makerandom called at -e line 1.
C:\Users\Jlinne\Documents>perl -MCrypt::Random -E "say makerandom_itv(1, 1000)"
Undefined subroutine &main::makerandom_itv called at -e line 1.
C:\Users\Jlinne\Documents>perl -MCrypt::Random -E "say makerandom_octet(10)"
Undefined subroutine &main::makerandom_octet called at -e line 1.
Crypt::Random does not export any methods by default.
Instead you must explicitly import them:
$ perl -MCrypt::Random=makerandom -E "say makerandom(100)"
$ perl -MCrypt::Random=makerandom_itv -E "say makerandom_itv(1, 1000)"
$ perl -MCrypt::Random=makerandom_octet -E "say makerandom_octet(10)"

No run time & compile time error in Perl

I am trying to execute the following small script.
test.pl
#!/bin/perl
use strict;
$value = 1;
The output is
perl -c test.pl
test.pl syntax OK
Please let me know why it is not throwing error?
Thanks in advance.
What version of Perl are you using? This is Perl 5.18:
$ cat test1.pl
#!/bin/perl
use strict;
$value = 1;
$ perl -c test1.pl
Global symbol "$value" requires explicit package name at test1.pl line 3.
test1.pl had compilation errors.
I get the error.
Is it possible you're not running against the Perl script you think you are?

Access perl module within Bash script

I have a perl module with data definitions (hashes, arrays, etc.), is there any way I can access that data from inside a bash script? This isn't working for me...
#!/bin/bash
perl -e 'use Data'
tests=`perl -e "#tests"; `
echo "Perl tests = ${tests}" # prints "Perl tests = "
The module looks something like this:
our #EXPORT_OK = qw( #tests );
our #tests = qw( 1 2 3 4 5 );
If you have package variable #tests inside Data module,
perl -MData -e 'print "$_\n" for #Data::tests'
For perl 5.10 and above,
perl -MData -E 'say for #Data::tests'
You can use a module from the command line with -M
perl -MData -e'print map {"$_\n"} #tests;'
In the code you give, you run one interpreter that loads Data. It exits. You then run a second interpreter, which prints #tests. As that's the only action the second interpreter has performed, it's uninitialized, and prints nothing.

Error: Can't locate File/HomeDir.pm in #INC

I use a Mac OS X 10.8, and want to run the latexindent.pl perl script on my latex files. This script is made by https://github.com/cmhughes/latexindent.plx. When I run this script on a latexfile I get this error message:
Can't locate File/HomeDir.pm in #INC (#INC contains: /Library/Perl/5.12/darwin-thread-multi-2level /Library/Perl/5.12 /Network/Library/Perl/5.12/darwin-thread-multi-2level /Network/Library/Perl/5.12 /Library/Perl/Updates/5.12.4/darwin-thread-multi-2level /Library/Perl/Updates/5.12.4 /System/Library/Perl/5.12/darwin-thread-multi-2level /System/Library/Perl/5.12 /System/Library/Perl/Extras/5.12/darwin-thread-multi-2level /System/Library/Perl/Extras/5.12 .) at helloworld.pl line 10.
BEGIN failed--compilation aborted at helloworld.pl line 10.
It seems that I am missing the File::HomeDir module in perl so I tried to download it using:
sudo perl -MCPAN -e 'install File::HomeDir',
I get the following error:
Can't locate object method "install" via package "File::HomeDir".
I managed to install the YAML::Tiny package without any problems.
I tried to run:
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use FindBin;
6 use YAML::Tiny;
7 use File::Copy;
8 use File::Basename;
9 use Getopt::Std;
10 use File::HomeDir;
11
12 print "hello world";
13 exit;
and got the same Error message as above...
Do anyone know what to do?
sudo perl -MCPAN -e 'install "File::HomeDir"'
^ ^
I had a similar problem. I just ran these commends, and it worked for me
sudo cpan -i File::HomeDir
then
sudo cpan -i Unicode::GCString
Basically, I will run latexindent file_name.tex, then an error message will appear that says you may need to install the XXXX module then I install the XXXX module using sudo cpan -i XXXX