Routine in module is Undefined subroutine in perl - 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)"

Related

Force Perl to stop special processing of command line arguments if -e eval switch is used

perl -e 'print(123, #ARGV);' a b
# 123ab
perl -e 'print(123, #ARGV);' --help
# prints Perl's help instead
This is a toy example demonstrating the problem. In my real use-case I'm using -e to execute a large script from an embedded interpreter using perl_parse(...) function, the script has its own processing of --help switch, so I'd like to block any special processing of command line arguments after -e.
Is it possible?
Use a double hyphen to stop argument processing:
$ perl -e'print "[#ARGV]\n"' -- --help
[--help]
$

using slash in Perl Command

Below the command i want to run.
perl -pi -w -e 's//apps/LIVE/appl/xx/11.5.0//$XXTOP//g;' prog.txt
Here, source and replacement both have slashes in them.
How to handle this?
--
Update
I tried with curly braces and tilde that was suggested.
perl -pi -w -e 's{apps/LIVE/appl/xx/11.5.0}{$XXTOP}g;' prog.txt
In this case, Dollar sign in giving issue, else it works fine..
Error:
Name "main::XXTOP" used only once: possible typo at -e line 1.
Use of uninitialized value at -e line 1, <> chunk 1.
Use of uninitialized value at -e line 1, <> chunk 2.
Use another delimiter:
perl -pi -w -e 's~/apps/LIVE/appl/xx/11.5.0/~$XXTOP/~g;' prog.txt
You could access environment variable thru %ENV, like:
perl -pi -w -e 's~/apps/LIVE/appl/xx/11.5.0/~$ENV{XXTOP}/~g;' prog.txt
Do you want the text $XXTOP to appear in the output. In which case escape the $ - {\$XXTOP}
perl -pi -w -e 's{apps/LIVE/appl/xx/11.5.0}{\$XXTOP}g;' prog.txt
Perl is thinking that $XXTOP is a Perl variable.
Of if you are want to expand an environment variable, use $ENV{$XXTOP}
solved:
Solution:
perl -pi -w -e 's{apps/LIVE/appl/xx/11.5.0}{\$XXTOP}g;' prog.txt

What's the point of blessed in Perl?

[root# ~]$ perl -e "print 1 if blessed $a;"
1
[root# ~]$ perl -e "print 1 if blessed $c;"
1
[root# ~]$ perl -e "print 1 if blessed $cee;"
1
It seems always true,the version is 5.8.8.
UPDATE
I'm not running as root, it's CHANGED by me for the sake of privacy:)
blessed is not a keyword in Perl. You are using double quotes in your shell command, so the variables ($a, $c, etc.) are from your shell's environment, they are not Perl variables.
Since these environment variables are probably empty, you are essentially executing the script
print 1 if blessed ;
When used like this, blessed is just a bareword string and always evaluates to true. What you have done is not much difference from running
$ perl -e 'print 1 if foo'
Do you mean blessed from Scalar::Util? You probably want to load the function first:
perl -MScalar::Util=blessed -e "print 1 if blessed $a;"
otherwise your blessed is just bareword (string), which is obviously true.
As has been pointed out, you need to load the module before using the method. Also, if you had used perl -we instead of perl -e, you would probably not have asked this question.
For me, with perl -we, I get this warning:
Can't call method "blessed" without a package or object reference at -e line 1.

perl doing different things on two different platforms

On Mac OSX, this works fine with perl
perl -v
This is perl, v5.8.9 built for darwin-2level
perl -e 'sub test {}'
But on Solaris
perl -v
This is perl, v5.8.8 built for i86pc-solaris-thread-multi
perl -e 'sub test {}'
Illegal declaration of anonymous subroutine at -e line 1.
Any ideas?
Thanks,
Kelly
It's most likely a behavior difference between the two versions of Perl. It's also probably just a bug in the CLI evaluation mode in 5.8.8
Try this test to see if it's just the CLI evaluation or Perl itself:
use strict;
sub test {}
If it passes strict mode in a file, it's probably as good as it's gonna get.
perldoc perldiag says:
Illegal declaration of anonymous subroutine
(F) When using the sub keyword to construct an anonymous subroutine,
you must always specify a block of code. See perlsub
It's could possibly be in a sitecustomize.pl file. It's not seeing the "test". It's reading it as 'sub'. Try typing perl -e 'test {}' on the command line.
Also to take out the customization file, you could add the -f switch to the command line. `perl -fe 'sub test {}'
perldoc perlrun for more information.

calling perl subroutine in shell script

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