Re: Automatically add all defined functions to your @EXPORT
by ysth (Canon) on Jun 21, 2005 at 18:58 UTC
|
To pick up declared but not defined subs, use exists &$_ instead. Also, avoid all-uppercase names.*
Example:
package Foo;
use strict;
use warnings;
our @EXPORT = do {
no strict 'refs';
grep exists &$_ && /[a-z]/, keys %{ __PACKAGE__ . '::'};
};
sub bar;
sub AUTOLOAD {
our $AUTOLOAD;
print "AUTOLOADing $AUTOLOAD\n";
}
1;
*perlsub says:
Subroutines whose names are in all upper case are reserved to the Perl
core, as are modules whose names are in all lower case. A subroutine in
all capitals is a loosely-held convention meaning it will be called
indirectly by the run-time system itself, usually due to a triggered event.
Subroutines that do special, pre-defined things include AUTOLOAD, CLONE,
DESTROY plus all functions mentioned in perltie and PerlIO::via.
Update: changed [_a-z] to [a-z], based on the new CLONE_SKIP method; but perhaps that ought to be CLONESKIP... | [reply] [d/l] [select] |
Re: Automatically add all defined functions to your @EXPORT
by dragonchild (Archbishop) on Jun 21, 2005 at 13:58 UTC
|
As an example of what merlyn is talking about, you can do something like:
our @EXPORT = do {
no strict 'refs';
grep {
!/^_/
} grep {
defined &$_
} keys %{ __PACKAGE__ . '::'};
};
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
| [reply] [d/l] |
|
our @EXPORT = do {
no strict 'refs';
grep {
defined &$_ and not /^_/;
} keys %{ __PACKAGE__ . '::'};
};
I mean, you already have a grep there. Use it. {grin}
| [reply] [d/l] [select] |
|
This is an interesting style difference. I prefer putting one very specific item per map or grep. I find it makes both comprehension and maintenance easier in the long run. Maybe, this is a meditation in the making?
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
| [reply] |
|
|
|
|
|
|
|
| [reply] [d/l] |
|
The do is to introduce a new scope during an assignemnt. "not work for me" is rather vague - you want to elaborate, possibly with the actual error message?
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
| [reply] |
|
Re: Automatically add all defined functions to your @EXPORT
by tlm (Prior) on Jun 21, 2005 at 15:26 UTC
|
Not only very handy, but IMO, a textbook example of the Snippet genre.
I will make a tiny modification for my use:
grep +( defined &$_ and !/^_/ ), keys %{ __PACKAGE__ . '::'};
...to keep _private_functions and __ANON__...s unexported.
Update: I just realized that what I wrote above is almost identical to merlyn's earlier earlier follow-up... D'oh!
| [reply] [d/l] |
|
__ANON__
I realize you struck that out, but I just tested, and even though creating an anonymous subroutine pokes an __ANON__ into the symbol table, the defined test fails, so this would never pick up on that. Unless you happened to also call a subroutine __ANON__.
| [reply] |
|
Well, I'm glad you checked, because I had a (rather embarrassing) bug in my test, which incorrectly showed the __ANON__s as passing the defined &$_ .
| [reply] |
Re: Automatically add all defined functions to your @EXPORT
by dragonchild (Archbishop) on Sep 21, 2005 at 02:03 UTC
|
Note - if you're wanting to generalize this to any package name, you would do something like :
my @function_names =
grep {
!/^_/ && /^[a-z_]+$/
} grep {
exists &{${ $CLASS . '::'}{$_}}
} keys %{ $CLASS . '::'};
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
| [reply] [d/l] |