You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Sanek Anoshin <sa...@netmedia.co.uk> on 2001/10/31 10:23:57 UTC

2 mod_perl questions

Hi everyone.

I'm a novice in mod_perl but I already surprised with its features !
Could you please help me with following two things:

1) What is the most common directory structure for mod_perl
web-applications ?
I keep all my modules in a separate directory (which is closed for
internet-users) and I have to add path to them to @INC (for automatic
reloading).
But what if
there are several applications with modules of the same name (like
config.pm or utils.pm) ?

2) Suppose I want to share variable $q between several modules.
In the main script I wrote:

use vars qw ($q);
use Module1 qw ($q);

In the Module1 I wrote:
BEGIN {
 use Exporter ();
 @Module1::ISA = qw (Exporter);
 @Module1::EXPORT = qw ();
 @Module1::EXPORT_OK = qw ($q);
}
use vars qw ($q);

All works perfectly. But I want to add second module:

I add to main script:
use Module2 qw ($q);

And I write in the Module2 things which are the same as in Module1.
And it works not stable until I add

use Module2 qw ($q);

in the Module1 and correspondingly

use Module1 qw ($q);

in the Module2.

Does it mean that to add the third module I need add such string to
modules 1 and 2 etc ?


Many thanks for advance.
Regards, Alexander.



Re: 2 mod_perl questions

Posted by Stas Bekman <st...@stason.org>.
> I'm a novice in mod_perl but I already surprised with its features !


Before you continue any further with new questions it's a time to read 
the guide: http://perl.apache.org/guide/

> Could you please help me with following two things:
> 
> 1) What is the most common directory structure for mod_perl
> web-applications ?
> I keep all my modules in a separate directory (which is closed for
> internet-users) and I have to add path to them to @INC (for automatic
> reloading).
> But what if
> there are several applications with modules of the same name (like
> config.pm or utils.pm) ?


You have to fully qualify these. E.g. MyProject1::config, 
MyProject2::config and etc. You will find more examples in the guide.

 
> 2) Suppose I want to share variable $q between several modules.


http://perl.apache.org/guide/perl.html#Using_Global_Variables_and_Shari

_____________________________________________________________________
Stas Bekman             JAm_pH      --   Just Another mod_perl Hacker
http://stason.org/      mod_perl Guide   http://perl.apache.org/guide
mailto:stas@stason.org  http://ticketmaster.com http://apacheweek.com
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/


Re: 2 mod_perl questions

Posted by "Ken Y. Clark" <kc...@logsoft.com>.
On Wed, 31 Oct 2001, Sanek Anoshin wrote:

> Date: Wed, 31 Oct 2001 15:23:57 +0600
> From: Sanek Anoshin <sa...@netmedia.co.uk>
> To: modperl@apache.org
> Subject: 2 mod_perl questions
>
> Hi everyone.
>
> I'm a novice in mod_perl but I already surprised with its features !
> Could you please help me with following two things:
>
> 1) What is the most common directory structure for mod_perl
> web-applications ?

Sanek (or Alexander?),

I would suggest you decide on one path that will be accessible to all
servers who need your code.  That is, if all your code is executed by
one Apache/mod_perl server (i.e., one box), then maybe you should put
all your custom modules under the Apache tree.  I like:

    /usr/local/apache/lib/perl/MyNameSpace/

If you have a farm of web servers, then perhaps choose a location
visible to all, like an NFS mount.

> I keep all my modules in a separate directory (which is closed for
> internet-users) and I have to add path to them to @INC (for automatic
> reloading).

It's also common to have a "PerlRequire" directive in your httpd.conf
that pulls in a "perlstartup.conf" file (or whatever you want to name
it).  This file would contain "use" statements for all the modules
you will use in your application.  It causes Apache to load them at
startup.  Here's a common example:

    # perlstartup.conf
    use Apache();
    use Apache::Request();
    use Apache::Cookie();
    use DBI();
    use DBD::mysql();

    # custom modules
    use lib 'lib/perl';
    use MyNameSpace::Foo();
    use MyNameSpace::Bar();
    use MyNameSpace::Baz();

> But what if
> there are several applications with modules of the same name (like
> config.pm or utils.pm) ?

I'd recommend you declare your own namespace.  I typically choose the
company's name or initials for whom I'm writing code, then add the
name of the application, then the module, like
"XYZCompany::ImageLibrary::DisplayManager."  If you still have modules
with the same names, then you'll have to grab a thesaurus and find
some other names.  :)

> 2) Suppose I want to share variable $q between several modules.
> In the main script I wrote:
>
> use vars qw ($q);
> use Module1 qw ($q);
>
> In the Module1 I wrote:
> BEGIN {
>  use Exporter ();
>  @Module1::ISA = qw (Exporter);
>  @Module1::EXPORT = qw ();
>  @Module1::EXPORT_OK = qw ($q);
> }
> use vars qw ($q);
>
> All works perfectly. But I want to add second module:
>
> I add to main script:
> use Module2 qw ($q);
>
> And I write in the Module2 things which are the same as in Module1.
> And it works not stable until I add
>
> use Module2 qw ($q);
>
> in the Module1 and correspondingly
>
> use Module1 qw ($q);
>
> in the Module2.
>
> Does it mean that to add the third module I need add such string to
> modules 1 and 2 etc ?
>
>
> Many thanks for advance.
> Regards, Alexander.

It seems like it would be safer to explicitly pass it your "$q"
variable to whatever functions that need it.  It's just a guess, but
if your "$q" is a CGI.pm object, then you'd definitely be better off
using the Apache::Request and then perhaps passing around whatever
values you have in a hash or something.  I would definitely recommend
against exporting things as it clutters up your namespaces and
confuses whoever will have to maintain your code.

But do you really even need to pass an object between modules?
Usually I design my modules so that they're fairly self-contained,
unless, of course, I've designed some object to handle some
processing.  Even then, I'll explicitly pass to the object whatever it
needs to work.  I would never ask a module or object to be implicitly
aware (or to trust) another module's variables.

HTH,

ky