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

The question is this: My main program uses two modules, module A and module B. Now I want one of the subroutines in module A to call one of the subroutines in module B. Is this possible? If so, how can I do it? (When I do this, Perl reports it can't find the subroutine in module B.) Or is my question a "violation" to the very essence of modules? Thanks!

Replies are listed 'Best First'.
RE: Module dependency
by Jouke (Curate) on Sep 07, 2000 at 13:51 UTC
    Just use module B in module A... like this:

    package a; use b; ...

    Jouke Visser, Perl 'Adept'
Re: Module dependency
by jreades (Friar) on Sep 07, 2000 at 17:30 UTC

    As an example:

    # package.pm $var = 'a variable'; sub mySub { print "called!\n"; } 1; ################# # script.pl use package; mySub(); print "Var: " . $var . "\n"; exit 0;

    You should note, however, that this isn't always the best behaviour -- frequently, you might want to define multiple namspaces/packages/modules in order to avoid being stuck with a load of globals in a thousand-line script...

    It is generally (at least AFAIK) considered much nicer to place things in the @EXPORT_OK array rather than the @EXPORT array where they are automatically dumped into my progam's namespace.

      Simply useing a module does not automatically export variables and subroutines into the calling module. For that you have to use stuff like Exporter to do this job (using, as you note, things like @EXPORT_OK and @EXPORT).

        Funnily enough, that's exactly what I thought -- so I was going to produce a big 'ta-da' with &package::mySub()... but then I wrote and ran the program you see and it worked.

        So, a quick reread of my Perl documentation suggests the following:

        • .pm files can be imported straight into the namespace of the using script if the .pm file lacks a package declaration
        • As soon as a package declaration is made in a .pm file the standard rules of EXPORTing and importing apply
        • So, if I rewrite my code as follows:
        ################## # MyPackage ################## package MyPackage; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(exportedSub); @EXPORT_OK = qw(exportedOkSub); $var = 'a variable'; sub exportedSub { print "Exported sub called!\n"; } sub exportedOkSub { print "ExportedOK sub called!\n"; } 1; ################# # Script.pl ################# use MyPackage qw(exportedOkSub); exportedSub(); exportedOkSub(); print "Var: " . $MyPackage::var . "\n"; exit 0;

        Then it should work as advertised.

        Note, however, that I have done some fudging as MyPackage isn't properly included in my @INC -- so YMMV. :^)

        ~
Re: Module dependency
by Fastolfe (Vicar) on Sep 07, 2000 at 17:22 UTC
    Fully qualify the name or import the items from one module into the other module that uses it.
    package a; sub a_test { print "Hello, world!\n"; } package b; &a_test; # fails &a::a_test; # Hello, world!
    You can also use the Exporter stuff to export functions from one module into another that uses it. I won't go into details about this. Check out the perlmod documentation as well as the stuff for the Exporter module.