Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I refactor Perl code that uses Template Toolkit with DBI to take advantage of FastCGI?
    text
    copied!<h2>Background</h2> <p>Below is a typical piece of Perl code (<em>sample.pl</em> for the sake of discussion) that grabs submitted form data using CGI, passes the form data to DBI which will then retrieve the required rows from MySQL and then hands the results over to Template Toolkit to render into a HTML document for display.</p> <p>Code listing for <em>sample.pl</em> :</p> <pre><code>#!/usr/bin/perl use strict; use CGI; use DBI: use Template; #Grab submitted form data my $cgi = CGI-&gt;new(); my $idFromSomewhere= $cgi-&gt;param('id'); my $driver = "mysql"; my $server = "localhost:3306"; my $database = "test"; my $url = "DBI:$driver:$database:$server"; my $user = "apache"; my $password = ""; #Connect to database my $db_handle = DBI-&gt;connect( $url, $user, $password ) or die $DBI::errstr; #SQL query to execute my $sql = "SELECT * FROM tests WHERE id=?"; #Prepare SQL query my $statement = $db_handle-&gt;prepare($sql) or die "Couldn't prepare query '$sql': $DBI::errstr\n"; #Execute SQL Query $statement-&gt;execute($idFromSomewhere) or die "Couldn't execute query '$sql': $DBI::errstr\n"; #Get query results as hash my $results = $statement-&gt;fetchall_hashref('id'); $db_handle-&gt;disconnect(); my $tt = Template-&gt;new(); #HTML output template my $input = 'template.html'; my $vars = { tests =&gt; $results, }; #Process template and output as HTML $tt-&gt;process($input, $vars) or die $tt-&gt;error(); </code></pre> <p>For better performance and scalability, web hosts providing shared servers, such as Dreamhost, strongly recommend that all production Perl script support FastCGI. The FastCGI documentation is pretty clear on how to modify existing Perl code to support FastCGI. The simple code below is often given as an example: </p> <pre><code>use FCGI; while (FCGI::accept &gt;= 0) { #Run existing code. } </code></pre> <p><strong><em>What's not so clear is where and what to put in the while loop.</em></strong></p> <h2>Sub Questions</h2> <p><strong>A.</strong> Should the code in sample.pl be simply wrapped around the existing code like so:</p> <pre><code>while (FCGI::accept &gt;= 0) { #Grab submitted form data my $cgi = CGI-&gt;new(); ... ... #Process template and output as HTML $tt-&gt;process($input, $vars) or die $tt-&gt;error(); } </code></pre> <p><strong>B.</strong> Or is there more to it? For instance, should the code that handles the cgi, database and template be refactored into their own subs?</p> <p><strong>C.</strong> Should DBI->connect() and $db_handle->disconnect() be called inside or outside the FCGI while loop and what are the performance implications?</p> <p><strong>D.</strong> Should $tt->process() be called inside or outside the FCGI while loop?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload