in reply to Use, Exporter, I'm Dizzy.

You have three major problems.

#!/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
    Thank you, that answers all my questions. So if package is omitted from my module it just treats the subroutine as if it's in the Main:: namespace?
    Thanks
    s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;

      Not quite. It uses the currently "active" namespace.

      package Foo::Bar; use MyTest;
      would use Foo::Bar.
Re^2: Use, Exporter, I'm Dizzy.
by caelifer (Scribe) on Feb 07, 2007 at 17:52 UTC
    Just a question to your answer - is there something wrong with this form?
    package MyTest; use strict; use warnings; use base qw(Exporter); our @EXPORT_OK = qw(testroutine); # Rest of the code here ...

    In other words, does BEGIN form have any advantage over use base?

    -BR
      • You pretend there's an is-a relationship where there isn't one.
      • That won't work if two modules include each other.

      Newer versions of Exporter recognize the first point and allow you to do

      package MyTest; use strict; use warnings; BEGIN { our @EXPORT_OK = qw( testroutine ); } use Exporter qw( import ); # Rest of the code here ...

      The BEGIN is there to address the second point.

        Thanks. Good insight!