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.


In reply to Re: staements in strings by wfsp
in thread staements in strings by PriNet

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.