Why do I get the error "Undefined routine" when calling a function from a module?

I have a module called Helpers.pm :

 use warnings; use 5.012; package Helpers; use Exporter qw(import); our @EXPORT_OK = qw(my_function); sub my_function { return { one => 1, two => 2 }; } 1; 

call it in the script:

 #!/usr/bin/env perl use warnings; use 5.012; use Data::Dumper; use FindBin qw($RealBin); use lib $RealBin; use Helpers qw(my_function); my $ref = my_function(); say Dumper $ref; 

and I do not receive error messages. But when I put the module in the TestDir directory, change the script as follows:

 #!/usr/bin/env perl use warnings; use 5.012; use Data::Dumper; use FindBin qw($RealBin); use lib $RealBin; use TestDir::Helpers qw(my_function); my $ref = my_function(); say Dumper $ref; 

I get this error message:

 Undefined subroutine &main::my_function called at ./perl.pl line 10. 

Why am I getting this error message?

+8
module perl
source share
2 answers

You probably forgot to change the package declaration from

 package Helpers; 

in

 package TestDir::Helpers; 
+14
source share

I think this is because it cannot find your module in the lib path, http://perldoc.perl.org/lib.html .

 use lib 'TestDir'; use Helpers qw(my_function); 
+3
source share

All Articles