Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How to dynamically invoke one of several similarly named modules

by 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

- 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.

Replies are listed 'Best First'.
Re: How to dynamically invoke one of several similarly named modules
by stevieb (Canon) on Aug 22, 2022 at 18:14 UTC

    If all the classes do exactly the same thing except they point to a different database, then the way you propose seems to be a whole lot of code duplication across several files for no reason (this will become a maintenance and testing nightmare).

    I'd create a single class/module, and select the database when loading the module (or on object creation) based on whatever condition you need (environment variable, object instantiation parameter etc). For this, you can use if statements, a dispatch table etc.

    Using if statements or a dispatch table is by far more elegant than having multiple modules all doing the same thing. Besides, even in your example you posted, you're still going to have to use if statements; at the module loading level instead of database selection level.

      Yes, that does sound more reasonable. I was stuck on the examples I was shown but I suppose I needed a second pair of eyes.

      Much appreciated!

        The pseudo-code for the way I imagine implementing this is something like:

        use DataBaseDriver; ... use constant CITIES => qw(lon par); use constant APPLICATIONS => qw(test prod); # build dispatch table for database handles my %db_handle; for my $city (CITIES) { for my $app (APPLICATIONS) { $db_handle{$city}{$app} = DataBaseDriver->new or die "'$city:$app' driver creation failed: ", DataBaseDriver->error_msg; } } ... # use database handle dispatch table my $msg = '...'; my $city = '...'; my $app = '...'; my $dbh = $db_handle{$city}{$app} or die "'$city:$app' driver access failed"; $dbh->store_to_database($msg); ...


        Give a man a fish:  <%-{-{-{-<

Re: How to dynamically invoke one of several similarly named modules
by kcott (Archbishop) on Aug 23, 2022 at 07:32 UTC

    G'day onemojofilter,

    Welcome to the Monastery.

    If the APIs for all of those *::LogTable classes are the same, I'd probably do something like this:

    { my %class_for = ( lonprod => sub { require App::London::Production::LogTable; return 'App::London::Production::LogTable'; }, parprod => sub { require App::Paris::Production::LogTable; return 'App::Paris::Production::LogTable'; }, ... ); sub get_class { $class_for{$_[0]}->() } } my $class = get_class($database); $class->find_and_create(...);

    — Ken

Re: How to dynamically invoke one of several similarly named modules
by Anonymous Monk on Aug 23, 2022 at 13:52 UTC

    If you are using at least Perl 5.10, the Module::Load module should be in core. It would allow an implementation something like this:

    # Arguments are city code ('lon' or 'par') and an environment ('prod' +or # 'test') use Module::Load qw{ load }; sub load_database_module { my ( $city, $envir ) = @_; state $city_map = { lon => 'London', par => 'Paris', }; state $envir_map = { prod => 'Production', test => 'Test', }; state $type_map = [ qw{ Prod` Test } ]; $city_map->{$city} or die "Invalid city '$city'"; $envir_map->{$envir} or die "Invalid envir '$envir'"; my $class = sprintf 'App::%s::%s::LogTable', $city_map->{$city}, $envir_map->{$envir}; load( $class ); return $class; }
Re: How to dynamically invoke one of several similarly named modules
by awncorp (Acolyte) on Aug 24, 2022 at 20:41 UTC
    Fwiw
    package main; use Venus::Match; use Venus::Space; my $name = 'lonprod'; my $match = Venus::Match->new($name); $match->just('lonprod')->then('App:London::Production::LogTable') $match->just('parprod')->then('App::Paris::Production::LogTable') $match->just('lontest')->then('App::London::Test::LogTable') $match->just('partest')->then('App::Paris::Test::LogTable') my $result = Venus::Space->new($match->result)->build->find_or_create( { column1 => $data{"column1"}, column2 => $data{"column2} } );
    "I am inevitable." - Thanos

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11146294]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2025-01-24 03:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (68 votes). Check out past polls.