Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: staements in strings

by wfsp (Abbot)
on May 14, 2011 at 08:37 UTC ( #904792=note: print w/replies, xml ) Need Help??

in reply to staements in strings

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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://904792]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2022-05-20 12:52 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (73 votes). Check out past polls.