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

- if database = "lonprod" then call module App:London::Production::LogTable - if database = "parprod" then call module App::Paris::Production::LogTable - if database = "lontest" then call module App::London::Test::LogTable - if database = "partest" then call module App::Paris::Test::LogTable

That is to say, I'm trying to avoid having to do this:

if $database eq "lonprod" { App::London::Prod::LogTable->find_and_create( { column1 => $data{"column1"}, column2 => $data{"column2} } ); } elsif $database eq "lontest" { ... #etc }

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.


In reply to How to dynamically invoke one of several similarly named modules by onemojofilter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.