Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Export all subs from a module

by davorg (Chancellor)
on Oct 31, 2001 at 16:35 UTC ( [id://122331]=note: print w/replies, xml ) Need Help??


in reply to Export all subs from a module

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."

Replies are listed 'Best First'.
Re: Re: Export all subs from a module
by graq (Curate) on Oct 31, 2001 at 17:27 UTC

      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

        Thanks everyone for your help - I'll go with this method.

        Looks easy to export nearly-all the subs, like maybe all of them except the ones which start with underscores, which could be handy for the future.

        Cheers!
        andy.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://122331]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-24 00:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found