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

This probably falls under the category of Don't Do It!(tm), but what the hay.

I've seen some modules in the past that used LIST args in import to activate different defaults for the usage of that module.

For example, let's say I have a Foo module that can be set to ignore or Carp errors during the course of its lifetime. I can add a method to toggle this behaviour:

use Foo; Foo->err_raise(1); Foo->err_raise(0);
or I can use 'use' (Who's on first?) funkyness to accomplish the same:
use Foo qw(:err_raise); use Foo qw(:err_ignore);

Besides the argument of whether it should or shouldn't be done, how is that accomplished?

Is it simply a matter of overloading import, or is there a cleaner way?

package Foo; use Exporter; @ISA = qw( Exporter ); ... sub import { my ($self, @args) = @_; ... check @args for :err_raise, :err_ignore change err handling defaults remove those fake tags from @args and call the real import ... $self->export_to_level(1, @cleanargs); };

Mod -1 points: Too Much Time On My Hands

Replies are listed 'Best First'.
Re: Exporter, import, and tags w/ double meanings.
by Zaxo (Archbishop) on Sep 25, 2002 at 20:03 UTC

    You can use Exporter's default methods for that if you define tags in %EXPORT_TAGS, like so:

    use vars qw( @EXPORT @EXPORT_OK %EXPORT_TAGS ); @EXPORT = (); @EXPORT_OK = qw( foo bar baz ); %EXPORT_TAGS = ( all => [qw( foo bar baz )], nobaz => [qw( foo bar )], nobar => [qw( foo baz )], nofoo => [qw( bar baz )], );

    After Compline,
    Zaxo

      That doesn't really solve his problem, err_raise and err_ignore aren't symbols he wants to export, or even sets of symbols he wants to export ... they are just flags, and if his client speciies those flags when importing his Module, he was arbitrary methods called.

      To answer that question, yes .. i believe writting your own import method which checks @_ for these special flags, takes the appropriate action, removes them from @_ and then delegates to export_to_level is the way to go.

      (Just don't forget that export_to_level expects you to leave "$self" in "@cleanargs" .. check the docs

Re: Exporter, import, and tags w/ double meanings.
by chromatic (Archbishop) on Sep 25, 2002 at 20:16 UTC

    Yep, that's how to do it. I'm not sure how you'd do it with %EXPORT_TAGS, but I generally write my own import() when necessary.

Re: Exporter, import, and tags w/ double meanings.
by Anonymous Monk on Sep 26, 2002 at 00:07 UTC
    I find that export_to_level is buggy. I prefer to munge @_ and then goto &Exporter::import.