awake416
awake416

Reputation: 301

How can I structure CGI programs that move page to page?

I have a general question about Perl CGI programs. For a few applications, not using MVCs, developers write CGI programs, may be using CGI.pm or some other way they like and for moving from screen to sceen, a new CGI program is written, (with - CVS TAGS, Perl headers etc etc..).

But what I feel is if there is some stuff (subroutine or business logic) which is not required in other programs, I keep on calling same script with each page called based on one extra Role parameter. Benefit I get is one time header declaration, easy to maintain,

%frmData = CGI::Lite->parse_form_data();
$Role    = $frmData{Role};
if ($Role eq 'A'){ getPageFirst()}
elsif ($Role eq 'B'){ getPageB()}  etc..

Is this a correct approach? Can we face some kind of problem. What will be the best approach?

Upvotes: 2

Views: 182

Answers (2)

brian d foy
brian d foy

Reputation: 132858

Based only on what you've said, I'd say you are creating a maintenance nightmare of duplicating the same code into multiple programs and going through the same logic in every program. You probably, at least, want to move that into a shared library.

It sounds like you might have the problem that CGI::Prototype was meant to solve. It handles the selecting the page that you should be on and lets you dispatch to the right handler for it. It's a minimal framework that doesn't provide much more than that.

Without knowing more about what you are doing, it's hard to give any better answer.

Upvotes: 1

Thilo
Thilo

Reputation: 262714

I think combining a bunch of functionality (several CGI) into one file does not really help maintainability.

The alternate approach would be to keep the CGI apart and put the common code into separate files (such as Perl modules), that each of them can include.

You mention you are worried about including too much stuff that is not necessary for all of the CGI. It seems to me that with the combined big CGI, you are actually making this situation worse. With modules, you can include what you need.

Can we face some kind of problem?

The first problem you will likely face with CGI is performance, because the script needs to be reloaded all the time. So you want to look at something like FastCGI, which keeps the script (and libraries) in memory between requests. If you go there, having your code base properly divided into modules will help.

Upvotes: 2

Related Questions