develooper Front page | perl.perl5.porters | Postings from September 2022

Re: module loading built-in

Thread Previous | Thread Next
From:
Darren Duncan
Date:
September 17, 2022 20:30
Subject:
Re: module loading built-in
Message ID:
d5298eb3-c25d-2f29-53e9-4aaa0d88686d@darrenduncan.net
On 2022-09-17 7:02 a.m., demerphq wrote:
> So all I see is a bunch of folks sticking this load function in an eval.

I interpret this that you see many people not understanding how to use the new 
function properly and that they incorrectly assume they have to put it in an 
eval to make it work like "require $foo" etc.  So that's up to education.

> Whatever, I don't care that much. But please don't call it "load". "module_load" 
> or "package_load".

Personally I'm not stuck on any particular name, and I welcome a separate 
discussion to bikeshed the name, but on the RFC itself.

> I'm a serous anti-fan of lazy loading patterns in library level code, especially 
> in a web context it is an antipattern that slows things down and consumes excess 
> memory. I understand and appreciate that for scripts it can be the opposite. One 
> thing that would sell me on adding this if we came up with a way to resolve the 
> conflict between those two use cases. Can we figure out a way that a module 
> consumer can reliably tell modules that they should *pre*load its dependencies 
> and not lazy load them?

A key usage scenario I see for these new methods is when we have a library with 
a plugin architecture such that the specific plugin module we want to use is not 
known except at runtime and it is a user configuration data file etc that is 
naming the module to use.

For example, it would be a cleaner way to implement something like the DBI 
module which can use the new methods to load any DBMS driver module the user 
names via connect().

For another example, it would be a cleaner way to implement localization type 
modules that implement each user language as a Perl module, an example being my 
Locale::KeyedText module on CPAN.

With the new builtins available, I wouldn't have to resort to these shenanigans:

     sub template_module_is_loaded {
         my ($self, $module_name) = @_;
         ...
         no strict 'refs';
         return scalar keys %{$module_name . '::'};
     }

     sub load_template_module {
         my ($self, $module_name) = @_;
         ...
         # Note: We have to invoke this 'require' in an eval string
         # because we need the bareword semantics, where 'require'
         # will munge the package name into file system paths.
         eval "require $module_name;";
         $self->_die_with_msg( ... )
             if $@;
         return;
     }

-- Darren Duncan


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About