# How should I pass settings to a module in Perl that also exports symbols?

In Test::Version I want to add configuration settings that allow one to toggle certain features. I'm thinking about passing an anonymous hash to a custom import like so.

use Test::Version 1.2 qw( version_ok ), { is_strict => '1', has_version => '0' };


other options I've thought of include package variables (though more ugly in my opinion).

use Test::Version 1.2 qw( version_ok );
$Test::Version::IS_STRICT = 1;$Test::Version::HAS_VERSION = 0;


or maybe using some other kind of operator to denote config settings on import, though I'm not sure exactly how to implement this.

use Test::Version 1.2 qw( version_ok ), +is_strict => '1', +has_version => '0';


Keep in mind that this has a simple functional interface, so passing objects, or using config files is overkill. Any preferences? or other suggestions?

• Check out Sub::Exporter. Dec 16 '11 at 22:03
• @BradGilbert I found Sub::Exporter to be a bit complicated and powerful for what I'm trying to achieve. Also, I wasn't able to actually discern how I would use it for this purpose. Dec 18 '11 at 8:00

I would stay away from changing the normal workings of import(). I've never seen a module mix exporting and configuration at the same time (maybe you have, though), so leave those out (options 1 and 3). I think you are right in thinking that option 2 isn't too pretty. That leaves us with none of those options.

I think instead of this you should just allow calling version_ok and version_all_ok with the options hash.

version_ok({ is_string => 1, has_version => 0 });


Clear and standard, nothing odd going on. From the way it seems like the module is going to be used this isn't a burden on the user (since there will normally only be one call to this in the entire test suite).

Besides passing the options to imported functions as aparker42 mentioned you could simply write a function configure within your package and deal with the options there.

package Test::version ;

#use vars qw($is_string$has_version) ;
# Instead of multiple configuration variables I personally would
# bundle them in a hash
# Filling the hash with defaults ... and it also makes the configure easier
use vars qw(%options) ;
%options = ( is_string => 0 ,
has_version => 1 ) ;

sub configure {
my ( $opthr ) = @_ ; foreach my$key ( keys %{ $opthr } ) { if( exists$options{$key} ) { # Check if the given variable exists in the %options hash$options{$key} =$opthr->{\$key} ;
}
}
}

# Rest of the code here ...

1;


And using the module with

use Test::version qw(version_ok) ;
Test::version::configure( { is_string => 1 } ) ;