I'm trying to switch some use statements in my code to require for modules I only use if certain conditions are met (e.g. the program might be loaded 1,000 times between times those modules are needed), in order to lower the overhead and improve the execution speed of my program (I've been using Devel::NYTProf to profile my code and some modules were adding significant execution time even when I didn't use them in a certain session).
I've run into an issue where the script dies on load because symbols aren't defined (that won't be defined until that module is imported). Is there a way to tell Perl that the symbols will be imported at runtime? A simple example: I only want to use Data::Dumper if I call a routine that triggers it:
sub dataDumper {
require Data::Dumper;
Data::Dumper->import;
say STDERR Dumper @_;
}
However, calling Dumper on that third line causes an exception. I can work around that by referring to Data::Dumper->Dump instead. However, a similar issue with LWP::UserAgent proves a bit sticker because LWP::UserAgent imports HTTP::Request. I haven't come up with a variation of my $request = new HTTP::Request GET => $params->{'url'}; that avoids throwing an exception.
Assuming I can that to work, is there any reason why this approach is a bad idea? Am I losing any sort of optimizations by not loading these modules at compile time? On a module like LWP, it seems like an easy call -- I just don't use it much. I'm a bit more confused on whether to try to dynamically switch between (for example) Text::Textile and Text::MultiMarkdown (I usually only need one per execution.