I want to do something like this in Perl:
$Module1="ReportHashFile1"; # ReportHashFile1.pm
$Module2="ReportHashFile2"; # ReportHashFile2.pm
if(Condition1)
{
use $Module1;
}
elsif(Condition2)
{
use $Module2;
}
ReportHashFile*.pm contains a package ReportHashFile* .
Also how to reference an array inside module based on dynamic module name?
@Array= @$Module1::Array_inside_module;
Is there anyway I can achieve this. Some sort of compiler directive?
People have already told you how you can load the module with Perl primitives. There's also Module::Load::Conditional.
If you're looking to access an array of the same name no matter which module you loaded, consider making a method for that so you can skip the symbolic reference stuff. Give each module a method of the same name:
Then, when you load that module:
I don't know what you're really doing (XY Problem), but there's probably a better design. When things seem tricky like this, it's usually because you're overlooking a better way to to it.
You might find the
if
module useful for this.Otherwise the basic idea is to use
require
, which happens at run-time, instead ofuse
, which happens at compile-time. Note that 'As for addressing globals, it might be easier if you just exported the variable or a function returning it to the caller, which you could use by its unqualified name. Otherwise there's also the possibility of using a method and calling it as
$Module->method_name
.Alternatively, you could use symbolic references as documented in
perlref
. However, that's usually quite a code smell.Unless the execution speed is important, you can use string eval: