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

I have a large collection of functions that I've put into a module "library." For ease of development, I would like to break the functions up into a few catagories (just so the file doesn't get too large to edit easily). So far, I have one module "Zephir.pm" that gets functions from a few other files and then makes them available to any scripts which "use Zephir;".

Here is the main module "Zephir.pm":

package Zephir; use strict; require 'Zephir-General.pl'; require 'Zephir-Cast.pl'; require 'Zephir-Gedit.pl'; require 'Zephir-Auth.pl'; 1;
And here is one of the sub libraries:
package Zephir; use strict; sub do_stuff(){ # does something profound... } 1;
So, is this the correct way to do this or is there a more elegant way (baring a complete OO overhaul). :-)

-caedes

Replies are listed 'Best First'.
Re: Spanning Modules Across Multiple Files
by fruiture (Curate) on Jul 05, 2002 at 12:42 UTC
    It's not "wrong", but you could use the advantages of the "Perl5-Module-Stuff", so instead of having "Zephir-General.pl" als 'library', you should have a "Zephir/General.pm" with the package Zephir::General.

    Zephir.pm (package Zephir) could then be an interface to the other Modules (e.g. via AUTOLOAD) and make functions available on-demand and export stuff on demand.

    Next step could be OO, Zephir.pm providing factories for the Zephir::* Subclasses, then you might make the modules even more flexible and usefull for others and then upload it to the CPAN and ... yes.
      Good ideas. I actually started out with Zephir::General, etcetera but later digresed to the single package when I noticed that all my scripts used every submodule. And yes you are right about the eventual OO implementation of the module, but I probably won't be doing it quite yet.

      -caedes

        You can put some code into Zephir/General.pm, load it with use Zephir::General but still have it in Zephir package as long as Zephir/General.pm starts with package Zephir. Module SomeName isn't really required to contain package SomeName. It is common convention but it is not strict rule you cannot break.

        --
        Ilya Martynov (http://martynov.org/)

        all my scripts used every submodule

        That's why I think you should let Zephir.pm organize loading and exporting of libraries. It could export an AUTOLOAD routine and fetch other stuff really in-time. Your scripts will use this interface without noticing what's going on behind the scenes, they just call subroutines.
Re: Spanning Modules Across Multiple Files
by Abigail-II (Bishop) on Jul 05, 2002 at 12:30 UTC
    It's a way of doing it, and there's not much wrong with it. I would use use instead of require, but that's a minute difference, the effect will be the same.

    Abigail

Re: Spanning Modules Across Multiple Files
by yodabjorn (Monk) on Jul 05, 2002 at 12:44 UTC
    Slap me if im worng, but wouldn't it be a little cleaner for you to maintain 1 file, but use @EXPORT_OK and just call your module with the functions you need to use. If you don't explicitly push functions on @EXPORT you will keep the calling program form sucking up your whole module.
    use my_module qw( func1 funk2 ) ;
    This is how i understand @EXPORT and @EXPORT_OK

      Well, right now I'm not using Exporter at all. I'm just calling the functions like so: Zephir::function('param');

      -caedes

        hrmm.. usually a good idea to at least setup @EXPORT so you don't poision your main app with all your functions.
        package my_module ; use strict ; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT = qw(&db_open &db_close) ; @EXPORT_OK = qw(&db_open &db_close &db_store &db_fetch &db_keys); #your subs go here 1;
        Least this is what i do.. look at the Module Tutorials