You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "Purcell, Scott" <sp...@ltcgroup.com> on 2001/06/20 16:04:18 UTC

Curious About Require

Hello,
I have a question about require when using mod-perl. I produced four simple
.pl files.
main.pl, one.pl two.pl three.pl

>From the main.pl you can link to either one two or three.pl. Pretty simple.

The problem begins when I put a require in "one.pl, two.pl, and three.pl".
If in each of those files I put in for instance:
require "D:/Apache/mod_perl/query.pl";

When I click on the main, and lets say I go to one.pl it works fine.
If I then proceed to click on two.pl (it says query.pl not found), even
though there is a require.

Restart Apache,
Then click from main to two.pl it works fine.
If then I go to one.pl error (it says query.pl not found).

So my question is why this bazaar behavior. If I change the query.pl to a
module and use it in each of the pages it is fine. I just find this behavior
rather funny. If anyone has any insight please update me.

Thanks,
Scott Purcell


Re: Curious About Require

Posted by Perrin Harkins <pe...@elem.com>.
> I have a question about require when using mod-perl.

The full story is at
http://theoryx5.uwinnipeg.ca/guide/porting/Name_collisions_with_Modules_and.
html

It sounds like you're having trouble because your .pl files are not real
modules but rather subs that get added into the current package space.  This
is a problem because required files are only loaded once per process
(standard require() behavior) but each Registry script has a different
package name.  The real fix is to give the .pl files package names and make
them into real modules, but here's a lame quick fix:

BEGIN {
do "mail.pl";
}

That will work, but it wastes memory by loading them multiple times per
child.  If you make them into real modules, they can be loaded from
startup.pl and thus be in shared memory.

- Perrin


Re: Curious About Require

Posted by Perrin Harkins <pe...@elem.com>.
> > > BEGIN {
> > >     delete $INC{'/foo/bar/query.pl'} if exists
$INC{'/foo/bar/query.pl'};
> > >     require '/foo/bar/query.pl';
> > >   }
> >
> > Mark, your suggestion doesn't work because of BEGIN.
>
> Ooops, gotta go to sleep. Your suggestion, Mark, will work :) but only in
> Registry/PerlRun which executes BEGIN on every request.

It only needs to do the require once per script, so if you put this in each
script it will be fine.  Also, I don't think Registry runs BEGIN blocks
every time.  Can't remember about PerlRun.

> BTW, here is shorter version of your suggestion:
>
> BEGIN { do '/foo/bar/query.pl'; }

That's what I used when porting someone's old perl4-ish code to PerlRun.
Both of these quick fixes waste memory though.

- Perrin


Re: Curious About Require

Posted by Perrin Harkins <pe...@elem.com>.
I hate to belabor this point, but I don't want people to get the wrong idea:

> > BEGIN { do '/foo/bar/query.pl'; }
>
> Sorry, I guess I should have been more explicit. I don't want to
> require in the file on every request, but rather only allow the
> same file to be require'd multiple times (to install the same sub
> routines into different packages).

That's what this accomplishes.  It only runs once, being inside of a BEGIN
block.  Using do() instead of require() skips the %INC check, so you don't
have to delete anything from %INC this way.  Of course, do() doesn't check
to see if your module returned true, but they are basically equivalent.
TMTOWTDI.

- Perrin


Re: Curious About Require

Posted by Mark Doyle <do...@aps.org>.
Greetings,

On Wednesday, June 20, 2001, at 12:45 PM, Stas Bekman wrote:

> Ooops, gotta go to sleep. Your suggestion, Mark, will work :) but only 
> in
> Registry/PerlRun which executes BEGIN on every request.
>
> Sorry about my previous post :(
>
> But if you delete and reload the file on every request it defeats the
> purpose of using mod_perl to some degree, depending on how many files 
> you
> force to reload and how big they are.
>
> BTW, here is shorter version of your suggestion:
>
> BEGIN { do '/foo/bar/query.pl'; }

Sorry, I guess I should have been more explicit. I don't want to
require in the file on every request, but rather only allow the
same file to be require'd multiple times (to install the same sub
routines into different packages).  This is needed because we have
modularized some of our XML handlers for SUBS-style parsing in
XML::Parser and we need create multiple subclasses of XML::Parser
that share the same modularized components. The packages that
subclass XML::Parser are read in only once in the startup.pl file.
The last require leaves the files in the %INC hash, so it isn't
subsequently read in on every request (so 'do' isn't quite the
same).

Cheers,
Mark

Re: Curious About Require

Posted by Stas Bekman <st...@stason.org>.
On Thu, 21 Jun 2001, Stas Bekman wrote:

> On Wed, 20 Jun 2001, Mark Doyle wrote:
>
> > Greetings,
> >
> > require will only happen once per perl process and since mod_perl
> > is essentially a single perl process, the file is only require'd
> > for the first request. You can get around this by deleting the file from
> > the %INC
> > hash which keeps track of which files you have loaded. Something like:
> >
> > BEGIN {
> >     delete $INC{'/foo/bar/query.pl'} if exists $INC{'/foo/bar/query.pl'};
> >     require '/foo/bar/query.pl';
> >   }
>
> Mark, your suggestion doesn't work because of BEGIN.

