Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: How to dynamically invoke one of several similarly named modules

by Anonymous Monk
on Aug 23, 2022 at 13:52 UTC ( [id://11146320]=note: print w/replies, xml ) Need Help??


in reply to How to dynamically invoke one of several similarly named modules

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; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found