I am reading through O'Reilly's Perl Objects, References & Modules, more specifically its section about modules. It states that when using use Some::Module
you can specify an import list. From its explanation it seems that the only benefit of using this list is for the sake of keeping your namespace clean. In other words, if you have a subroutine some_sub
in your main
package and the loaded module has a sub with the same name, your subroutine will be overridden. However, if you specify an import list and leave out some_sub
from this list, you'll not have this conflict. You can then still run some_sub from the Module by declaring it like so: Some::Module::some_sub
.
Is there any other benefit than the one I described above? I am asking this because in some cases you load modules with loads of functionality, even though you are only interested in some of its methods. At first I thought that by specifying an import list you only loaded those methods and not bloating memory with methods you wouldn't use anyway. However, from the explanation above that does not seem the case. Can you selectively save resources by only loading parts of a module? Or is Perl smart enough to do this when compiling without the need of a programmer's intervention?
After an application loads a resource by using LoadResource, the system will unload the associated memory only when all references to its module are freed through FreeLibrary.
After a module is unloaded from memory, resource handles become invalid. An application can use FindResource and LoadResource to find and load any type of resource, but these functions should be used only in one of these situations: When the application cannot access the resource by using an existing resource-specific function.
Description The Save-Module cmdlet downloads a module and any dependencies from a registered repository. Save-Module downloads and saves the most current version of a module. The files are saved to a specified path on the local computer.
The files are saved to a specified path on the local computer. The module isn't installed, but the contents are available for inspection by an administrator. The saved module can then be copied into the appropriate $env:PSModulePath location of the offline machine. Get-PSRepository displays the local computer's registered repositories.
From use we see that use Module LIST;
means exactly
BEGIN { require Module; Module->import( LIST ); }
On the other hand, from require
Otherwise,
require
demands that a library file be included if it hasn't already been included. The file is included via the do-FILE mechanism, [...]
and do 'file'
executes 'file'
as a Perl script. Thus with use
we load the whole module.
"Importing" a sub means that its name is added (or overwritten) in the caller's symbol table (via the CODE
slot for the typeglob, normally aliased), by the package's import
function. The sub's code isn't copied. Now, import
can be written any way the author wants to, but generally the import list in the use
statement merely controls what symbols are brought into the namespace. The preferred way to provide import
in a module is to use the Exporter's import
method.
Selective importing relieves the symbol table (and perhaps some related mechanisms), but I am not aware of practical benefits of this. The benefits are related to programming, via reduced chances for collisions.
Another clear benefit is that it nicely documents what is used in the code.
Note that "import list" is just a convention. Module's import
function is free to do whatever it pleases with this list and you can see it (ab)used by many so-called pragma modules. Therefore partial loading is NOT bound to use
in any way. For example module can load heavy function stubs WHEREVER you've imported them or not and dynamically load heavy implementation on actual first call.
Therefore use
with partial import list may, or may not actually save any resources - it is all depends on actual implementation of use
d module.
While require
and use
indeed load entire .pm
file - that file well could be just a lightweight stub and loader for actual code located elsewhere. There's another convention to call those modules ::Heavy
.
Modules are free to implement partial loading in any way they please as well. Here are just some possibilities how module can save resources:
require
them at runtime in functions that depend on them instead of compile-time use
at the very start.Everything on this list could work automatically behind the scenes, exposed through use
import list, or work/be called in other, completely arbitrary way. Once again, it's completely up to module's implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With