What is the best approach to transfer CGI to a structure?

I have a great web application running in perl CGI. It works fine, it is well written, but as it was done in the past, all html are defined as hard-coded in CGI calls, so you can imagine that it is difficult to do, improve, etc. So now I would like to start adding some templates and integrating them with the framework (catalyst or CGI :: application). My question is: does anyone have such an experience? Are there things I should pay attention to? I know that from both frameworks I can run my own CGI scripts, so this is good, because I can run both (CGI native ad "frameworked" code) together without any trauma. Any tips?

+7
perl model-view-controller catalyst cgi
source share
5 answers

First write the tests (e.g. Test::WWW::Mechanize ). Then, when you change things, you always know that something is breaking, and what it means is breaking.

Then extract the HTML into the templates and usually use subs. After this, a piece of the cake switches to the frame.

In general, step by step so that you always have a working application.

+10
source share

Extract HTML from processing logic in a CGI script. Define all the code that affects HTML output, as they are candidates for template variables. Separate this in an HTML file, with identified parts marked with template variables. In the end, you can reorganize the page so that all processing is done at the beginning of the code and the HTML template is just called at the end of all processing.

+4
source share

In such a situation, rewriting from scratch basically, the old code is useful for A) testing and B) design details. Ideally, you should do a set of tests for all the basic functions that you want to replicate, or at least tests that analyze the final pages of the results, so you can see that the new code returns the same information for the same inputs.

Details of the structure inside the code can be useless, depending on how the system is automatically processed. If you have a good test suite and a simple conversion works well, you're done. If the behavior of the new does not match the old, you probably need to delve into the “why?”. and this is likely to be something strange, it does not make sense at first glance.

One thing that you need to remember first of all is to find out if someone has done this in the framework of the structure you are using. You could save a lot of time and money.

+3
source share

Here's how I did it using Python instead of Perl, but that doesn't matter:

  • Separate HTML and code into separate files. For this, I used the template engine .
  • Functions are created from the code that displays the template with a set of parameters.
  • Organized the features (which I called opinions inspired by Django) in a sensible way. (Admin views, user views, etc.) All submissions follow the same calling convention !
  • Implements a database and requests material so that the views contain only certain code (read: Processing GET, POST requests, etc., but nothing low level!). They relied heavily on existing libraries.

I'm here at the moment. :-) The next obvious step, of course:

  • Write a dispatcher that maps the URLs to your views. This will also lead to nicer URLs and more convenient 404 and error handling, of course.
+3
source share

One of the assumptions that frameworks make is that URLs map to code. For example, in a structure you often see the following:

 http://app.com/docs/list http://app.com/docs/view/123 

Usually, although older CGI scripts do not work, you will most likely have something like:

 http://app.com/docs.cgi?action=view&id=123 

To take advantage of the structure, you may need to change all URLs. Can you do this, and how you keep old working links may well make up most of your decision.

In addition, the frameworks provide support for some kind of ORM (object relational mapper), which abstracts database calls and allows you to deal only with objects. For Catalyst , it’s usually DBIx::Class , you must evaluate what the cost of switching to it will be.

You will probably find that you want to make a complete correspondence with the old code as a reference platform. It can be much less than you expect. However, start with a few toy sites to get an idea of ​​which frame / orm / template you decide to go with.

+3
source share

All Articles