This is a follow up to my prevous posting CGI Design

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.