0
votes

I was wondering about using a single Perl file to generate the HTML form and act upon the parameters versus the standard method of a single HTML file calling a separate Perl script.

What methods could be used to present HTML input forms and pass the parameters to the same calling script, then act upon them? Is that bad practice?

2

2 Answers

2
votes

What methods could be used to present HTML input forms and pass the parameters to the same calling script

Omit the action attribute, or set the URL to the current URL.

then act upon them?

Test to see if the fields are included in the data and/or if the request method is POST (assuming that POST is appropriate for the form) and use that to determine if you should display a form or the next step.

Is that bad practice?

No, it is a common pattern. It allows you to reuse the code that generates the form to return a new form, pre-populated with the submitted data, in the event of an input error.

0
votes

It's a very common practice to have CGI program that looks something like this:

if (parameters) {
  Handle the paremeters to produce a page of results
} else {
  Present an input form
}

But I worry that your title talks about producing the HTML using CGI.pm's HTML function. Pretty much everyone worked out that those function were a bad idea about fifteen years ago. You should be using a templating engine to produce your HTML outout.

And, really, you shouldn't be writing CGI programs these days. If you write software using something like PSGI, you'll find it far more flexible.