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

I am using a number of my own modules that have grown over several years.
The problem is that some are not modified very often and I forget what is exported from them.
Therefore I wondered in any Monk could tell me how, after loading a module
I can get a list of variables and subs exported from that module.
I appreciate that I can look in the original but I wanted to other things with the list that I obtained.
Many thanks

Replies are listed 'Best First'.
Re: What is Exported from modules
by Joost (Canon) on Jul 12, 2005 at 16:17 UTC
Re: What is Exported from modules
by gellyfish (Monsignor) on Jul 12, 2005 at 16:19 UTC

    If you want to see what is explicitly exported then (as long as you are not doing something hair-curling in your import) you can do something like:

    perl -MMyModule -e'print "@MyModule::EXPORT"'
    or some variation thereof.


Re: What is Exported from modules
by Codon (Friar) on Jul 12, 2005 at 16:28 UTC
    You could look in your namespace:
    use Some::Module; my %subs = map { $_ => \&$_ } grep { defined &$_ } keys %main:: ;

    Ivan Heffner
    Sr. Software Engineer, DAS Lead, Inc.
Re: What is Exported from modules
by gam3 (Curate) on Jul 12, 2005 at 16:21 UTC
    I don't think you can get all the imports from inside the module becuase you would not be sure what had been imported from the EXPORT_OK array. But by looking at @_module_::EXPORT and @_module_::EXPORT_OK you should be able to get a good idea of what was imported. The problem would be if you have 2 modules that export the name in the EXPORT_OK array.

    Update: Added example code.

    -- gam3
    A picture is worth a thousand words, but takes 200K.
Re: What is Exported from modules (explicit!)
by tye (Sage) on Jul 12, 2005 at 19:24 UTC


    use Some::Module;

    Instead either:

    use Some::Module qw( Things Used From This Module ); #or require Some::Module;

    and you won't have this problem. If you're confused, just think how confused your replacement will be.

    This is why module authors should use @EXPORT_OK and never @EXPORT.

    And when I say "never" remember this: All things in the strictest of moderation, including moderation.

    - tye        

      If you want to really confuse your replacement, do what my predecessor did: write class (has a new() method which returns a blessed reference) with methods that are named (by convention) as private ( sub _do_something { } ) or "double private" ( sub __double_secret_probation { } ) and then add them to @EXPORT. Oh, and while you're at it, add every subroutine to @EXPORT. That'll teach people to learn a convention and then have unfair expectations when they see it used elsewere.

      I think my predecessor lived (and may die) by this.

      Ivan Heffner
      Sr. Software Engineer, DAS Lead, Inc.
Re: What is Exported from modules
by broquaint (Abbot) on Jul 12, 2005 at 17:26 UTC
    To reliably find what was exported from another package you need to do a little symbol table interrogation e.g
    sub find_exported_symbols { my($in, $from) = @_; no strict 'refs'; my($in_tbl, $from_tbl) = ( \%{"$in\::"}, \%{"$from\::"} ); return grep { my $sym = $_; grep { \*$sym == \*$_ } keys %$from_tbl; } keys %$in_tbl; } use Symbol qw/ gensym qualify delete_package /; print "found $_ in main from Symbol\n" for find_exported_symbols( 'main', 'Symbol' );