The problem
Requiring/using/loading a module returns a true value. The value can be any type of true value. However the current implementation of ‘require' always returns a ‘1’ if the module was previous required, instead of the actual last value in the module:
Eg
# Some module
package My::Mod;
{key=>”value”}; # last value ’true'
# Main program
my $result=require My::Mod;
# somewhere else in the program
my $result2=require My::Mod;
# $result will be the hash ref from the module ‘last value’
# $result2 will always be ‘1’
Currently the user needs to create their own table of required modules to cache the return value, which seems a duplication of how ‘require' tracks already required modules and deals with the bareword and file path separator complications.
Syntax changes
None required
Benefits
1. A module can be used directly as configuration, or other advanced stateful purposes.
2. Gives a uniform way of returning a value from a module instead of assuming a particular name for a package variable.
3. No logical changes to existing modules returning true values. Backward compatible
Potential problems
Returning a reference value can allow the contents of to be modified after the module has been loaded. Which depending on your context could be desirable or undesirable. However this is no different to having a package variable for the same purpose
Thank you for your time and consideration.
Ruben Westerberg
Requiring/using/loading a module returns a true value. The value can be any type of true value. However the current implementation of ‘require' always returns a ‘1’ if the module was previous required, instead of the actual last value in the module:
Eg
# Some module
package My::Mod;
{key=>”value”}; # last value ’true'
# Main program
my $result=require My::Mod;
# somewhere else in the program
my $result2=require My::Mod;
# $result will be the hash ref from the module ‘last value’
# $result2 will always be ‘1’
Currently the user needs to create their own table of required modules to cache the return value, which seems a duplication of how ‘require' tracks already required modules and deals with the bareword and file path separator complications.
Syntax changes
None required
Benefits
1. A module can be used directly as configuration, or other advanced stateful purposes.
2. Gives a uniform way of returning a value from a module instead of assuming a particular name for a package variable.
3. No logical changes to existing modules returning true values. Backward compatible
Potential problems
Returning a reference value can allow the contents of to be modified after the module has been loaded. Which depending on your context could be desirable or undesirable. However this is no different to having a package variable for the same purpose
Thank you for your time and consideration.
Ruben Westerberg