require "Text::CSV" is much different than require Text::CSV. For your code to work, you'd need to s-::-/-g on $module for the require (but not for the import).
You can also use a lexical. Note that my declarations happen when the statement is compiled but my initializations happen when the statement is run. So:
Using eval "use $module" avoids this complication. Which to prefer is mostly a matter of taste (unless there is "risk" of $module ending up containing something strange). - tyeuse strict; my $module; BEGIN { $module= ... ? 'Text::CSV' : 'Text::CSV_XS'; my $file= $module; $file =~ s-::-/-g; require "$file.pm"; $module->import(); } my $csv = $module->new();
In reply to Re^2: using a variable to specify which perl module to use (bareword)
by tye
in thread using a variable to specify which perl module to use
by princepawn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |