![]() |
|
Syntactic Confectionery Delight | |
PerlMonks |
How to dynamically invoke one of several similarly named modulesby onemojofilter (Novice) |
on Aug 22, 2022 at 17:59 UTC ( [id://11146294]=perlquestion: print w/replies, xml ) | Need Help?? |
onemojofilter has asked for the wisdom of the Perl Monks concerning the following question:
Perhaps I'm not going about this correctly, so I'll state my issue.
My goal is to write a perl script that will scrape log files and write to the appropriate database. (There are two possible locations - let's say they are London and Paris and two possible environments, Production and Test). To expand on the example, the databases themselves are referred to as lonprod, lontest, parprod, partest. I've been trying out 'Class::DBI'. I liked the apparent simplicity of implementing. It seems the way to use this is to create classes to connect to the database but it must be one to one. Meaning, you create one class for each database connection and then create classes for CRUD operations for each. It seems cumbersome to me but I decided to try it out. Now I'm thinking that when I need to connect to one database or another I wanted to avoid creating a lot of if statements to determine which database I need to create records in because I figured there was a more elegant way of going about this in perl. I thought that perhaps using something akin to an indirect reference would be the way to go. So I was thinking something like
That is to say, I'm trying to avoid having to do this:
Was thinking that perhaps this approach is too prone to error (since I'm repeating essentially very similar calls and maintenance might be a pain) I'm not a perl expert, nor am I a beginner per se, but let's say I'm hacking my way through some of this. Is there a way to declare a variable as a reference to one of these modules based on the value of the $database variable? Is this even a good idea, or is there a better way of doing this? Much appreciated.
Back to
Seekers of Perl Wisdom
|
|