Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

dereferencing use module

by (Initiate)
on May 23, 2014 at 20:39 UTC ( #1087268=perlquestion: print w/replies, xml ) Need Help?? has asked for the wisdom of the Perl Monks concerning the following question:

I would like to use a variable in a "use" clause.
I have one script that runs on multiple servers, each taking its own <servername> file
Currently I have to copy the script and rename the file in the use clause for each server.
I would like to advoid this and do something like
my $variable = "<servername>_data";
use $variable;

Is there any way to do this ?

Replies are listed 'Best First'.
Re: dereferencing use module
by InfiniteSilence (Curate) on May 23, 2014 at 20:50 UTC

    Maybe what you really want to do is require?

    Celebrate Intellectual Diversity

Re: dereferencing use module
by skx (Parson) on May 24, 2014 at 00:01 UTC

    Although the other answers have already given you the help you need to solve your problem as-stated, I wonder if there isn't a better approach.

    If you're just loading a module on a per-host basis, to get configuration data, why not load a config file instead?

    Rather than loading the module just load and parse:

    • /etc/foo/default.conf
    • /etc/foo/$hostname.conf

    That way you could distribute all your config to all hosts - via Puppet, Chef, or for the perl-lovers Slaughter, and the right thing will happen..

Re: dereferencing use module
by LanX (Sage) on May 23, 2014 at 21:37 UTC
    Sounds like you only need a do $filepath (and maybe a call to import() ?)

    HTH :)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    PS: for a better understanding

    use > require > do > eval

    with > meaning "is a specialized usage of"


    added explanation + mention of import()

Re: dereferencing use module
by boftx (Deacon) on May 23, 2014 at 23:24 UTC

    This is my general approach when I need to do something similar, in this case, a poor man's Module::Pluggable system:

    # NOTE! This is just a snippet from a larger work shown only to illust +rate the principle use strict; use warnings use Cwd (); use Try::Tiny; # used to find the location of THIS module. assumes that all plug-ins # will be under a directory named after this module (without the '.pm' +) sub _basename_dir { my $class = shift; # In this case, we want to always use THIS package's name, not a s +ub-class my $package = __PACKAGE__; $package =~ s/::/\//g; my $packpath = $INC{ join( '.', $package, 'pm' ) }; $packpath =~ s/\.pm$//; my $realpath = Cwd::realpath($packpath); return $realpath; } # be sure to get the method in THIS package, not a sub-class my $basedir = __PACKAGE__->_basename_dir(); my $pdir = "$basedir/Plugin"; opendir( my $dh, $pdir)or die "Can not open $pdir"; my @plugins = grep { /\.pm$/ && -f "$basedir/Plugin/$_" } readdir($dh) +; closedir( $dh ) or die "Can not close $pdir"; for my $plugin ( @plugins ) { my $ppkg = try { my $package = __PACKAGE__; # gotta change from '::' to '/' so require is happy. $package =~ s/::/\//g; my $ppath = join( '/', $package, 'Plugin', $plugin); require $ppath; # and now we change back to normal '::' without the .pm to use + as a class name. $plugin =~ s{(?:\.pm)$}{}; my $pname = join( '::', __PACKAGE__, 'Plugin', $plugin ); return ($pname->isa( __PACKAGE__ ) && $pname->_enabled) ? $pna +me : undef ; } catch { warn "Invalid plugin: $plugin\n$_\n"; return; } or next; $ppkg->register(); }

    There are probably better or cleaner ways to do this (this is Perl, after all) but this has been a very stable approach for me.

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: dereferencing use module
by taint (Chaplain) on May 23, 2014 at 22:22 UTC
    My first thought was also as InfiniteSilence already indicated (using a require).

    I'd simply add a field/name/label for servername, and then apply a Conditional Operator (if, then, else) type block, if I required different variables depending the servername I was working on/with.

    Best wishes.


    λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2022-05-19 07:56 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (71 votes). Check out past polls.