http://qs321.pair.com?node_id=904792


in reply to staements in strings

CGI applications often need to generate output depending on input/parameters. One way of achieving this is to use a dispatch table. The following highly contrived example demonstrates how a dispatch table could be implemented (not using CGI or modules to keep it simple).
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $params = { param1 => 1, param2 => 2, }; my ($case, $args) = process_params($params); my %dispatch_table = ( case1 => \&case1, case2 => \&case2, default => \&default, ); my $page_header = page_header(); my $body = $dispatch_table{$case}->($args); my $page_footer = page_footer(); print $page_header, $body, $page_footer; sub process_params{ my $params = shift; my $case = q{default}; my $args = {}; if ($params->{q{param1}} eq 1){ $case = q{case1}; } elsif ($params->(q{param2}) eq 2){ $case = q{case2}; } else{ $case = q{default}; } return $case, $args; } sub case1 { my $args = shift; my $output = qq{case one\n}; return $output; } sub case2 { my $args = shift; my $output = qq{case two\n}; return $output; } sub default { my $args = shift; my $output = qq{default\n}; return $output; } sub page_header { my $ph = qq{page header\n}; return $ph; } sub page_footer { my $pf = q{page footer}; return $pf; }
The "case" subs could call subs/methods in different (application) modules.

If this approach is suitable then something like CGI::Application would be worth looking at. It can help look after and simplify all this and much more. Combined with, say, HTML::Template, CGI applications, imo, become a whole lot easier and reusable. A little extra effort up front but well worth it.