in reply to Getting name of sub from a hash-based dispatch table?
So as you can see that's not a particularly beautiful solution, although I could say the same for applying encapsulation to a simple dispatch table ;)sub lookup_subname { my($pkg, $sub) = @_; no strict 'refs'; return ( grep { $pkg->can($_) eq $sub } keys %{"$pkg\::"} )[0]; } sub foo; sub _bar; my %hash = ( foo => \&foo, bar => \&_foo ); for my $f (keys %hash) { my $name = lookup_subname __PACKAGE__, $hash{$f}; printf "%s is %s\n", $f, ( $name =~ /^_/ ? 'private' : 'public' ); } __output__ bar is private foo is public
_________
broquaint
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Getting name of sub from a hash-based dispatch table?
by theguvnor (Chaplain) on Jan 21, 2004 at 20:26 UTC |