Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Dynamically requiring a module

by balachandran (Novice)
on Jan 23, 2014 at 09:16 UTC ( #1071716=perlquestion: print w/replies, xml ) Need Help??
balachandran has asked for the wisdom of the Perl Monks concerning the following question:

I need to do something like this.

if ($ver > 2) { require 'Config'; } else { require 'Settings'; }

Config is actually the renamed newer version of Settings and both those modules expose the same constant(a hash named CONFIG, but the values are different). But I need this inside another module definition which uses the constants defined in those modules. $ver is a param to new() method of this module. So, I am not able to put it in a BEGIN block outside the module.

Since I am using a 'require', I get Can't use bare word CONFIG. Can someone help me with this please ?

Replies are listed 'Best First'.
Re: Dynamically requiring a module
by tobyink (Abbot) on Jan 23, 2014 at 09:28 UTC

    Probably easiest is to do something like:

    my $module; if ($ver > 2) { require Config; $module = "Config"; } else { require Settings; $module = "Settings"; } my $enable_foo = $module->CONFIG->{should_enable_foo};
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: Dynamically requiring a module
by choroba (Bishop) on Jan 23, 2014 at 09:30 UTC
    You have two options:
    1. Do not use the bareword. Change CONFIG to CONFIG() or &CONFIG wherever called.
    2. Define the sub CONFIG so that the parser knows it, then redefine it with constant. You might want to turn off warnings:
      sub CONFIG () {} { no warnings 'redefine'; if ($ver > 2) { require Config; Config->import; } else { require Settings; Settings->import; } }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      This works like a charm! Thanks. The other approach of renaming it to $module->CONFIG would mean a bit more change in a lot of places(though it is just find-replace)
Re: Dynamically requiring a module
by fishmonger (Chaplain) on Jan 23, 2014 at 15:16 UTC

    Another option besides what has already been given would be to use Module::Load.

    use Module::Load; my $config = 'Config'; my $settings = 'Settings'; my $ver = 3; load $ver > 2 ? $config : $settings;
Re: Dynamically requiring a module
by Bloodnok (Vicar) on Jan 23, 2014 at 14:33 UTC
    At its' simplest, would something along the lines of the following not suffice...
    my $module = $ver > 2 ? 'Config' : 'Settings'; require $module; sub get_conf_val { no strict qw/refs/; return ${"$module\::CONFIG"}{shift()}; } my $val = get_conf_val('conf_data_name');

    A user level that continues to overstate my experience :-))
Re: Dynamically requiring a module
by Anonymous Monk on Jan 23, 2014 at 14:33 UTC

    Consider this very-useful bit of glue: UNIVERSAL::require ... There are just enough gotchas in requiring things, to make you glad to have this lil helper.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1071716]
Approved by tobyink
Front-paged by MidLifeXis
and the rats come out to play...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2018-06-23 20:20 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.