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

I created a module from which I want to forcibly export a variable and optionally export a function. Problem is I seem to be able to either export forcibly (@EXPORT) or optionally (@EXPORT_OK) but not both.
package testpack ; use strict ; use Exporter ; our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, $xyz) ; $VERSION = 0.99 ; @ISA = qw(Exporter) ; @EXPORT = qw($xyz) ; @EXPORT_OK = qw(&rtine) ; $xyz = 888 ; sub rtine { $xyz = 999 ; print "I\'m in the su-ub ! and xyz = $xyz \n" ; } 1;
Heres the importing script :
use strict ; BEGIN{$Exporter::Verbose=1} use testpack ('&rtine') ; print "I\'m before the sub and xyz = $xyz \n" ; &rtine ; print "I\'m after the sub and xyz = $xyz \n" ;
When I run it, it doesn't import $xyz..
Exporter::import('testpack', '&rtine') called at E:\Sites\packnew\cgi- +bin\test.pl
No matter how I twist and turn, I can't export forcibly the variable and optionally the function. Been at this for a day and a half. Anyone have a tip for me ? Phil.

Replies are listed 'Best First'.
Re: Exporting forcibly and optionally, can both be done ?
by chipmunk (Parson) on Jun 21, 2001 at 18:17 UTC
    Let's see what the documentation for Exporter has to say...
    Specialised Import Lists If the first entry in an import list begins with !, : or / then the list is treated as a series of specifications which either add to or delete from the list of names to import. They are processed left to right. Specifications are in the form: [!]name This name only [!]:DEFAULT All names in @EXPORT [!]:tag All names in $EXPORT_TAGS{tag} anonymous li +st [!]/pattern/ All names in @EXPORT and @EXPORT_OK which m +atch A leading ! indicates that matching names should be deleted from the list of names to import. If the first specification is a deletion it is treated as though preceded by :DEFAULT. If you just want to import extra names in addition to the default set you will still need to include :DEFAULT explicitly.
    The last sentence there reveals the solution. To import all the @EXPORT names and some @EXPORT_OK names, you would do something like this: use testpack qw(:DEFAULT &rtine);
      Thanks for your reply. Using :DEFAULT works. I'm kinda new to perl. I had read that documentation, many times actually. But it's not exactly easy to grasp and after a few hours inverting variables, you mind gets screwed. Thx again. Philippe.
Re: Exporting forcibly and optionally, can both be done ?
by Sifmole (Chaplain) on Jun 21, 2001 at 17:03 UTC
    I believe what is happening is that your module is doing both. However, the "use" line is specifying a specific list of items to import. This list overrides any "forcibly" imported items. Thus, you can do
    use Foo ();
    And cause even the items in the @EXPORT to not be imported. You might want to check out the use of %EXPORT_TAGS to give you some more flexibility in this area.
Re: Exporting forcibly and optionally, can both be done ?
by Anonymous Monk on Jun 21, 2001 at 21:36 UTC
    From the look of it (source of 5.005/Exporter.pm) there is an undocumented variable %EXPORT which keys are like @EXPORT_OK, only they override @EXPORT_OK and @EXPORT in that they define which could optionally exported. The effect is if a symbol is in @EXPORT but not in %EXPORT you can it only forcibly export. ':DEFAULT' should result in: "$sym" is not exported by the $pkg module, for $sym=that symbol.

    I hope I made myself understood.