in reply to Use, Exporter, I'm Dizzy.
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Use, Exporter, I'm Dizzy.
by logie17 (Friar) on Feb 07, 2007 at 17:30 UTC | |
by ikegami (Patriarch) on Feb 07, 2007 at 17:33 UTC | |
|
Re^2: Use, Exporter, I'm Dizzy.
by caelifer (Scribe) on Feb 07, 2007 at 17:52 UTC | |
by ikegami (Patriarch) on Feb 07, 2007 at 17:55 UTC | |
by caelifer (Scribe) on Feb 07, 2007 at 18:25 UTC |