You have three major problems.
-
Perl comes with a module called test. You aren't loading the module you think you are loading until you added use lib '.';, which causes your module to be found before the one that comes with Perl.
-
use test (); means import nothing
use test; means import everything the module exports by default. With module using Exporter, that would be the items listed in @EXPORT. In the case of your module, that means import nothing.
-
Modules loaded using require (including those loaded using use) must have a package statement for them to work correctly when loaded from two different namespaces (such as from the main script and from a module).
You'll notice by adding the package statement that your module will need to use Exporter (or similar) to export testroutine to the caller.
#!/usr/bin/perl
use warnings;
use strict;
#use MyTest; # Imports what's in the module's @EXPORT
#use MyTest ( ); # Won't work.
#use MyTest qw( ); # Another way of writing the same thing.
use MyTest qw( testroutine ); # Imports testroutine and nothing else.
my $output = testroutine();
print $output, "\n";
# MyTest.pm
use strict;
use warnings;
package MyTest;
BEGIN {
our @EXPORT = qw( ); # Export nothing by default.
our @EXPORT_OK = qw( testroutine );
require Exporter;
*import = \&Exporter::import;
}
# Put other "use" statements here, if any.
sub testroutine {
return "This is a test";
}
1;