http://www.perlmonks.org?node_id=1015433


in reply to Help me fill a gap in my server-side knowledge?

Plack is "middleware" that sits between the web development framework (Catalyst/Dancer/etc) and the server. It smooths out the differences between, say, CGI, mod_perl, FastCGI, etc so that they can be deployed in all those different ways with little effort.

Plack-based websites can be deployed as CGI scripts. I have done this a number of times, and if you're careful you can get reasonable performance. However, if you use one of the larger frameworks like Catalyst, you'll find they run very slowly through CGI. This is because all the Perl modules used by the frameworks (in the case of Catalyst, this includes the Moose!) have to be loaded every request.

Other deployment options allow some or all code to "persist" between requests. For example, mod_perl keeps compiled (i.e. Perl optree) copies of all the modules your script uses in Apache's memory, saving them from being re-loaded and re-compiled each request.

Many deployment options do away with Apache or other "traditional" web servers altogether, instead using a web server written in pure Perl (or perhaps with a bit of XS to speed up the slow parts). Plack comes with the plackup tool which can be used to run many of these servers. If you've got the right permissions on the server you can write a shell script to start/stop the server and place it in /etc/init.d/ like all the other start/stop scripts on your machine that start/stop servers. Thus they'll restart when you restart the system.

Some people will power some parts of their website with Apache and other parts with a Perl web server running on a different TCP port; they can then use Apache's mod_proxy to forward certain requests to the Perl server.

If deploying via CGI or mod_perl, you certainly don't need root, and might not even need shell access.

If deploying via other means, you probably want shell access and maybe root access.

PS: here's an example of writing a tiny web app on bare Plack (no framework):

#!/usr/bin/env plackup # Assume this file is called "greeting.psgi" my $app = sub { return [ 200, [ Content_Type => 'text/plain' ], [ 'Hello world' ], ]; };

Just chmod +x greeting.psgi to mark it as executable, then run it at the command line. It will spawn a Perl web server on port 5000 of your computer. Type "http://localhost:5000/" into your your browser and lo and behold, a friendly greeting to the planet!

If you want to deploy it on CGI, create an accompanying file called greeting.cgi containing just:

#!/usr/bin/env perl use Plack::Loader; my $app = Plack::Util::load_psgi("/full/path/to/greeting.psgi"); Plack::Loader->auto->run($app);

... then chmod +x greeting.cgi, then visit the CGI script in your browser how you would normally.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name