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

Dear Monks,

my name is Pedro and I just joined the monastery! I have a doubt on how to include my own modules in other modules. For instance if I have the following:

lib/ Module/ Try Error.pm Task.pm Main.pm
Now if I wan to use the Error.pm in Module::Main how do I do that. If, while developing the module, and before installing my it I simply do
package Module::Main use Module::Try::Error;
I get an error when I try to run the code. Same thing happens when I want to use Module::Try::Error in Module::Try::Task and do
package Module::Try::Task; use Module::Try::Error;
any help is much appreciated!

Replies are listed 'Best First'.
Re: Including my own modules
by desemondo (Hermit) on Dec 17, 2009 at 07:27 UTC
    Welcome to the fold Pedro!

    Take a look over the perlmod section on Perldoc... It provides an excellent starting point for writing modules.

    With regards to
    package Module::Main use Module::Try::Error;
    giving you an error, looks like you're missing a ; when you declared Module::Main... also, the clearer your questions are, the easier and quicker the answers to you will be.

    best of luck with it.


    ps. also, I'd suggest using a different names for your module rather than "Module" and "Module::Main". They don't really convey much of an idea of what they are intended to do... And the name space of your module is kind of ground level zero. Everything else is built on top of it... Of course you can change it later, but why make life harder for yourself... change it now while there aren't (m)any dependancies on it...
Re: Including my own modules
by BioLion (Curate) on Dec 17, 2009 at 10:12 UTC

    As well as the other comments monks have given, I thought i should mention that you should always use strict and use warnings. You might also want to use diagnostics which will give you more verbose warnings, and can often help identify the cause of your errors.

    You should also make sure your script knows where your mods live, if it is not in with all the others. This is a good time to use a BEGIN{} block and lib:

    use strict; use warnings; BEGIN{ use lib '/path/to/your/homemade/modules/'; } ## now try use Module::Try::Error;

    There is no point in doing this if your modules are already somewhere that perl knows about (the places it looks for modules are held in the special variable @INC). HTH.

    Just a something something...

      Thank you guys for the quick replies. I always do "use strict" but hardly ever "use warnings", so I will start using it as well.

      I was using the lib as well but I thought that it was not very elegant solution. What is your advice for when you create a distribution that is installed by other users? By then the modules will live where perl can find them and the lib statement is no longer needed.

      Should I use lib /my/svn/work/dir while in development and remove it afterwards?

      what are the best practices?

      Once again thank you in advance for all your wisdom.

        By then the modules will live where perl can find them and the lib statement is no longer needed.

        Should I use lib /my/svn/work/dir while in development and remove it afterwards?

        In this case you could also set the environment variable PERL5LIB to tell Perl about the additional lib directory in the development environment. This way you wouldn't need any use lib statement at all...

        The general idea is

        dev.pl

        use lib '/my/svn/work/dir'; use MySweetApp; MySweetApp->run();
        prod.pl
        use lib '/production/dir'; use MySweetApp; MySweetApp->run();
        cust.pl
        use lib '/production/dir'; use MySweetApp; MySweetApp->new('notdefault.config')->run();
      BEGIN isn't necessary, use happens at BEGIN time
Re: Including my own modules
by Anonymous Monk on Dec 17, 2009 at 07:17 UTC
    What error?