Dispatch Tables
At a previous job, I saw some code that asked the user which function they wanted to run and then executed a subroutine with that name. This code demonstrates why such a practice is bad:
use strict;
use warnings;
sub greet { print "Hello!\n" }
sub inquire { print "How are you?\n" }
sub bye { print "Farewell!\n" }
sub delete_all_files { print "*KABOOM*\n" }
sub insecure_call {
no strict 'refs';
shift->();
}
insecure_call('greet');
insecure_call('inquire');
insecure_call('bye');
insecure_call('delete_all_files');
Output:
Hello!
How are you?
Farewell!
*KABOOM*
One solution to this is the dispatch table. With a dispatch table, you define up front which calls are legal for an outsider to make:
more
I cover dispatch tables in Mastering Perl along with other such subroutine tricks. :)
Just put a prefix on the names of functions.
Now if you have a sub
delete_all_files
there is no way to call it throughsecure_enough_call
.