Following the wonderful advice of Ovid I have moved his code example into my CGI script. So I went from this quickly growing IF/THEN/ELSE constuct...
if (defined($q->param())) { if ($q->param('action') eq 'add') { if ($q->param('confirm')) { if ($q->param('commit')) { &commit_dialog(); } else { &confirm_dialog(); } } else { &add_dialog(); } } elsif ($q->param('action') eq 'remove') { if ($q->param('confirm')){ if ($q->param('commit')) { &commit_dialog(); } else { &confirm_dialog(); } } else { &choose_dialog(); } } elsif ($q->param('action') eq 'modify') { if ($q->param('confirm')) { if ($q->param('commit')) { &commit_dialog(); } else { &confirm_dialog(); } } else { &choose_dialog(); } } else { &action_dialog(); } } else { &action_dialog(); }
To this rather cleaner Dispatch table from Ovid...
sub get_branch_function { my $query = shift; my $action = $query->param('action') || 0; my $confirm = $query->param('confirm') ? 1 : 0; my $commit = $query->param('commit') ? 1 : 0; my %dispatch = ( add => { confirm => { commit => \&commit_dialog, default => \&add_dialog, }, default => \&add_dialog, }, remove => { confirm => { commit => \&commit_dialog, default => \&confirm_dialog, }, default => \&choose_dialog, }, modify => { confirm => { commit => \&commit_dialog, default => \&confirm_dialog, }, default => \&choose_dialog, }, default => \&action_dialog ); if ( ! exists $dispatch{ $action } ) { return $dispatch{ default } } elsif ( ! $confirm ) { return $dispatch{ $action }{ default }; } elsif ( ! $commit ) { return $dispatch{ $action }{ confirm }{ default }; } else { return $dispatch{ $action }{ confirm }{ commit }; } }
Unfortunately this will only work if all actions have the same depth in the dispatch table. I need one of my actions to have an extra step.
my %dispatch = ( add => { confirm => { commit => \&commit_dialog, default => \&confirm_dialog, }, default => \&update_dialog, }, remove => { confirm => { commit => \&commit_dialog, default => \&confirm_dialog, }, default => \&choose_dialog, },
modify => { update => { confirm => { commit => \&commit_dialog, default => \&confirm_dialog, }, default => \&update_dialog, }, default => \&choose_dialog, },
default => \&action_dialog );
So I am again faced with replacing the simple logic in my (Ovid's) dispatch code with a large IF/THEN/ELSE.
sub dispatch_table { my $q = shift; my $action = $q->param('action') || 0; my $confirm = $q->param('confirm') ? 1 : 0; my $commit = $q->param('commit') ? 1 : 0; my $update = $q->param{'update') ? 1 : 0; my %dispatch = ( add => { confirm => { commit => \&commit_dialog, default => \&confirm_dialog, }, default => \&update_dialog, }, remove => { confirm => { commit => \&commit_dialog, default => \&confirm_dialog, }, default => \&choose_dialog, }, modify => { update => { confirm => { commit => \&commit_dialog, default => \&confirm_dialog, }, default => \&update_dialog, }, default => \&choose_dialog, }, default => \&action_dialog );
if (! exists $dispatch{$action}) { return $dispatch{default} } elsif (! $confirm) { return $dispatch{$action}{default}; } elsif (! $commit) { return $dispatch{$action}{confirm}{default}; } else { return $dispatch{$action}{confirm}{commit}; } }
Am I still just not getting it? I had lots of responses to my first question suggesting I move to CGI::Application. Which I may still do but I'd like to beable to solve this with using another module. I am willing to provide the entire CGI by emai/etc because it is rather large to be posting here. Any suggestions?
Thanks, -Dogma
Edit by tye (added <readmore>, "fixed" link)
In reply to Improved CGI Design by Dogma
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |