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

I seek wisdom on the following scenario:

My task master has requested that for each client of a web system I am building that we stash (key, value) pairs that are stored in a relational database (Postgres in this case) in Perl modules that can simply be use-d.

When values change in the table, the Perl modules would need to be regenerated.

There are roughly 100 pairs and perhaps five to ten are needed by each script in the system.

This seems unnatural to me because:

All the scripts need to connect to the RDBMS for other functionality and it seems that the cost of retrieving the config from the table each time as opposed to reading the module is only a few thousandths of a second.

Has anyone worked with a scenario like this? Please shed the bright light of your wisdom on the darkness of my situation!

  • Comment on Stashing DB Key/Value Pairs in Perl Modules

Replies are listed 'Best First'.
Re: Stashing DB Key/Value Pairs in Perl Modules
by Tanktalus (Canon) on Apr 21, 2005 at 21:53 UTC

    I take it you're talking about something like:

    my $perl = "package Data;\nour %data = (\n"; my $sth = $dbh->prepare('select key,value from data'); $sth->execute(); while (my ($key,$value) = $sth->fetchrow_array()) { $perl .= "\t$key\t=>'$value',\n"; } $perl .= ");\n1;\n"; open my $fh, '>', 'Data.pm'; print $fh $perl;
    (plus error checking) to dump the database? Yeah, I'd say it was unnatural. There's a reason why databases were invented. This is one of them. Use it. ;-)

Re: Stashing DB Key/Value Pairs in Perl Modules
by mpeters (Chaplain) on Apr 22, 2005 at 00:09 UTC
    If your boss insists on using a hash to access these key value pairs you could always create a hash that is tied to the database. (look at Tie::RDBM) Did your boss give you any reasons for wanting you to do this?
Re: Stashing DB Key/Value Pairs in Perl Modules
by 5mi11er (Deacon) on Apr 21, 2005 at 21:06 UTC
    I'm confused. How do you store data in a Postgres DB INSIDE a Perl module?

    If Perl (module, script, whatever) is reading from a (separate) DB, when the DB changes, the module/whatever doesn't need to be regenerated...

    -Scott

    Update: after reading the reply by Tanktalus, I'm no longer confused about what the question was.

    I also whole heartedly agree, don't do it.