in reply to modularizing database script?

Perl is quite good for DB interaction, you may want to have a look at a few tutorials here in the monastry (Tutorials): Reading from a database and The fine art of database programming.

Hope this helps, -gjb-