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

Hi,monks.

Let's say we don't want hardcode the dsn,user and passwd. How could I pass the parameters dynamically.

Could I use import? Or you have any suggestions

Thanks

  • Comment on Class::DBI, how to pass dynamic connection string to set_db

Replies are listed 'Best First'.
Re: Class::DBI, how to pass dynamic connection string to set_db
by PodMaster (Abbot) on Sep 23, 2003 at 19:04 UTC
    Read the "Dynamic Database Connections" section of the manual.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Could you please explain the doc, as shown below

      Dynamic Database Connections

      It is sometimes desirable to generate your database connection information dynamically, for example, to allow multiple databases with the same schema to not have to duplicate an entire class hierarchy.

      The preferred method for doing this is to supply your own db_Main() method rather than calling set_db(). This method should return a valid database handle.

      Probably a example? Note however that this is class data, and that changing it may have unexpected behaviour for instances of the class already in existence.

Re: Class::DBI, how to pass dynamic connection string to set_db
by bart (Canon) on Sep 23, 2003 at 20:11 UTC
    Caveat: this approach I use for normal DBI apps, as I don't use Class::DBI myself. I hope it's still of some use.

    I use an entirely different approach than the others have represented here so far. I've written a small, private library file, similar in spirit to Config.pm, which contains the hardcoded database values. I called it DbConnect.pm. A function fetches database name, user name and password from the hash containing the hardcoded values, based on a parameter passed to a function. Like this:

    use DbConnect; use DBI; my $dbh = DBI->connect(DbConnect::connection("foo"), { printError => 0 +, raiseError => 1 }); ...
    Every site has its own version of this little module.
Re: Class::DBI, how to pass dynamic connection string to set_db
by princepawn (Parson) on Sep 23, 2003 at 18:42 UTC
    You might be using the wrong tool. Or you might ask on the Class DBI Mailing list at here

    Class::DBI is a tool for crafting applications, not for on-the-fly type stuff. You might look at DBIx::Recordset::Playground and DBIx::Connect for dynamic database usage.

    Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality.

      He's just asking about how to use his own method for creating database connections. Class::DBI supports that. You could easilly use DBIx::Connect with Class::DBI.
        Could you provide a high level example?