in reply to Getting name of sub from a hash-based dispatch table?

There's usually a way with perl, but it's not necessarily nice ...
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
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 ;)
HTH

_________
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

    This is the type of thing I had in mind, though you're right broquaint, it's a touch more... verbose than I'd been hoping for.

    I guess this is Larry's waterbed theory at work - keeping the dispatch table structure simple just increases the work I have to do to determine some extra info from the sub names.

    Thanks,

    [Jon]