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

Well bretheren I'm in a bit of a pickle! I'm writing my first Perl Module and though it is working there is a small but irritating problem! My module called Hoos has several house-keeping related subroutines in it. In an ordinary script I have "use Hoos ;" and a call to one of the subs Hoos::mail_log(params). This works fine but I would prefer to omit the Hoos:: from the call. To this end I've included the following -
use Exporter ; our @ISA = qw(Exporter)
in the Hoos.pm code. My script now fails as a result of "an undefined routine main::mail_log" being called. Next I changed the Hoos.pm code to include -
package Hoos ; our @EXPORT = qw( list of subs mail_log mail end_it etc) ; use Exporter ; our @ISA = qw(Exporter);
All of which was lifted from the Perl Objects References & Modules manual. None of this has made a blind bit of a difference, the only way I can call any of the subs in Hoos.pm is to prefix the routine name by Hoos:: - can anyone tell me how to fix this.

Cheers,
Ronnie "Novice" Cruickshank

PS I don't pretend to understand the Exporter/Import business yet so don't presume I know very much!

edit (broquaint): added formatting

Replies are listed 'Best First'.
Re: Module Inheritance
by Abigail-II (Bishop) on Apr 29, 2004 at 12:40 UTC
    package Hoos; use Exporter (); our @ISA = qw /Exporter/; our @EXPORT = qw /mail_log/;
    ought to work.

    Abigail

Re: Module Inheritance
by broquaint (Abbot) on Apr 29, 2004 at 12:44 UTC
    You may need to demonstrate more code as that should work fine e.g
    shell> cat Hoos.pm package Hoos; our @EXPORT = 'testsub'; use Exporter; our @ISA = 'Exporter'; sub testsub { print "exported testsub() ok\n" } 1; shell> perl -e 'use Hoos; testsub()' exported testsub() ok
    So there we've set up Exporter to export testsub whenever Hoos is used. Also, you should require Exporter as useing it calls its own import method.
    HTH

    _________
    broquaint

      So there we've set up Exporter to export testsub whenever Hoos is used. Also, you should require Exporter as useing it calls its own import method.
      A populair belief, but not true. Sure, using 'require' prevents the import() method to be called, but that's not the only way.
      use Exporter (); # Don't call import!
      Less characters to type, and it gets dealt with at compile time. Why bother with require?

      Abigail

        A populair belief, but not true.
        Well, it is true. You know that. It doesn't magically not call import.
        Less characters to type, and it gets dealt with at compile time. Why bother with require?
        It's one less character to type and in modules the likes of require Exporter are being implicitly being handled at compile-time anyhow (assuming they've been useed). As for the 'Why', force of habit I guess. I generally do use Module () if I don't want import to be invoked but make an exception for require Exporter. C'est la vie.
        HTH

        _________
        broquaint

Re: Module Inheritance
by gellyfish (Monsignor) on Apr 29, 2004 at 12:45 UTC

    Well the code as in your second attempt shoulod (indeed does in a small test.) work fine. There must be something you are not showing us here - are you doing 'use Hoos;' or 'require Hoos;' as the latter will not call Hoos::import() (which you inherited from Exporter) and thus will not work.

    Can you show us a cut down version of your code that exhibits this behaviour?

    /J\

Re: Module Inheritance
by matija (Priest) on Apr 29, 2004 at 12:42 UTC
    I have a feeling that this is one of the places where the order of the statements matters. Move the @EXPORT assignement after the @ISA declaration in your code, and see if that helps.