http://qs321.pair.com?node_id=122323

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

Hi folks,

I'd like to export all the subs from a module (as you probably guessed from the title). The module's something I'm knocking together for my own use, so I'm not worried about namespace pollution.

Can anyone suggest a succinct way to do this?

Cheers,
andy.

Replies are listed 'Best First'.
Re: Export all subs from a module
by davorg (Chancellor) on Oct 31, 2001 at 16:35 UTC

    All you need to do is to build your @EXPORT array dynamically after defining all of your functions.

    use strict; package MyModule; use vars qw(@ISA @EXPORT); require Exporter; @ISA = qw(Exporter); sub test { print "In test\n" }; sub test_again { print "In test_again\n" }; sub last_test{ print "In last_test\n" }; while (my ($name, $glob) = each %MyModule::) { push @EXPORT, $name if defined *$glob{CODE}; } 1;
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

        That's nice, but it potentially exports too much - not just the subs, but everything in stash.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you don't talk about Perl club."

        To do this in a way that doesn't export package vars as well (which I believe was davorg's objection), perhaps this would work:

        @EXPORT_OK = grep {defined *{$MyPackage::{$_}}{CODE} } keys %MyPackage::;

        And unless I'm very much mistaken, that line could just as well be at the top of your package as at the bottom: unless you're doing fancy run-time redefinition of subs, they should all be defined by the time the first line of non-begin-block code gets evaluated.



        If God had meant us to fly, he would *never* have given us the railroads.
            --Michael Flanders

Re: Export all subs from a module
by MZSanford (Curate) on Oct 31, 2001 at 17:33 UTC
    If you are going so far as to export all subs, without reservation, you could also use package main;, or even define your subs as :
    sub ::mysub { 1; }

    please note this post does not endorse or condem exporting all subs
    i had a memory leak once, and it ruined my favorite shirt.
Re: Export all subs from a module
by graq (Curate) on Oct 31, 2001 at 15:57 UTC
      Done that, and some other searches elsewhere, before I posted. Of course, it's entirely possible (likely, even!) that I've missed the crucial section, in which case could you point me towards it.

      The problem isn't that I don't know how to export subs, it's that I want to export all the subs automatically, e.g. @EXPORT = All_the_subs() not @EXPORT = qw(long tedious list)

      andy. Going to the dentist now. Preemptive Ow!

        Apologies, I did not spot that detail in your original post.

        I like your question (probably because the answer does not spring to mind). I have seen many modules with HUGE lists of functions in EXPORT_OK, but none with something clever.

        Off the top of my head, the possibility that springs to mind is a hash of subrefs, but that kind of defeats the point of the exercise.

        /me scratches head and goes looking

        <a href="http://www.graq.co.uk">Graq</a>