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

Fellow monks,
I'm developping an application using mod_perl and I have a module naming problem.
I'm used to gather all my little functions in a separate module other than my main script in order to keep things clear in it.
I wanted to do the same with a script under mod_perl: I wrote it, tested it, worked nice. Then I took all the subs out of it, gathered them in a separate module.

It broke everything.
Trouble is, under mod_perl you have to specify the whole module name when using a function coming from another module.
It is a pain in my case, cause I wanted to make things more readable and I end up making it worse.

So the question is:
Is there a simple trick (magical or black magical :o) ) that would enable me to use my "delocalized" subs without naming them the 'long' way?

Thanks a lot.
  • Comment on mod_perl and modules (Or "how do I import subs into my PerlHandler module)

Replies are listed 'Best First'.
Re: mod_perl and modules (Or "how do I import subs into my PerlHandler module)
by UnderMine (Friar) on Nov 22, 2002 at 11:25 UTC
    Please can we have a bit more detail.. preferably a code snippet.

    Are you using EXPORT, EXPORT_OK or EXPORT_TAGS in your module?

    By PerlHandler you are not talking about PerlTransHandler are you cause they are far more fun ;)

    Some times it is a good idea to alter the startup.pl script to include :-

    use lib qw(/path/to/module/library);
    This is grate when running different apache daemons on the same server with different versions of the same modules.

    Hope this helpful
    UnderMine

Re: mod_perl and modules (Or "how do I import subs into my PerlHandler module)
by Sihal (Pilgrim) on Nov 22, 2002 at 11:16 UTC
    Ok (answering my own question) the trick is to do:
    use Module qw(subs) ;

    But this doesn't explain why certain subs can be called and other can't.
Re: mod_perl and modules (Or "how do I import subs into my PerlHandler module)
by Sihal (Pilgrim) on Nov 22, 2002 at 11:34 UTC
    Here are some snippets: my PerlHandler module does something like that:
    use QMM::photo::qmm_photo qw(get_usr_params);
    and in package qmm_photo;
    package QMM::photo::qmm_photo ; #use strict; use Exporter; use DBI; use lib "/home/loic/server" ; our @ISA = ('Exporter'); our @EXPORT = qw( get_usr_params );
    Without the explicit use QMM::photo::qmm_photo qw(...) ; my main module wouldn't load the funcs properly.
      Try removing the our as @ISA and @EXPORT are special variables and are not related to the package scope..
      package QMM::photo::qmm_photo ; use strict; use Exporter; use DBI; use lib "/home/loic/server" ; @ISA = ('Exporter'); @EXPORT = qw( get_usr_params );
      Hope it Helps
      UnderMine
      If you want to refer to subs defined in another package you do have to use the package prefix or import them. That's the perl5 way. There are people who make perl4-style library files with no package declaration that just add their subs to the current package when you use them, but this is a bad practice and should be avoided.

      The only mod_perl-specific issue is that if you run your script under Apache::Registry and use the same perl4-style library file in two scripts, the second one to run will not find the subs (because it's in a different package).

        Hum.
        Well usually everything in @EXPORT is exported by default when under normal use, and only stuff in EXPORT_OOK has to be exported explicitely.
        Apparently (from my experience, but I might do something wrong) under mod_perl, you'd better import everything explicitely else it won't work.
Re: mod_perl and modules (Or "how do I import subs into my PerlHandler module)
by Sihal (Pilgrim) on Nov 22, 2002 at 11:04 UTC
    okay, udpate: I suspect there is something I don't understand somewhere, cause I'm able to load funcs from some modules, but other modules won't behave the same. There is something I'm missing. (as always :( )
      Sihal,
      I think some snippets of code may make things a bit clearer here.
      AA