Undefined subroutine &main::key_enc - perl

I have a Perl module named McuEnc.pm which is located at /home/eric/temp directory:
package McuEnc;
use 5.010;
use strict;
use warnings;
use Exporter qw(import);
our #EXPORT_OK = qw(key_enc data_enc data_dec);
sub key_enc { some code }
sub data_enc { some code }
sub data_dec { some code }
1;
I have a Perl script named test.pl which is also located at /home/eric/temp directory:
use 5.010;
use strict;
use warnings;
use McuEnc qw(key_enc data_enc data_dec);
key_enc("1111");
...
I have changed working directory to /home/eric/temp. Now if I run test.pl script, it runs correctly. But if I create a new directory /home/eric/temp/My and move the McuEnc.pm file into it and then modify the test.pl script as follwoing:
use 5.010;
use strict;
use warnings;
use My::McuEnc qw(key_enc data_enc data_dec);
key_enc("1111");
...
then I'm getting the following error when I run test.pl again:
Undefined subroutine &main::key_enc called at ./test line 6.
However, I can still call the subroutine correctly with:
McuEnc::key_enc("1111");
So why I'm getting the error message? what is wrong?

This was answered by Sobrique in the comments, but it was never posted as an official answer:
"The use statement doesn't match the package statement. Try package My::McuEnc instead"

Related

Perl package file Can't locate [duplicate]

This question already has answers here:
Doesn't Perl include current directory in #INC by default?
(3 answers)
Closed 4 years ago.
I have integrate sample function in perl. Everything working fine but i am not able to connect Perl module package file(.pm)
Please review below code
sample.pl
#!/usr/bin/perl
use strict;
use warnings;
use Sample;
print Sample->test_function();
Sample.pm
package Sample;
sub test_function
{
return 'Welcome';
}
Once after run the sample.pl file. It's return the error "Can't locate Sample.pm " but the package file availale in same folder.
The reason of the issue is installed perl module location could not found . add the below line your sample.pl file
use strict;
use warnings;
use Cwd qw( abs_path );
use File::Basename qw( dirname );
use lib dirname(abs_path($0));
use Sample;
print Sample->test_function();
First of all, your Sample.pm file must finish returning a true value. The common way to do that is appending 1; and the end of that file.
package Sample;
use strict;
use warnings;
sub test_function {
return 'Welcome';
}
1;
After Perl 5.26, current directory was removed from #INC. There are several ways to deal with this. For example, you can use the -I option when running the script:
perl -I. sample.pl
For a more deeper lecture on how to deal with #INC, please read this SO post.

Perl Module::Load fails

I tried to using Module::Load to dynamically load plugin from directory, but it doesn't work, even after maximal simplifing the code.
Main file:
#!/usr/bin/perl
use strict;
use warnings;
use Module::Load;
Module::Load::load_remote('Package', 'plugins/Blank.pm') or die;
plugins/Blank.pm:
#!/usr/bin/perl
package plugins::Blank;
use strict;
use warnings;
1;
But it still die. When i remove '...or die' part, it no dies, but Package still not loaded.
Module::Load::load_remote dies when failing, but does not return anything. Your example works fine for me, and the package is loaded. If it doesn't load the package it should die inside load_remote. load_remote only loads the functions that are exported into the other namespace. It also only seem to work if the other module is loaded as a module name, instead of a filename:
load.pl:
#!/usr/bin/perl
use strict;
use warnings;
use Module::Load;
Module::Load::load_remote("Package","plugins::Blank","hw");
Package::hw();
plugins/Blank.pm:
package plugins::Blank;
use strict;
use warnings;
use Exporter qw(import);
our #EXPORT_OK = qw(hw);
sub hw {
print "Hello World\n";
}
1;
Outputs:
Hello World
But to achieve what you want you can probably use an approach like this:
#!/usr/bin/perl
use strict;
use warnings;
use Module::Load;
my $pkg = 'plugins::Blank';
Module::Load::load($pkg);
$pkg->hw();

Perl throws undefined subroutine in module instead of correct error

