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.)

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.