require v.s. do in modperl

2001-08-01 Thread Philip Mak

I have a CGI application where I do:

require 'db.pl';

where db.pl defines some functions and variables related to connecting to
the database, and then executes C$dbh = DBI-connect(...).

I tried to convert this application to modperl, but I ran into the problem
that require did not execute db.pl again the second time I called the
script, so that the C$dbh = DBI-connect(...) line was not executed.

I can get around this by changing Crequire to Cdo, but is that the
correct way of doing things? It seems a waste to redefine all the
subroutines and variables again. But I do need it to reinitialize $dbh
when Crequire 'db.pl'; is called.

What should I do?




Re: require v.s. do in modperl

2001-08-01 Thread Perrin Harkins

 I have a CGI application where I do:

 require 'db.pl';

 where db.pl defines some functions and variables related to connecting to
 the database, and then executes C$dbh = DBI-connect(...).

snip

 I can get around this by changing Crequire to Cdo, but is that the
 correct way of doing things?

No.  Put the connect stuff in a subroutine and call it from your
application.  Things in the main section of a required file are only
supposed to run once.

- Perrin




Re: require v.s. do in modperl

2001-08-01 Thread Gunther Birznieks

For what you are trying to do, you should turn it into a module. Sorry for 
the short post, I've gotta split...

Although it's not user friendly, my more constructive hint is to type 
perldoc perlmod to get a quick tutorial on writing a module.

At 06:56 PM 8/1/2001 -0400, Philip Mak wrote:
I have a CGI application where I do:

require 'db.pl';

where db.pl defines some functions and variables related to connecting to
the database, and then executes C$dbh = DBI-connect(...).

I tried to convert this application to modperl, but I ran into the problem
that require did not execute db.pl again the second time I called the
script, so that the C$dbh = DBI-connect(...) line was not executed.

I can get around this by changing Crequire to Cdo, but is that the
correct way of doing things? It seems a waste to redefine all the
subroutines and variables again. But I do need it to reinitialize $dbh
when Crequire 'db.pl'; is called.

What should I do?

__
Gunther Birznieks ([EMAIL PROTECTED])
eXtropia - The Open Web Technology Company
http://www.eXtropia.com/




Re: require v.s. do in modperl

2001-08-01 Thread Gunther Birznieks

At 07:16 PM 8/1/2001 -0400, Perrin Harkins wrote:
  I have a CGI application where I do:
 
  require 'db.pl';
 
  where db.pl defines some functions and variables related to connecting to
  the database, and then executes C$dbh = DBI-connect(...).

snip

  I can get around this by changing Crequire to Cdo, but is that the
  correct way of doing things?

No.  Put the connect stuff in a subroutine and call it from your
application.  Things in the main section of a required file are only
supposed to run once.

I am not sure, but I don't think connect() is only supposed to run once 
especially with Apache::DBI?





Re: require v.s. do in modperl

2001-08-01 Thread Nick Tonkin


On Wed, 1 Aug 2001, Philip Mak wrote:

 I have a CGI application where I do:
 
 require 'db.pl';
 
 where db.pl defines some functions and variables related to connecting to
 the database, and then executes C$dbh = DBI-connect(...).
 
 I tried to convert this application to modperl, but I ran into the problem
 that require did not execute db.pl again the second time I called the
 script, so that the C$dbh = DBI-connect(...) line was not executed.
 
 I can get around this by changing Crequire to Cdo, but is that the
 correct way of doing things? It seems a waste to redefine all the
 subroutines and variables again. But I do need it to reinitialize $dbh
 when Crequire 'db.pl'; is called.
 
 What should I do?


One of the things you should do that I have not yet seen mentioned is
look into using Apache::DBI so you don't have it reinitialize $dbh on
every request. 

If you do have multiple DBs in your application you can still used cached
database handles; just name them differently.

- nick




Re: require v.s. do in modperl

2001-08-01 Thread Gunther Birznieks

At 07:18 PM 8/1/2001 -0700, Nick Tonkin wrote:

On Wed, 1 Aug 2001, Philip Mak wrote:

  I have a CGI application where I do:
 
  require 'db.pl';
 
  where db.pl defines some functions and variables related to connecting to
  the database, and then executes C$dbh = DBI-connect(...).
 
  I tried to convert this application to modperl, but I ran into the problem
  that require did not execute db.pl again the second time I called the
  script, so that the C$dbh = DBI-connect(...) line was not executed.
 
  I can get around this by changing Crequire to Cdo, but is that the
  correct way of doing things? It seems a waste to redefine all the
  subroutines and variables again. But I do need it to reinitialize $dbh
  when Crequire 'db.pl'; is called.
 
  What should I do?


One of the things you should do that I have not yet seen mentioned is
look into using Apache::DBI so you don't have it reinitialize $dbh on
every request.

If you do have multiple DBs in your application you can still used cached
database handles; just name them differently.

But you should still call $dbh = connect() on every request so that 
Apache::DBI's magic can truly work??

Otherwise ping tests and other such stuff will not work and you may as well 
not use Apache::DBI at all and just use BEGIN { $dbh ||= connect() } which 
of course, is probably not working well.






Re: require v.s. do in modperl

2001-08-01 Thread Perrin Harkins

Gunther Birznieks wrote:
 
 At 07:16 PM 8/1/2001 -0400, Perrin Harkins wrote:
   I have a CGI application where I do:
  
   require 'db.pl';
  
   where db.pl defines some functions and variables related to connecting to
   the database, and then executes C$dbh = DBI-connect(...).
 
 snip
 
   I can get around this by changing Crequire to Cdo, but is that the
   correct way of doing things?
 
 No.  Put the connect stuff in a subroutine and call it from your
 application.  Things in the main section of a required file are only
 supposed to run once.
 
 I am not sure, but I don't think connect() is only supposed to run once
 especially with Apache::DBI?

Right, and at the moment he has it in the main section, so it's only
running once.  He should move it to a sub and call it from his
application so it gets run every time.