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

Good Monks,
I'm trying to load a bunch of modules through one main module so that when I use the module, I will be able to use all of it's sub-modules/classes.
Example:
use My_Module; my $stuff = My_Module::My_other_module::Get_stuff();


Simple enough, it seems, but it's got me scratching my head.

Thanks in advace.
[dhoss]
"and I wonder, when I sing along with you if everything could ever feel this real forever? if anything could ever be this good again? the only thing I'll ever ask of you, you've gotta promise not to stop when I say 'when'", she sang

Replies are listed 'Best First'.
Re: Loading modules through one main module?
by Abigail-II (Bishop) on Jan 12, 2004 at 02:19 UTC
    Use the Exporter module and its export_to_level method.

    Abigail

Re: Loading modules through one main module?
by ysth (Canon) on Jan 12, 2004 at 03:33 UTC
    That's no problem assuming your submodule is named "My_Module::My_other_module". Just have My_Module.pm say use My_Module::My_other_module.

    (I'm not trying to say they have to be nested like that, just that there is no automatic nesting done for you.)

    Example:

    $ cat script.pl use strict; use warnings; use Foo; print "calling Foo::mysub\n"; Foo::mysub(); print "calling Bar::mysub\n"; Bar::mysub(); $ cat Foo.pm package Foo; use strict; use warnings; use Bar; sub mysub { print "I'm a sub in ", __PACKAGE__, "\n" } 1; $ cat Bar.pm package Bar; use strict; use warnings; sub mysub { print "I'm a sub in ", __PACKAGE__, "\n" } 1; $ perl script.pl calling Foo::mysub I'm a sub in Foo calling Bar::mysub I'm a sub in Bar
    All this applies if you are not exporting anything. If you want things from Bar to export to script, you can either set up Bar to export them (so they are imported to Foo) and Foo to export them also (so they are imported to script); or use Abigail's suggestion of export_to_level (see Exporter doc) to do it in one fell swoop.
Re: Loading modules through one main module?
by Fletch (Bishop) on Jan 12, 2004 at 02:58 UTC

    Check out the main POE module which does something similar.

    ## This loads POE, POE::Session, and POE::Component::Client::DNS use POE qw( Session Component::Client::DNS );
Re: Loading modules through one main module?
by William G. Davis (Friar) on Jan 12, 2004 at 09:13 UTC

    To get a module to export what it imports, just copy the sub module's appropriate @EXPORT* array into the module's appropriate @EXPORT* array:

    My_Module.pm --------------------------- package My_Module; use warnings; use strict; use vars '@EXPORT'; use base 'Exporter'; use My_Module::My_other_module; @EXPORT = @My_Module::My_other_module::EXPORT;