halfcountplus has asked for the wisdom of the Perl Monks concerning the following question:

(My remaining is in bold at the end)

If I have a module "mine.pm" containing package "thispack" and when used I want it to export method "thisfunc" (without having to use "thispack::thisfunc", how can I do that?

What I tried thus far is:
in the module
{ package thispack; use base ("Exporter"); our @EXPORT = ("thisfunc"); sub thisfunc {

then in some script
use mine ("thispack");
However, calls to "thisfunc" still require the package name. What have I got wrong?


LATER: ahh, it seems the .pm name and the package name have to be the same!
Does that mean I have to put multiple packages in seperate modules??!?

Replies are listed 'Best First'.
Re: how to export methods from module [only partially SOLVED]
by ikegami (Patriarch) on May 03, 2009 at 17:30 UTC

    use does a require and a import. Feel free to just do the later.

    Or,

    BEGIN { package Foo; ... $INC{"Foo.pm"} = 1; } BEGIN { package Bar; ... $INC{"Bar.pm"} = 1; }
    use Module qw( ); # Load the file use Foo qw( ... ); # Import use Bar qw( ... ); # Import
Re: how to export methods from module [only partially SOLVED]
by shmem (Chancellor) on May 03, 2009 at 21:42 UTC
    LATER: ahh, it seems the .pm name and the package name have to be the same! Does that mean I have to put multiple packages in seperate modules??!?

    No, and no. But: use does a lookup per file name. Inside a file, the package statement declares a name space. It is highly confusing if the file name and the name space name are not related, or chosen nilly-willy.

    Remember that use Foo is (roughly) equivalent to

    if (eval "require $package") { $package->import(@args); }

    If your file is called mine.pm and you use that (use mine;), but inside that file there's a name space mypack established, the use attempts to call mine->import and not mypack->import. There is no facility in Exporter to check which name spaces have been set up by an imported file which wishes to export symbols.

    As for your second question - you may set up multiple name spaces in one file, but only the name space which matches the file name will be handled correctly with Exporter.

      It is exactly equivalent to
      BEGIN { require Module; import Module LIST; }
Re: how to export methods from module [only partially SOLVED]
by Anonymous Monk on May 03, 2009 at 17:21 UTC