Put the config stuff into a hashref so that it doesn't get confused with symbols to be exported.
use MyModule { foo' => 'bar' }, qw/do_something do_something_else/;
Your import method would go something like this:
sub import { my $class = shift; my $config = ref($_[0]) ? shift : {}; # do stuff with $config here unshift @_, $class; # restore $class onto @_ for Exporter goto \&Exporter::import; # need to use "goto" to ensure tail call }
The alternative would be to use Exporter::TypeTiny which has a hook called _exporter_validate_opts exactly for doing this kind of thing...
package MyModule; use strict; use warnings; use base 'Exporter::TypeTiny'; our @EXPORT_OK = qw( something something_else ); sub _exporter_validate_opts { my ($class, $config) = @_; # do stuff with $config here }
Though it's worth noting that the $FOO variable here is basically a global. So if two different modules use your module, they will stomp on each others' config.
Rather than using a global variable to store your config, it's better to use a lexical, and then export functions that close over that lexical. Sub::Exporter is generally the go-to module for doing that sort of thing; Exporter::TypeTiny can as well, but currently the interface for doing so is not as clean. (It's on my todo list.)
In reply to Re: Importing methods and configuration at the same time?
by tobyink
in thread Importing methods and configuration at the same time?
by PopeFelix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |