in reply to Automatically export all subs in a module

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.

Replies are listed 'Best First'.
Re: Re: Automatically export all subs in a module
by chip (Curate) on Dec 19, 2001 at 23:43 UTC
    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.

        'no strict ...' is ok as long as you know what you're doing. And we're doing it under strictly controlled conditions here (if you scope it as I did above, or if you're a madman like Damian)...but it would be no strict 'refs' not vars :-)
        You chaps are the best... not seen such elegance since Paris fashion week... it's installed and working. May I suggest this is one for the Snippets section?

        § George Sherston
Re: Re: Automatically export all subs in a module
by George_Sherston (Vicar) on Dec 19, 2001 at 20:18 UTC
    Okey, now this one I like... because it DOESN'T export BEGIN. Now I'm REALLY happy :)

    § George Sherston