in reply to Overriding Exporter::import

Beware. Exporter uses caller to figure out where to export things to. If you want to override it, you can but you need to erase yourself from the call stack with goto.

I have an example of how to do it at Versioned modules.

There are some documented ways to do it with Exporter. However when I looked at it about a year ago, it turned out that they were somewhat buggy. (Something about unused code not getting maintained, and other code expecting how many layers back to look for caller. I don't remember details though.) The goto trick is not.

  • Comment on Re (tilly) 1: Overriding Exporter::import

Replies are listed 'Best First'.
Re: Re (tilly) 1: Overriding Exporter::import
by John M. Dlugosz (Monsignor) on Jul 20, 2001 at 02:40 UTC
    re beware of caller:

    The normal import calls that internal function which is told how many levels back to go, so I think if used carefully it won't be any more buggy than normal :)

    If I localize @EXPORT and then do a magic call-stack goto, what happens? If it transfers localization to the new routine as well, I can easily jump to the "super" form without having to muck with the internal forms.

    —John

      When you localize something and then goto, it first has to clean up your scope before removing it from the callstack and so the local is lost.

      The documented approach if you are not calling Exporter's import directly is to call export_to_level. It is buggy in at least some versions of Perl with some exports. (I think it is fine if you are just exporting functions, but if you trigger Export::Heavy...) No, I don't remember exact details, but I do have a recollection floating around that it had to do with the rewrite for Exporter/Heavy.pm. You will have to analyze the source code for yourself if you want to know more.