Having a dispatch table to handle multiple operations is the basic strategy. But I think you want,
my %dispatch = (
home => \&home,
donate => \&donate,
news => \&news,
faq => \&faq,
....
);
instead of
my %dispatch = (
home => sub { \&home(); },
donate => sub { \&donate(); },
news => sub { \&news(); },
faq => sub { \&faq(); },
....
);
So you can say, for example,
my $default_action = 'home';
my $action = $cgi->param('a') || '';
# is $action recognized?
my $executor = $dispatch{$action} ||
$dispatch{$default_action};
&$executor();
When you specify
\&some_func(), you actually mean to call
some_func() and make a reference of whatever it returns. And that reference is what you get when you dispatch, for example,
$dispatch{home}->(). Some thing I'm sure not what you want.
Since I can't put a template inside a template
What's your exact needs regarding this? If you have another html file, wheter is static or another template, you can use the
<tmpl_include> command. This way, you need to process the placeholders in the included template file at the same level of the main template file. See the manpage for details.
... some html....
<tmpl_include name="other-template.html">
<tmpl_include name="other-static.html">
If what you need is some kind of multi processing template then you need to work this out yourself. One way is by having a number of template objects according to the number of included templates.
<!-- news.html -->
...some placeholders here...
<!-- end of news.html -->
<!-- faq.html -->
... placeholders for FAQ ...
<!-- end of faq.html -->
<!-- main.html -->
<html>
....
Today: <tmpl_var name="today">
<p><tmpl_var name="news">
<p><tmpl_var name="faq">
...
</html>
<!-- end of main.html -->
Now, you have to process the
news.html and
faq.html separately, getting their outputs and putting it in their respective placeholders when processing
main.html. Something like (imaginatively),
# assuming load_template() is defined somewhere
# to return a template object
my $faq = load_template('faq.html');
$faq->param(PARAMS_FOR_FAQ_PLACEHOLDERS);
my $news = load_template('news.html');
$news->param(PARAMS_FOR_NEWS_PLACEHOLDERS);
my $template = load_template('main.html');
$template->param(
today => scalar(localtime),
news => $news->output,
faq => $faq->output,
);
print $template->output;
Having said all of that, I really like to suggest you to try
CGI::Application. It would make your life easier :-) It shifts the dispatch table to some higher extend.
Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!