Open In App
Related Articles

Debugging Perl CGI Scripts

Improve Article
Save Article
Like Article

Perl is a cross-platform, open-source computer programming language that is extensively used in both the commercial and private sectors. Perl is popular among Web developers due to its adaptable, ever-evolving text-processing and problem-solving capabilities. Although Perl was initially developed for text editing, its flexibility makes it a versatile tool for a variety of tasks.

We’ve looked at some of the things that might create frequent mistakes, but not everything is a common issue. If you’re encountering troubles and none of the options are working for CGI, you’ll need to conduct some research. In this part, we’ll look at various tools that can assist you in locating the cause of the problem. Here’s a quick rundown of the actions you can take:

  • Using the -c switch, you may check the syntax of your scripts.
  • Examine the error logs on the webserver.
  • Use the command line to run your script. Dumping variables to the browser allows you to test their values.
  • Make use of an interactive debugger.

Verifying Syntax

If your code does not parse or compile, it will never execute correctly. So, before you test your scripts in the browser, make a practice of testing them using the -c flag from the command line, and while you’re doing it, have it checked for warnings using the -w flag. Remember that if you use taint mode (which you probably do with all of your scripts), you must also pass the -T parameter to avoid the following error: myScript.cgi -wc perl The “-T” option is no longer available. As a result, use the 

perl -wcT calendar.cgi

Examine the Error Logs

Errors are often reported to STDERR, and on certain web servers, everything produced to STDERR while a CGI script is executing shows up in the error logs. When you have an issue, you may typically find a lot of important information by reviewing these logs. This file may be found at:

/usr/local/apache/logs/error log or /usr/var/logs/httpd/error log with Apache. 

Errors are appended to the bottom of the log; you should keep an eye on it while you test your CGI script. If you execute the tail command with the -f option

$ tail -f /usr/local/apache/logs/error_log

Scripts Executed from the Command Line

After your scripts have passed a syntax check, you may try running them from the command line. Because CGI programs rely heavily on environment variables, you can manually configure these variables before running your script

$ export HTTP_COOKIE=”user_id=abc123″ 

$ export QUERY_STRING=”month=jan&year=2001″ 


$ ./calendar.cgi

Debugger for Perl

You’ll end up in an interactive session if you run Perl with the -d switch. Regrettably, this implies that the debugger can only be used via the command line. Although this is not the typical environment for CGI scripts, as we showed before, it is not straightforward to replicate the CGI environment. Saving a CGI object to a query file, initializing any other environment variables you might require, such as cookies, and then running your CGI script like this is the best method to do it.

$perl -dT calendar.cgi 

Loading DB routines from version 1 

Emacs support available

Enter h or `h h’ for help. 

main::(Dev:Pseudo:7): my $q = new CGI; DB<1>

At first, the debugger may appear scary, but it is really powerful. 

The table provides a quick overview of all the fundamental commands you’ll need to troubleshoot a script to get you started. Although many more tools are available, you can debug all of your CGI scripts with only these instructions. To understand how to navigate the debugger, practice stepping through programs that you already know work properly. Because the debugger does not modify your files, you cannot break a working script by inputting an incorrect command.

Command Description

Step; Perl steps into any subroutines and executes the line listed above the prompt. It’s worth noting that evaluating a line with several instructions may take a few steps.


Following that, Perl runs the line listed above the prompt, skipping any subroutines (they still run; Perl waits for them to finish before continuing)


Continue until the program ends or the next breakpoint is reached, whichever comes first.

c 123 Continue until you reach line 123, which must include a command (it cannot be a comment, blank line, the second half of a command, etc.)

Set a breakpoint at the current line; breakpoints stop the execution of code that has been halted by c.

b 123

Place a breakpoint on line 123, which must include a command (it cannot be a comment, blank line, the second half of command, etc.).

b mySub

Set a breakpoint on my sub’s first executable line.


Removes a breakpoint from the current line and accepts the same parameters as b.


Breakpoint delete


There are two requirements for using ptkdb. You’ll need an X Window server first; the X Window System is bundled with most Unix and comparable systems, and commercial versions are also available for other operating systems. Second, the webserver must contain the module, which needs Tk and is accessible on CPAN. Tk is a graphics library that comes with the Tcl programming language. 

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 14 Jul, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials