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

hello all,

a want to configure the AnyDBM_File to use a certain db lookup scheme, just like in the POD:

BEGIN { @AnyDBM_File::ISA = qw(DB_File GDBM_File NDBM_File ODBM_File) +}

could I put this snippet of code in a CONFIG.pm module (or any module that has my configurations for the program) and then call both the CONFIG.pm and the ANYDB_File module in a program, like this:

use CONFIG; use AnyDBM_File;

and have it do what i want it to do, which is have the AnyDBM_File obey what's in the CONFIG file?

If not, is there a way? This is for a CGI program I release, so I never know what kind of DB thingy they got going on, but I want users to change what db package is used.

-justin simoni
!skazat!

Replies are listed 'Best First'.
Re: AnyDBM_File and configing in a different module.
by AgentM (Curate) on Oct 17, 2000 at 02:49 UTC
    You don't actually need to assign to @AnyDBM::ISA in the percompilation stage, it will manage just fine later.- which is what you want- since you want the user input (from the CGI or some other method described below) The way I see it, you have a few options:
    1. Store the settings in a config file unique to your program- in this case I would recommend a file called /etc/MyProg.conf where you save the settings for later use. In the case of CGI, you'll want to start up by reading this file and keeping it in a configuration hash for checks whenever necessary. This way, the user has direct access to the configuration without going through some program goo. Since you are reading a pretty much static file every time you load your CGI, you should consider fastCGI or a mod_perl configuration.

      In /etc/MyProg.conf:

      
      UseDBType SDBM_File
      
    2. Always default to SDBM_File (or some other DB_File included in the default base Perl install)- choose one that's optimized for your purposes; most books have a nifty table displaying good/bad
    3. Have the program chose based on Perl's config hash. does it have the optimum performance for your use and does it exist? No, move on to second choice, etc.
    The thing I would stay away from is exactly what you do: specifying a user-definable variable in a perl script (not mentioning that a bunch of variables wouldn't be most practical in a .pm module but rather in a simple .pl and then a quick require ...). You probably want it more user-friendly than that. Of course, the optimal solution would be to offer an option right in your CGI script which would collect all of the available DB_File stuffs, throw them into a menu and let the user decide- but this is usually not the most practical unless you are writing a full-blown Web App. if you're unsure, stick with the file since perl code may be cryptic and scary to some users, even if you put a "Change this line here and nothing else" line into the code. have fun!
    AgentM Systems or Nasca Enterprises is not responsible for the comments made by AgentM- anywhere.