I have test_utils.pm:
package TestUtils;
use strict;
use warnings;
use Exporter 'import';
our @EXPORT = qw / print_diagnostic /;
sub print_diagnostic { <some_code_here> }
And I'd like to call print_diagnostic
from my main script tester.pl which is located in the same folder (I've used this solution):
#!/usr/bin/perl
use lib dirname (__FILE__);
use test_utils qw (:ALL);
print_diagnostic(<some_message>);
The problem is that I'm getting
Undefined subroutine &main::print_diagnostic called at ...
Calling the function with explicit package name TestUtils::print_diagnostic
works fine, but for various reasons I do want to call it w/o such a prefix.
use test_utils qw(print_diagnostic);
and
require test_utils;
yields the same results.
I've dug over few scores of similar questions, tried the answers and "solutions", but all that doesn't seem to work for me, and I'm getting really confused with all that "export", "import" and all the things around it, that I can't seize these concepts, so counterintuitive, comparing C++ and Java.
All what I want to do, is to split very complex scripts into several modules and reuse the code (now there is a lot of duplication).
use test_utils qw( :ALL );
is equivalent to
BEGIN {
require test_utils;
import test_utils qw( :ALL );
}
but you want
BEGIN {
require test_utils;
import TestUtils qw( :ALL );
}
Use the same name for the file and the package to avoid this problem.
By the way, you should replace
use File::Basename qw( dirname );
use lib dirname(__FILE__);
with
use Cwd qw( abs_path );
use File::Basename qw( dirname );
use lib dirname(abs_path(__FILE__));
or simply
use FindBin qw( $RealBin );
use lib $RealBin;
Your version will break if a symlink to the script is used.