Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Automatically export all subs in a module

by George_Sherston (Vicar)
on Dec 19, 2001 at 16:52 UTC ( [id://133106]=perlquestion: print w/replies, xml ) Need Help??

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

Just a little one, but it's been bothering me, so I crave indulgence of wise monks. I have a "module", which is nothing more than a repository for subroutines I'm working on. It's basically so I don't have to retrieve and save a huge script each time I edit, and to impose some loose-coupling discipline. When finished, the sub gets cut 'n' pasted into its final home.

So anyway, at the bottom of this module I have

our @EXPORT = qw/Whatever_I_happen To_be_working On_at_present/; And obviously I keep having to rewrite this line, as the contents of the module (called "devbox.pm") change. What I'd like to be able to do is automatically export everything in the name space. I've tried all the variations I can think of on our @EXPORT = qw/devbox::*/; to no avail. Is there a way?

§ George Sherston

Replies are listed 'Best First'.
Re: Automatically export all subs in a module
by andye (Curate) on Dec 19, 2001 at 17:14 UTC
    I had the same problem a while ago... Have a look at this thread:

    Export all subs from a module

    I ended up using this:

    @EXPORT = grep {defined *{$My::Module::{$_}}{CODE} } keys %My::Module: +: ;
    hth,
    andy.
      I like this, but I hate ever mentioning any datum twice -- it invites maintenance bugs.

      @EXPORT = do { my $pkg = \%My::Module::; grep { defined *{$$pkg{$_}}{CODE} } keys %$pkg };

          -- Chip Salzenberg, Free-Floating Agent of Chaos

        To be even more generic:
        @EXPORT = do { no strict 'refs'; my $pkg = \%{__PACKAGE__."::"}; grep exists &{$$pkg{$_}}, keys %$pkg; };
        chip - you're right - but we've *already* named the package once... so perhaps this would be better:
        @EXPORT = grep {defined *{${__PACKAGE__.'::'}{$_}}{CODE} } keys %{__PA +CKAGE__.'::'} ;
        Unfortunately you need to switch to no strict 'refs' in order to use it, which is a shame, but there you are (unless someone else can do it strictly).

        andy.

      Okey, now this one I like... because it DOESN'T export BEGIN. Now I'm REALLY happy :)

      § George Sherston
Re: Automatically export all subs in a module
by MZSanford (Curate) on Dec 19, 2001 at 17:03 UTC
    If i recall, the following will work :
    our @EXPORT = keys %devbox::;

    You were very close, but the qw// was non-interpolated. While this does work, i do not suggest doing such things. It is usually better, in my opinion, to create a real module and distribute, rather than copy/paste ... but, just my $0.02

    update : Added disclaimer.
    $ perl -e 'do() || ! do() ;' Undefined subroutine &main::try
Re: Automatically export all subs in a module
by merlyn (Sage) on Dec 19, 2001 at 19:52 UTC
    Exporting "everything" probably isn't right, but I've been known to do this (for production code, in fact):
    use vars qw(@EXPORT); ... sub foo { ... } push @EXPORT, 'foo'; ... sub bar { ... } push @EXPORT, 'bar'; ...
    and so on. Then when I'm adding or deleting a subroutine, the code to "export" it is right there.

    -- Randal L. Schwartz, Perl hacker

Re: Automatically export all subs in a module
by George_Sherston (Vicar) on Dec 19, 2001 at 17:55 UTC
    Thanks all. @EXPORT = keys %devbox::; works (and has the merit of concision) ... I shd have been able to work that out. Now, as a result of the Law of Unintended Consequences, I get a warning that I've redifined subroutine BEGIN... so I definitely am exporting EVERYTHING.

    § George Sherston
Re: Automatically export all subs in a module
by bmccoy (Beadle) on Dec 19, 2001 at 19:31 UTC
    Automatically exporting *everything* isn't necessarily a good thing. Being able to control what you export and what you don't is a Good Thing.

    -- Brett

    Go not to the Elves for counsel, for they will say both no and yes

      I agree, in almost all circumstances. My only motivation here is to avoid having to type the name of whatever routine I'm working on into my EXPORT list each time I start working on a new one (and avoid the irritation of forgetting to do this). Perhaps this is the Wrong Kind Of Laziness... but it IS a Kind of Laziness :)

      § George Sherston

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-23 13:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found