Ooops, gotta go to sleep. Your suggestion, Mark, will work :) but only in
Registry/PerlRun which executes BEGIN on every request.

Sorry about my previous post :(

But if you delete and reload the file on every request it defeats the
purpose of using mod_perl to some degree, depending on how many files you
force to reload and how big they are.

BTW, here is shorter version of your suggestion:

BEGIN { do '/foo/bar/query.pl'; }

> See the URL posted in my other reply for the explanation.

Still holds :)

> > Cheers,
> > Mark
> >
> > On Wednesday, June 20, 2001, at 10:04 AM, Purcell, Scott wrote:
> >
> > > Hello,
> > > I have a question about require when using mod-perl. I produced four
> > > simple
> > > .pl files.
> > > main.pl, one.pl two.pl three.pl
> > >
> > > So my question is why this bazaar behavior. If I change the query.pl
> > > to a
> > > module and use it in each of the pages it is fine. I just find this
> > > behavior
> > > rather funny. If anyone has any insight please update me.
> > >
> > > Thanks,
> > > Scott Purcell
> > >
> >
>
>
>
> _____________________________________________________________________
> 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://apachetoday.com http://eXtropia.com/
> http://singlesheaven.com http://perl.apache.org http://perlmonth.com/
>
>
>



_____________________________________________________________________
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://apachetoday.com http://eXtropia.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/



Re: Curious About Require

Posted by Stas Bekman <st...@stason.org>.
On Wed, 20 Jun 2001, Mark Doyle wrote:

> Greetings,
>
> require will only happen once per perl process and since mod_perl
> is essentially a single perl process, the file is only require'd
> for the first request. You can get around this by deleting the file from
> the %INC
> hash which keeps track of which files you have loaded. Something like:
>
> BEGIN {
>     delete $INC{'/foo/bar/query.pl'} if exists $INC{'/foo/bar/query.pl'};
>     require '/foo/bar/query.pl';
>   }

Mark, your suggestion doesn't work because of BEGIN.

See the URL posted in my other reply for the explanation.

> Cheers,
> Mark
>
> On Wednesday, June 20, 2001, at 10:04 AM, Purcell, Scott wrote:
>
> > Hello,
> > I have a question about require when using mod-perl. I produced four
> > simple
> > .pl files.
> > main.pl, one.pl two.pl three.pl
> >
> > So my question is why this bazaar behavior. If I change the query.pl
> > to a
> > module and use it in each of the pages it is fine. I just find this
> > behavior
> > rather funny. If anyone has any insight please update me.
> >
> > Thanks,
> > Scott Purcell
> >
>



_____________________________________________________________________
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://apachetoday.com http://eXtropia.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/



Re: Curious About Require

Posted by Mark Doyle <do...@aps.org>.
Greetings,

require will only happen once per perl process and since mod_perl
is essentially a single perl process, the file is only require'd
for the first request. You can get around this by deleting the file from 
the %INC
hash which keeps track of which files you have loaded. Something like:

BEGIN {
    delete $INC{'/foo/bar/query.pl'} if exists $INC{'/foo/bar/query.pl'};
    require '/foo/bar/query.pl';
  }

Cheers,
Mark

On Wednesday, June 20, 2001, at 10:04 AM, Purcell, Scott wrote:

> Hello,
> I have a question about require when using mod-perl. I produced four 
> simple
> .pl files.
> main.pl, one.pl two.pl three.pl
>
> So my question is why this bazaar behavior. If I change the query.pl 
> to a
> module and use it in each of the pages it is fine. I just find this 
> behavior
> rather funny. If anyone has any insight please update me.
>
> Thanks,
> Scott Purcell
>

Re: Curious About Require

Posted by Stas Bekman <st...@stason.org>.
On Wed, 20 Jun 2001, Purcell, Scott wrote:

> Hello,
> I have a question about require when using mod-perl. I produced four simple
> .pl files.
> main.pl, one.pl two.pl three.pl
>
> >From the main.pl you can link to either one two or three.pl. Pretty simple.
>
> The problem begins when I put a require in "one.pl, two.pl, and three.pl".
> If in each of those files I put in for instance:
> require "D:/Apache/mod_perl/query.pl";
>
> When I click on the main, and lets say I go to one.pl it works fine.
> If I then proceed to click on two.pl (it says query.pl not found), even
> though there is a require.
>
> Restart Apache,
> Then click from main to two.pl it works fine.
> If then I go to one.pl error (it says query.pl not found).
>
> So my question is why this bazaar behavior. If I change the query.pl to a
> module and use it in each of the pages it is fine. I just find this behavior
> rather funny. If anyone has any insight please update me.

please take some time off to read the guide and most of your questions
will be answered before you even knew you had them. This particular
question is answered here:
http://perl.apache.org/guide/porting.html#Name_collisions_with_Modules_and

_____________________________________________________________________
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://apachetoday.com http://eXtropia.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/