Thread Switch und Perlversion (11 answers)
Opened by morph at 2006-05-12 17:21

Strat
 2006-05-13 13:16
#66043 #66043
User since
2003-08-04
5246 Artikel
ModeratorIn
[Homepage] [default_avatar]
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:
Code: (dl )
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
perl -le "s::*erlco'unaty.'.dk':e,y;*kn:ai;penmic;;print"
http://www.fabiani.net/

View full thread Switch und Perlversion