in reply to Getting name of sub from a hash-based dispatch table?
use constant PUBLIC => 0, PRIVATE => 1; %query = ( action1 => { visibility => PUBLIC, code => \&handler1, }, action2 => { visibility => PUBLIC, code => \&handler2, }, action3 => { visibility => PRIVATE, code => \&_do_not_advertise, }, ); my @public_actions = grep $query{$_}->{visibility} == PUBLIC, keys %query;
If that's too verbose, just make subs which do the same thing (e.g. register_private( action3 => \&_do_not_advertise )). Or encapsulate things in a class and make visibility a method.
Update: Or just have a register sub which takes an action name and a sub name and then adds the right elements.
sub register_action { my( $action, $subname ) = @_; no strict 'refs'; $query{ $action } = { visibility => ($subname =~ /^_/), code => \&{$subname} }; }
|
|---|