Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 223461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:13:24+00:00 2026-05-11T19:13:24+00:00

Background Below is a typical piece of Perl code ( sample.pl for the sake

  • 0

Background

Below is a typical piece of Perl code (sample.pl 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.

Code listing for sample.pl :

#!/usr/bin/perl
use strict;
use CGI;
use DBI:
use Template;

#Grab submitted form data
my $cgi = CGI->new();
my $idFromSomewhere= $cgi->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->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->prepare($sql)
        or die "Couldn't prepare query '$sql': $DBI::errstr\n";

#Execute SQL Query
$statement->execute($idFromSomewhere)
    or die "Couldn't execute query '$sql': $DBI::errstr\n";

#Get query results as hash
my $results = $statement->fetchall_hashref('id');

$db_handle->disconnect();
my $tt = Template->new();

#HTML output template
my $input = 'template.html';
my $vars = {
    tests => $results,
};

#Process template and output as HTML
$tt->process($input, $vars)
    or die $tt->error();

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:

use FCGI;
while (FCGI::accept >= 0)
{    
   #Run existing code.
}

What’s not so clear is where and what to put in the while loop.

Sub Questions

A.
Should the code in sample.pl be simply wrapped around the existing code like so:

while (FCGI::accept >= 0)
{    
    #Grab submitted form data
    my $cgi = CGI->new();
    ...
    ...
    #Process template and output as HTML
    $tt->process($input, $vars)
    or die $tt->error();
}

B.
Or is there more to it? For instance, should the code that handles the cgi, database and template be refactored into their own subs?

C.
Should DBI->connect() and $db_handle->disconnect() be called inside or outside the FCGI while loop and what are the performance implications?

D.
Should $tt->process() be called inside or outside the FCGI while loop?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:13:25+00:00Added an answer on May 11, 2026 at 7:13 pm

    If you’re familiar with CGI.pm, there is no point in using FCGI.pm, use CGI::Fast.

    Your example converted to use CGI::Fast would be:

    #!/usr/bin/perl
    use strict;
    use CGI::Fast;
    use DBI;
    use Template;
    
    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->connect( $url, $user, $password ) or die $DBI::errstr;
    
    while ( my $cgi = CGI::Fast->new() ) {
    
        #Grab submitted form data
        my $idFromSomewhere = $cgi->param( 'id' );
    
        #SQL query to execute
        my $sql = "SELECT * FROM tests WHERE id=?";
    
        #Prepare SQL query
        my $statement = $db_handle->prepare( $sql )
            or die "Couldn't prepare query '$sql': $DBI::errstr\n";
    
        #Execute SQL Query
        $statement->execute( $idFromSomewhere )
            or die "Couldn't execute query '$sql': $DBI::errstr\n";
    
        #Get query results as hash
        my $results = $statement->fetchall_hashref( 'id' );
    
        my $tt = Template->new();
    
        #HTML output template
        my $input = 'template.html';
        my $vars = { tests => $results, };
    
        #Process template and output as HTML
        $tt->process( $input, $vars )
            or die $tt->error();
    }
    

    As for your sub questions:

    • A:
      Do not use FCGI unless you are 100% sure that you know what you’re doing. You definitely want CGI::Fast 🙂
    • B:
      I would refactor it for readability
    • C: if you use DBI->connect before accepting connection you get permanent database connection which is great from performance standpoint
    • D: definitely inside.

    Just as a side information – if you want to develop websites in Perl, at least take a look at Catalyst (http://www.catalystframework.org/)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

I'm re-designing an app I inherited that sends digital photos from a laptop to
I am doing some performance tests using .Net 3.5 against SQL Server. I am
I'm creating a website for my church and I'm having problems making it display
Recently, I made a post about the developers I'm working with not using try

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.