pbeckingham has asked for the wisdom of the Perl Monks concerning the following question:

I would like to emulate a module feature that I see in Test::More in my own module. I am able to set the number of tests on the same line that the use statement appears. For example:

use Test::More tests => 5;
What I'm seeing is that Test::More implements sub import, which has, as its 2nd and 3rd element of @_ the pair tests => 5, and so it uses splice to extract the pair, then punts the remainder of @_ using:
__PACKAGE__->_export_to_level(1, __PACKAGE__, @imports);

The question is: is this really the way it should be done? Is there a simpler way to achieve this? Thank you all. I am using 5.8.0 and 5.8.4.

Replies are listed 'Best First'.
Re: "use" modifiers
by Abigail-II (Bishop) on May 12, 2004 at 14:55 UTC
    The question is: is this really the way it should be done?
    There's little "should" in Perl. This is a way of doing it - the Test::More module is just taking the arguments it's interested in, and it's delegating any exporting to Exporter.

    Abigail

      Thank you. I understand the "should" issue - a side effect of there being MTOWTDI. I was just hoping that someone knows of a cleaner way to achieve this without intercepting the import process and fishing out the desired values. Of the many ways to do it, I was hoping that one would be more readable and obvious to the next guy that has to maintain my code.

      I would prefer clearer, more obvious code over a cryptic incantation with a paragraph of explanatory prose.

        Considering the only time (shy of doing a source filter) you get to the arguments of "use" is when "import" is called, there is no other way than "intercepting the import process". I don't see what's so non-clean about it - the import process is 100% Perl, and just involves a method call - a call that's as regular as any other method call.

        Of course, you could always to your own exporting - which might be as simple as the call to export_to_level. Everyone uses Exporter, but in 99% of the cases it could as easily be done without using Exporter.

        Abigail

Re: "use" modifiers
by bbfu (Curate) on May 12, 2004 at 16:15 UTC

    A technique that I've used for flags on the use line, but that doesn't work for pair options, is to use Exporter's @EXPORT_FAIL feature. Basically, if Exporter sees a value that is in @EXPORT_FAIL and not in either @EXPORT or @EXPORT_OK, it dispatches a call to $class->export_fail() to deal with the value.

    Alternatively, you could switch to the Exporter::VA module, which has several features geared exactly towards this sort of task.

    bbfu
    Black flowers blossom
    Fearless on my breath

Re: "use" modifiers
by perrin (Chancellor) on May 12, 2004 at 20:00 UTC
    In my opinion, abusing import like this is obfuscated. I think it would be better to just have an initialization method, since that would make it more obvious where to look when someone wants to change that code.