The tainting of information, and the use of CGI::Carp, are both low-level issues and that can still be a cause for concern. However, the low-level aspects of CGI applications, such as dealing with query arguments and outputting header material, can be simplified by using one of a number of web application frameworks, such as Catalyst or Dancer. Plack works with frameworks or can be used on its own, as demonstrated below.
Plack is Perl super-glue for web frameworks and web servers. Plack sits between your tex (whether you use a web framework or not) and the web server (for example, Apache, Starman, FCGI). This means that you (and your framework) do not need to worry about specifics of a web server and vice-versa. Setting up Plack Let's get you set-up. We are going to use cpanm (from App::cpanminus) to download and install modules into yourlocal::lib (so you do not need root access). This is shown in initial setup;
code:
# archive of any existing cpan configuration mv ~/.cpan ~/.cpan_original
# Then one of the following:
# if you can run wget wget -O - http://cpanmin.us/ | perl - local::lib App::cpanminus && echo 'eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)' >> ~/.bashrc && . ~/.bashrc
# OR if you can run curl curl -L http://cpanmin.us/ | perl - local::lib App::cpanminus && echo 'eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)' >> ~/.bashrc && . ~/.bashrc
# otherwise, download the contents of http://cpanmin.us to a file called cpanmin.us, make it executable and then run: ./cpanmin.us local::lib App::cpanminus && echo 'eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)' >> ~/.bashrc && . ~/.bashrc
This installs the core Plack modules that are required to build web applications quickly and easily
code:
cpanm Task::Plack # Please also run this as we will use it later cpanm Plack::Middleware::TemplateToolkit
The perl5 folder in your home directory will now have all the modules you need. The next step is to create a .psgi configuration file which will allow us to return a web page (see Listing 5 below).
code:
# Tell Perl where our lib is (ALWAYS use this) use lib "$ENV{HOME}/perl5/lib/perl5";
# ensure we declare everything correctly (ALWAYS use this) use strict;
# Give us diagnostic warnings where possible (ALWAYS use this) use warnings;
# Allow us to build our application use Plack::Builder;
# A basic app my $default_app = sub { my $env = shift; return [ 200, # HTTP Status tex [ 'Content-Type' => 'text/html' ], # HTTP Headers, ["All is good"] # Content ]; };
# Return the builder return builder { $default_app; }
Save this to a file, called 1.psgi, then use the plackup command to start your web server from the command line as follows:plackup 1.psgi. You will see: HTTP::Server::PSGI: Accepting connections at http://SERVER_IP:5000/.
Using your web browser, go to http://SERVER_IP:5000/. If you are developing on your desktop computer, thenhttp://localhost:5000/ will work. You should now see a page with "All is good". In fact, if you go to any page this is what you will see http://localhost:5000/any_page.html because we are always returning this, irrespective of the request.
You will notice that on the command line you can see the access logs for the web server. This is because Plack defaults to development mode and turns on a few extra middleware layers for you, specifically AccessLog, StackTrace and Lint.
To see StackTrace in operation, comment out line 27 of Listing 4 by adding a hash (#) in front of it: # ["All is good"] # Content.
Restart your plackup command (type Ctrl+C to stop the process, then run plackup 1.psgi to start it). Now, in your web browser go to http://localhost:5000/ again and you will see a StackTrace of the error. Note the main error message at the top of the page "response needs to be 3 element array, or 2 element in streaming". You can then follow each step of the trace, click on the Show function arguments and Show lexical variables links under any section of the trace to help debug the issue.