You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "Nathanial P. Hendler" <eq...@retards.org> on 2004/05/27 20:13:00 UTC

[mp2] $dbh and config values methods

Ok, so I'm convinced that I should be using Log::Log4Perl.  I'm using
ModPerl::Registry and was wondering if people could share their approaches
to configuration variables and database handlers under mp2.

I'd like to have a config module, that holds things like:

  $document_root    = '/var/www/html';
  $images_base_path = $document_root . '/images';
  $images_base_url  = '/images';

  %phrases = ( red=>'This is red', blue=>'this is blue' );

But I can also think of reasons to have subroutines as well.  The module
would need to be available to all of the used modules, and I'd rather not
have to pass around a handle.  It also seems like it'd be nice if the
values were re-written, that it would only last for that session, but that
the change would be visible to all modules.

How do you guys deal with that?

What about a (or several) $dbh handle?  I'm going to have SQL queries in
all of my modules.  Do they all have to create their own $dbh?  Is there a
well understood approach to this that I'm not finding on the interweb?

Thanks again for all the help, I just seem to have a hard time building
complex perl systems the same way I used to under mod_perl 2.0, and need a
little experience and wisdom to make sure I head in mostly the right
direction.

Nathanial Hendler
Tucson, AZ USA
http://retards.org/


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: [mp2] $dbh and config values methods

Posted by Perrin Harkins <pe...@elem.com>.
On Thu, 2004-05-27 at 14:13, Nathanial P. Hendler wrote:
> I'd like to have a config module, that holds things like:
> 
>   $document_root    = '/var/www/html';
>   $images_base_path = $document_root . '/images';
>   $images_base_url  = '/images';
> 
>   %phrases = ( red=>'This is red', blue=>'this is blue' );
> 
> But I can also think of reasons to have subroutines as well.  The module
> would need to be available to all of the used modules, and I'd rather not
> have to pass around a handle.  It also seems like it'd be nice if the
> values were re-written, that it would only last for that session, but that
> the change would be visible to all modules.
> 
> How do you guys deal with that?

Well, first I would drop the crazy stuff about subroutines and changing
values in mid-flight.  That's not config and it belongs somewhere else.

There's a section on this in the guide:
http://perl.apache.org/docs/1.0/guide/porting.html#Configuration_Files__Writing__Dynamically_Updating_and_Reloading

I typically use one of the config file modules on CPAN and a simple
singleton pattern.  You could use Class::Singleton, or roll your own.

> What about a (or several) $dbh handle?  I'm going to have SQL queries in
> all of my modules.  Do they all have to create their own $dbh?  Is there a
> well understood approach to this that I'm not finding on the interweb?

It's been discussed here many times.  You either just connect in each
one, letting Apache::DBI handle the persistence, or you use a singleton
approach of some kind.  Check the mailing list archives for more.

- Perrin


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: [mp2] $dbh and config values methods

Posted by Perrin Harkins <pe...@elem.com>.
On Fri, 2004-05-28 at 02:51, Tom Schindl wrote:
> As Perrin already said Apache::DBI or once more a Singleton-Class 
> providing the DBH, although I'd use Apache::DBI because it handles 
> everthing for you.

I typically use them together.  Apache::DBI handles the persistence, and
the singleton provides easy access from any module that wants to use
it.  It also centralizes the db configuration in one spot.

I would caution anyone against rolling their own database handle
persistence system, using simple tricks with globals.  It's much better
to use DBI->connect_cached() if you are unable to use Apache::DBI for
some reason.

- Perrin


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: [mp2] $dbh and config values methods

Posted by Tom Schindl <to...@gmx.at>.
Nathanial P. Hendler wrote:
> Ok, so I'm convinced that I should be using Log::Log4Perl.  I'm using
> ModPerl::Registry and was wondering if people could share their approaches
> to configuration variables and database handlers under mp2.
> 
> I'd like to have a config module, that holds things like:
> 
>   $document_root    = '/var/www/html';
>   $images_base_path = $document_root . '/images';
>   $images_base_url  = '/images';
> 
>   %phrases = ( red=>'This is red', blue=>'this is blue' );
> 

To read config-files you could use 
http://search.cpan.org/~wadg/Config-IniFiles-2.38/. When it comes to the 
point of creating an Configuration-Class (e.g. to represent things like 
above) its common to use Singleton-Pattern.

> But I can also think of reasons to have subroutines as well.  The module
> would need to be available to all of the used modules, and I'd rather not
> have to pass around a handle.  It also seems like it'd be nice if the
> values were re-written, that it would only last for that session, but that
> the change would be visible to all modules.
> 
> How do you guys deal with that?
> 
> What about a (or several) $dbh handle?  I'm going to have SQL queries in
> all of my modules.  Do they all have to create their own $dbh?  Is there a
> well understood approach to this that I'm not finding on the interweb?
> 
> Thanks again for all the help, I just seem to have a hard time building
> complex perl systems the same way I used to under mod_perl 2.0, and need a
> little experience and wisdom to make sure I head in mostly the right
> direction.
> 

As Perrin already said Apache::DBI or once more a Singleton-Class 
providing the DBH, although I'd use Apache::DBI because it handles 
everthing for you.

> Nathanial Hendler
> Tucson, AZ USA
> http://retards.org/
> 
> 


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html