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

Im using Exporter to write a module that exports some constant values (by default), aswell as subs on request. It works fine if I just do this:

use Foo; printf "Value is: %d\n",SOME;

However when I actually specify a sub to import my default constants are no longer imported. eg. the following breaks:

use Foo qw(bar); printf "Value is: %d\n",SOME;
The module looks something like this:
package Foo; use strict; our (@ISA,@EXPORT,@EXPORT_OK); BEGIN { require Exporter; @ISA = qw(Exporter); @EXPORT = qw(SOME CONST VARS); @EXPORT_OK = qw(bar); }; use constant ( SOME => 1 ,CONST => 2 ,VARS => 42 ); sub bar { . . . } 1;
parsing XML is easy, said the data modeler to the programmer...you just look for an angle bracket and...

Replies are listed 'Best First'.
Re: exporter question
by DrHyde (Prior) on Jul 28, 2008 at 10:18 UTC

    If you're going to release your code to the CPAN, *please* consider whether you really need to export anything by default. In my opinion it's a really bad idea, because even if you carefully document what you're doing so that the user avoids writing anything that will clash with your exported symbols, he may be using other modules which export the same symbols. That leads to incredibly hard-to-debug problems. Much better IMO to make the user write something like ...

    use Some::Module qw(:CONSTANTS);

    Also bear in mind that someone might be using Exporter::Renaming, which relies on you actually using Exporter to do your exports. So even though Exporter is a bit of a pain sometimes, please don't be tempted to use anything else :-)

Re: exporter question
by Anonymous Monk on Jul 27, 2008 at 12:25 UTC
Re: exporter question
by CountZero (Bishop) on Jul 28, 2008 at 18:47 UTC
    The documentation is quite clear on this matter:
    How to Import

    In other files which wish to use your module there are three basic ways for them to load your module and import its symbols:

    use ModuleName;

    This imports all the symbols from ModuleName's @EXPORT into the namespace of the use statement.

    use ModuleName ();

    This causes perl to load your module but does not import any symbols.

    use ModuleName qw(...);

    This imports only the symbols listed by the caller into their namespace. All listed symbols must be in your @EXPORT or @EXPORT_OK, else an error occurs. The advanced export features of Exporter are accessed like this, but with list entries that are syntactically distinct from symbol names.

    Unless you want to use its advanced features, this is probably all you need to know to use Exporter.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: exporter question
by apl (Monsignor) on Jul 27, 2008 at 15:17 UTC
    It's line 42...
      No, its line 1, the use line. When you ask for a specific item, you only get that item, no defaults.

        that last reply probably answers my question...

        ~thanx

        parsing XML is easy, said the data modeler to the programmer...you just look for an angle bracket and...