ich muss gestehen, anstelle von switch lieber mit einem hash zu arbeiten... also wenn z.B. bei einem cgi immer ueber den parameter action an subroutinen verzweigt werden soll, dann verwende ich haeufig sowas wie das folgende:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package MyApp::Config;
use vars qw($ActionsHRef);
use Readonly $ActionsHRef =>
{
default => {
subroutine => \&ActionDefault,
template => 'action_default.tmpl',
},
overview => {
subroutine => \&ActionOverview,
template => 'action_overview.tmpl',
maxEntriesPerPage => 40,
},
# ...
};
package main; # hauptprogramm
use CGI;
my $cgi = CGI->new();
my $action = $cgi->param('action') || 'default'; # action nie 0
my $dbh = DBI->connect($dsn, $user, $pw)
or die ...;
my $subRef;
if( exists $MyApp::Config::ActionsHRef->{$action} ) {
$subRef = $MyApp::Config::ActionsHRef->{$action}->{subroutine};
} # if
else {
$subRef = $MyApp::Config::ActionsHRef->{'action_error'};
} # else
my ($templateFile, $params) = $subRef->( $cgi, $dbh, $action );
print $cgi->header();
my $template = &ReadAndFillTemplate( $templateFile, %DefaultParams, %$params );
$template->output();
# -----------------------------------------
sub ActionDefault {
my ($cgi, $dbh, $action) = @_;
# mach was...
my %params = (p1 => 'v1', p2 => 'v2');
return ($MyApp::Config::ActionsHRef->{$action}->{template}, \%params);
} # ActionDefault
oder so aehnlich