4

I asked a separate question about Python here, and was told that "classic CGI isn't the best way to use anything at all. With classic CGI server has to spawn a new process for every request"

Now I am working on a Perl-based website using Apache, and all my scripts start with:

use CGI;

Is this a problem? Is there a newer or better way to build a Perl-based website?

Community
  • 1
  • 1
Andy Swift
  • 2,179
  • 3
  • 32
  • 53
  • 1
    It sounds like you were being steered toward [FastCGI](http://www.fastcgi.com). – Politank-Z Nov 07 '14 at 15:50
  • 1
    There are a couple of things at play here: the web server interface and the application framework. Alternative interfaces are mod_perl, FastCGI, and PSGI (this is what the comment about "classic CGI" is referring to). Alternative frameworks are Mason, Dancer/Dancer2, Catalyst, and Mojolicious, among others. Note that you can actually use the `CGI.pm` *module* with the FastCGI *interface*, for example. – ThisSuitIsBlackNot Nov 07 '14 at 16:12

2 Answers2

9

Write your website using Plack or a framework built on top of Plack (such as Catalyst or Dancer/Dancer2).

You then have several options to deploy the website. You can deploy it as a CGI script (easy to do, but inefficient), or using FastCGI or Apache's mod_perl, or forget Apache altogether and use a standalone Perl web server such as Starman. Yet another option is uWSGI which is conceptually similar to FastCGI.

Apache's mod_proxy allows you to take a hybrid approach for deployment. Your website could be running on Apache, which could forward requests for particular URLs through to Starman.

Dondi Michael Stroma
  • 4,668
  • 18
  • 21
tobyink
  • 13,478
  • 1
  • 23
  • 35
  • -1: That is a grossly confident solution, given that you have no idea at all what the OP needs to accomplish. Recommending that they write a site with `Plack` and then go back to CGI is a little strange. – Borodin Nov 07 '14 at 18:28
  • 2
    There's a difference between using CGI.pm and using CGI, the interface. CGI.pm is extremely crufty and to be avoided. CGI, as an interface between a web server and a script is perfectly OK. Inefficient, sure, but it has its advantages too: each request runs as a separate process, which means that is a memory leak or segfault happens, it will at least only effect that request, and the next request will happen afresh. – tobyink Nov 08 '14 at 22:26
4

The CGI perl module is a way of interfacing with a webserver. It does the job it was always intended to, but it's been superseded by an assortment of frameworks for web development.

It has been removed from the perl core:

The rational for this decision is that CGI.pm is no longer considered good practice for developing web applications, including quick prototyping and small web scripts. There are far better, cleaner, quicker, easier, safer, more scalable, more extensible, more modern alternatives available at this point in time. These will be documented withCGI::Alternatives

However, there's a bit of a learning curve for the assorted frameworks, and personally I find them geared up to 'proper wesbites' - not the 'script with a web interface' type things I tend to knock together. So I'd suggest reading though those alternatives, and if you're trying to do something clever - use one of them.

And if you're not, you probably didn't really need CGI in the first place.

cjm
  • 61,471
  • 9
  • 126
  • 175
Sobrique
  • 52,974
  • 7
  • 60
  • 101
  • +1: I agree with all of what you say except for the final *"you probably didn't really need CGI in the first place"*. There is a lot of CGI that is centered around writing valid HTML, but there are useful parts too that help you write an HTTP response without previous knowledge. – Borodin Nov 07 '14 at 18:35
  • 1
    Well, I've always found `print "Content-Type: text/html\n\n";` works fairly well ;p. I've yet to find one of the alternatives that's actually straightforward to 'get on with' like CGI. – Sobrique Nov 07 '14 at 18:51
  • 2
    Those line terminators should properly be `"\r\n"` written to a `:raw` file handle. If you've been working exclusively on Windows then your version will work, but it's *always* simpler and more reliable to `use CGI` and `print header`. And you should try responding to a `POST` request the same way. – Borodin Nov 07 '14 at 18:56
  • This question very probably isn't about CGI.pm, but about using CGI scripts. A lot of people asking beginner's questions about Perl today are driven to it by documentation dating from 1995. If you don't believe me, try hanging around in a #perl of your choice for a while. – reinierpost Nov 07 '14 at 21:53