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

Hi Monks
I think I've a simple problem. I like to use some of the subroutines of a modules as if they were subroutine of my main program.
I tried something with Exporter, DynaLoader, ISA and our, but without succes.
So don't want to create objects, with wich I can access those subroutines
Here is a snippet of what I tried:
package A; require Exporter ; require DynaLoader ; our ($ISA, @EXPORT, $AUTOLOADER) ; @ISA = qw(Exporter, DynaLoader) ; @EXPORT = qw(suba) ; sub suba { print "suba\n" ; } 1 ; __END__
So, here is my main prog:
use A qw(suba); &suba() ; __END__
Thanks
Luca

Janitored by Arunbear - replaced pre tags with code tags, to allow code extraction

Replies are listed 'Best First'.
Re: subroutine inheritance
by Fang (Pilgrim) on Apr 20, 2005 at 07:42 UTC

    First off, DynaLoader is not mandatory for what you want to do. Someone will probably explain you its use, because I personally don't know much about it. I know however that Exporter should be enough.

    Second, you have a syntax error that would have been reported, had you turned on warnings.

    @ISA = qw(Exporter, DynaLoader);

    should be

    @ISA = qw(Exporter DynaLoader); # no comma

    Last, it's not necessary to specify which subroutine to import when using @EXPORT. It's only mandatory when going with @EXPORT_OK

    Putting it all together, something like that will do what you want:

    package MyModule; use strict; use warnings; # might want to turn this off once everything works fine use Exporter; our (@ISA, @EXPORT); @ISA = qw(Exporter); @EXPORT = qw(suba); sub suba { print "suba\n"; } 1;

    Then, in your code:

    #!/usr/bin/perl use strict; use warnings; use MyModule; suba();

    Update: I had forgotten the necessary use Exporter; line... shame on me. Anyway, you'll want to read perldoc Exporter.

      DynaLoader loads and initializes shared libraries, usually compiled XS code. It's fairly clever and unfortunately underused. Unless you write or use shared libraries directly, you don't need to know about it.

      I had forgotten the necessary use Exporter; line... shame on me.

      Use use base 'Exporter'; instead and forget all about this @ISA and require/use Exporter; nonsense. ;-)

      ihb

      See perltoc if you don't know which perldoc to read!

Re: subroutine inheritance
by johnnywang (Priest) on Apr 20, 2005 at 07:35 UTC
    package A; require Exporter ; our @ISA = qw(Exporter); @EXPORT_OK = qw(suba); sub suba { print "suba\n" ; } 1 ;
    and then:
    use strict; use A qw(suba); suba();
Re: subroutine inheritance
by ysth (Canon) on Apr 20, 2005 at 07:43 UTC
    To have the import list (here, qw(suba)) actually do something, your module needs to provide an import sub. Because A doesn't define one or inherit one, the import list is silently ignored. This has been construed as a feature :).

    You do try to inherit Exporter's import(), but it fails due to an error so common that there's a specific warning just to tell you about it. As soon as you enable warnings, you'll see it.

      Had the OP enabled strict in package A, it would have shown the error, too. :-)