in reply to Looping on function names

You can hop through the symbol table at runtime. The following code is a bit ugly, but it works with my testing:
#!/usr/bin/perl -w use strict; my $name = "chromatic"; my $quest = "nefarious symbol wrangling"; sub findme { # just has to be defined return 0; } foreach (keys %main::) { # use whichever package you prefer no strict 'refs'; # else the next line fails if (defined *{$_}{CODE}) { print "$_\n"; # could be pushed onto an array, into a h +ash } }
The trick is knowing that you can access a symbol table with %main:: or %packagename:: or %:: for the current package.

The typeglob trickery in the defined line just looks to see if there's a CODE bit in the current package.

Personally, I'd just keep a hash of subroutine names and references around:

my %subs = ( mod_disk => \&mod_disk, mod_ram => \&mod_ram, mod_sticky_note => \&mod_sticky_note, mod_gum_wrapper => \&mod_gum_wrapper );

Replies are listed 'Best First'.
RE: (chromatic) Re: Looping on function names
by eduardo (Curate) on Jul 20, 2000 at 07:16 UTC
    i just wanted to say, that this:
    foreach (keys %main::) { # use whichever package you prefer no strict 'refs'; # else the next line fails if (defined *{$_}{CODE}) { print "$_\n"; # could be pushed onto an array, } # into a hash }
    both terrifies me, and amazes me. i guess, i know you can do that, and i'm pretty sure i've done it myself... but damn... next time anyone tells me perl isn't a real language that alows you to do powerful things, i'm going to shove this example of raw power down their throats.