I've moved part of a script into a module, and now the only error I get when I do something wrong is "Undefined subroutine", even when the real error is that I misspelled a variable, or forgot a closing paren, or left off a semi-colon.
The only way to find the real error is to copy the entire thing into a script and run it that way. It's very tedious. Am I doing something wrong, or is this just the way modules are supposed to work?
Here is a very simple example that shows the problem:
Module:
#!/usr/bin/env perl
package CalledError;
use Exporter qw(import);
our #EXPORT_OK=qw(do_build_stor_pools);
use strict;
use warnings;
use feature qw(say);
sub do_build_stor_pools {
say "now in CalledError do_build_stor_pools";
#my $undef_var="uncomment this to fix";
say $undef_var;
return;
}
Calling script:
#!/usr/bin/env/perl
use strict;
use warnings;
my $buildstor_mod="CalledError";
eval "require $buildstor_mod";
$buildstor_mod->import();
CalledError::do_build_stor_pools();
Run it like this to get
Undefined subroutine &CalledError::do_build_stor_pools called at calling_test.pl line 11.
Uncomment the definition of $undef_var to make it work.
If you checked $EVAL_ERROR, you would see the real error:
#!/usr/bin/env/perl
use strict;
use warnings;
my $buildstor_mod="CalledError";
eval "require $buildstor_mod";
if ($#) {
die "$#";
}
$buildstor_mod->import();
CalledError::do_build_stor_pools();
Error message:
Global symbol "$undef_var" requires explicit package name at CalledError.pm line 15.
Compilation failed in require at (eval 1) line 2.
You see the undefined subroutine error because require fails.
You're not checking the error condition of your eval:
eval "require $buildstor_mod";
die $# if $#;
However, to load a module, you should just use use:
use strict;
use warnings;
use CalledError qw(do_build_stor_pools);
do_build_stor_pools();
Outputs:
Global symbol "$undef_var" requires explicit package name at CalledError.pm line 14.

Why I am getting: Undefined subroutine

I am trying to move existing perl files to a new server and I am getting an error (error_log file). It work on the old server.
When I access to: http://2x.29.xx.xx/admin/web.pl
I get an error:
[error] Undefined subroutine &web_main::main called at
/var/www/path/web.pl line 40.
web.pl file, it look something like this:
#!/usr/bin/perl -w
use strict;
use warnings;
use lib '/var/www/web';
our $var = '/var/www/web';
our $var1;
our $var2;
use web_main qw($var $var1 $var2);
web_main::main(); # Line 40
web_main.pm file (in /var/www/web), look something like this:
#!/usr/bin/perl -w
package web_main;
use strict;
use warnings;
sub main
{
# Lots of code here... removed for brevity
}
Not sure what went wrong?
I'm not really an expert in Perl but perhaps you need to export the main subroutine? You can use Exporter.

Why doesn't my Perl script use my module?

module.pm
package module;
use 5.012;
use warnings;
sub Parse
{
return 1;
}
1;
script.pl
#!/usr/bin/perl -w
use 5.012;
use warnings;
use lib 'C:/';
use module;
print Parse("value");
Stdout
Undefined subroutine &main::Parse
You need either to write:
print module::Parse("value");
or to change the module package to export the name Parse.
See http://perldoc.perl.org/perlmod.html#Perl-Modules for guidance in exporting symbols from your module.
(By the way, you should really name your module Module rather than module. Lowercase module-names are used for Perl built-in features like use warnings and use strict.)
Several things:
First, use Local as your module prefix. That way, if you just happen to have a module with the same name in your Perl installation, it will use yours. Call it "Local::Module". Then, create a Local directory, and name your module Module.pm.
The other thing you have to understand is that you define your module in another namespace. By default, everything is in the main namespace until you use the package statement. That creates another namespace that your package uses. This way, if your package has a function foo, and you've defined a function foo in your main program, they won't collide.
Thus, you have two choices: One (the preferred now) is to simply call your subroutine with the full package name prepended to it. The second is to export your subroutine names to your main program. This can cause problems with duplicate names, but you don't have to keep typing in the package name every time you call your subroutine.
Without Exporting the name
Local/Module.pm
# /usr/bin/env perl
# Local/Module.pm
package Local::Module;
use strict;
use warnings;
sub Parse {
my $value = shift; #Might as well get it.
print "I got a value of $value\n";
return $value;
}
1; #Need this or the module won't load
program.pl
# /usr/bin/env perl
# program.pl
use strict;
use warnings;
use Local::Module;
Local::Module::Parse("Foo");
With export:
Local/Module.pm
# /usr/bin/env perl
# Local/Module.pm
package Local::Module;
use strict;
use warnings;
use Exporter qw(import);
our #EXPORT_OK(Parse); #Allows you to export this name into your main program
sub Parse {
my $value = shift; #Might as well get it.
print "I got a value of $value\n";
return $value;
}
1; #Need this or the module won't load
program.pl
# /usr/bin/env perl
# program.pl
use strict;
use warnings;
use Local::Module qw(Parse);
Parse("Foo");