use strict; use CGI; use Template; $|++; # Dispatch table is a hash where # every value is a sub-reference # When the user doesn't provide # a run-mode, then list() is # triggered (default case, see # the last line my %dispatch_table = ( list => \&list, item => \&item, fetch_image => \&fetch_image, '' => \&list, ); my $query = new CGI; print $query->header; print $query->start_html( -title => 'A small example...' ); # Now we read the param mode from # the query. The string is used to # fetch the hash table. We got a # reference to a function, so we # can call it... my $mode = $query->param('mode'); &{ $dispatch_table{$mode}}; print $query->end_html; sub list { print "

List mode

\n"; my $action = $query->param('action'); # What we can do with $action? # Another dispatch table here is not # possible (one can't define procedures # local to other procedures, like in # Pascal). Another possibility is to # define other procedures "at the same # level" of list(), item() and so on... # but it isn't elegant and easily maintanable. # So we could use different # packages for different "top level run-modes". # Or, simply, we can cope with $action # here, with a if-elsif-else construct. } sub item { print "

Item mode

\n"; } sub fetch_image { print "

Fetch_image mode

\n"; }