Imagine that I want to load a module at runtime. I expected this to work
use warnings; use strict; eval { require Cwd; Cwd->import; }; if ( $@ ) { die "Can't load Cwd: $@ " } say "Dir: ", getcwd;
but this is not so, for Bareword "getcwd" not allowed ...
Cwd exports getcwd by default. I tried to name the function import , and I tried to use its other functions. It works with the full name say Cwd::getcwd , so I think it does not import.
This works as an attempt for several other core modules I tried, for example
use warnings; use strict; eval { require List::Util; List::Util->import('max'); }; if ( $@ ) { die "Can't load List::Util: $@ " } my $max = max (1, 14, 3, 26, 2); print "Max is $max\n";
NOTE added . Apparently, function calls with parentheses give the key to the compiler. However, in my opinion, the question remains, see EDIT at the end. In addition, a function like the first BLOCK LIST from the above module does not work.
However, it does not work for several (well installed) non-core modules that I tried. Worse and more vaguely, it does not work even with fully qualified names.
I can imagine that the character (function) used is not known at compile time if require used at run time, but it works for (other) base modules. I thought this was the standard way to load at runtime.
If I need to use full names for dynamic loading, then fine, but what is it with inconsistency? And how can I load (and use) non-core modules at runtime?
I also tried with Module::Load::Conditional , and this did not work.
What am I missing and how to load modules at runtime? (Tried 5.16 and 5.10.1 .)
EDIT
As noted by Matt Jacob , calling with brackets works getcwd() . However, considering perlsub
NAME LIST; # Brackets are optional if specified / imported.
this means that the import did not work, and the question of why remains.
Also, using a variety of syntax based on how the module loads is not very good. Also, I cannot get non-core modules to work this way, especially those that have syntax like List :: MoreUtils .