Suppose the module AAA::BBB::CCC
located in ~/modules/AAA/BBB/CCC.pm
,
and "~/modules
" is in @INC
, so why the following code doesn't work and lead to compile error?
$class = "AAA::BBB" ;
$type = "CCC";
require $class . '::' . $type ;
I try to use require AAA::BBB::CCC
instead, it works . If I do need dynamically require a module by combining strings together rather than hardcode the module name directly, how should I do ?
thanks
Best Solution
My approach:
Using eval, if you're only doing "die $@ if $@" doesn't make much sense - without eval it will work just as fine.
This additional step of making file_name, and requiring $file_name instead of class makes it not required to use string based eval, which tends to be rather slow.
Of course you still can use eval if you want to provide some kind of fallback:
But note, that this is block-based eval, and not a string-based one.