in reply to Re: Automatically export all subs in a module
in thread Automatically export all subs in a module

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

Replies are listed 'Best First'.
Re: Re: Re: Automatically export all subs in a module
by runrig (Abbot) on Dec 19, 2001 at 23:59 UTC
    To be even more generic:
    @EXPORT = do { no strict 'refs'; my $pkg = \%{__PACKAGE__."::"}; grep exists &{$$pkg{$_}}, keys %$pkg; };
Re: Re: Re: Automatically export all subs in a module
by andye (Curate) on Dec 20, 2001 at 00:08 UTC
    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're right - but I'd already noticed and fixed it (typo, honest! :). I was amused to see that we'd both looked at the post above and thought exactly the same thing.

        andy.

      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