++ed by:

1 non-PAUSE user.

Jonas Liljegren


CGI::Debug - module for CGI programs debugging


 use CGI::Debug;

 use CGI::Debug( report => ['errors', 'empty_body', 'time', 
                            'params', 'cookies', 'environment',
                 on     => 'fatals',
                 to     => { browser => 1,
                             log     => 1,
                             file    => '/tmp/my_error',
                             mail    => ['staff@company.orb',
                 header => 'control',
                 set    => { error_document => 'oups.html' },


CGI::Debug will catch (almost) all compilation and runtime errors and warnings and will display them in the browser.

Just "use CGI::Debug" on the second row in your program. The module will not change the behaviour of your cgi program. As long as your program works, you will not notice the modules presence.

At any time you can remove the "use CGI::Debug" without changing the behaviour of your program. It will only start faster.

The actions of CGI::Debug is determined by, in order: 1. cookie control variables 2. environment control variables 3. the import control parameters 4. the defaults

Default behaviour

Report to browser:

  • bad HTTP-headers

  • empty HTTP-body

  • warnings and errors

  • elapsed time

  • query parameters

  • cookies

  • environment variables (max 60 chars in value)


Only report errors: use CGI::Debug( report => 'errors' );

Do not bother about warnings: use CGI::Debug( on => 'fatals' );

Allways show complete debugging info: use CGI::Debug( report => 'everything', on => 'anything' );

Send debug data as mail to file owner: use CGI::Debug( to => 'mail' );


Cookie control variables makes it possible to control the debugging environment from a program in another browser window. This would be prefereble with complex web pages (framesets, etc). The page is viewd as normal in one window. All debugging data is shown i another window, that also provides controls to alter the debugging environment. (But this external debugging program is not yet implemented.)

Environment control variables makes it more easy to globaly set the debugging environment for a web site. It is also a way for the target program to control the CGI::Debug module actions.

The four methods can be mixed. (Cookies, enviroment, import parameters and defaults.) The module will try to make sense with whatever you give it. The possibilites of control are more limitied in the Cookie / ENV version.

report errors

  Cookie / ENV: CGI-Debug-report=errors

  Import: report => 'errors'
          report => [ 'errors', ... ]

Report the content of STDERR.

This will allways be reported. This control is for saying that none of the other defualt things will be reported. This will only override the default. Other report controls will be accumulated.

report empty_body

  Cookie / ENV: CGI-Debug-report=empty_body

  Import: report => 'empty_body'
          report => [ 'empty_body', ... ]

Report if HTTP-body is empty.

This requires that "header control" is set.

report time

  Cookie / ENV: CGI-Debug-report=time

  Import: report => 'time'
          report => [ 'time', ... ]

Report the elapsed time from beginning to end of execution.

If Time::Hires is found, this will be given with subsecond precision.

report params

  Cookie / ENV: CGI-Debug-report=params

  Import: report => 'params'
          report => [ 'params', ... ]

Report a table of all name/value pairs, as given by the CGI module.

Multiple values will be reported as distinct pairs, in order. Values will be truncated to the "set param_length" number of chars. The total length is shown for each value.

report cookies

  Cookie / ENV: CGI-Debug-report=cookies

  Import: report => 'cookies'
          report => [ 'cookies', ... ]

Report a table of all cookies, as given by the CGI module.

Multiple values will be reported as distinct pairs, in order. Values will be truncated to the "set param_length" number of chars. The total length is shown for each value.

report environment

  Cookie / ENV: CGI-Debug-report=environment

  Import: report => 'environment'
          report => [ 'environment', ... ]

Report a table of all environment varialbes INCLUDING empty_body, time, params, cookies.

report everything

  Cookie / ENV: CGI-Debug-report=everything

  Import: report => 'everything'
          report => [ 'everything', ... ]

Report environment and all what that includes.

(The plan is for this control to include the contorl of HTML compliance.)

report internals

  Cookie / ENV: CGI-Debug-report=internals

  Import: report => 'internals'
          report => [ 'internals', ... ]

Report data for the debugging of the module itself, including everything else. Data::Dumper will be used, if found.

If the module itself dies, you will probably not get any output at al. You can check for errors in the file /tmp/CGI-Debug-error-$$. In order to see what error CGI::Debug is generating, you could changing $DEBUG to 2 or more, in the module file itself. Please email the author about any problems.

on fatals

  Cookie / ENV: CGI-Debug-on=fatals

  Import: on => 'fatals'

Only deliver report on fatal errors.

This will ignore warnings. CGI::Debug checks the exit value. Reports will also be delivered if an empty body is detected, in case "header control" is set.

on warnings

  Cookie / ENV: CGI-Debug-on=warnings

  Import: on => 'warnings'

Only deliver report on fatals or if there was any output to STDERR.

on anything

  Cookie / ENV: CGI-Debug-on=anything

  Import: on => 'anything'

Always deliver reports, even if there was no errors.

to browser

  Cookie / ENV: CGI-Debug-to=browser

  Import: to => 'browser'
          to => [ 'browser', ... ]
          to => { 'browser' => 1, ... }

Send report to browser.

The report will come after any program output. The module will assume the page is in text/html, unless "header control" is set, in case this will be checked. (In none HTML mode, the header and delimiter will be ASCII.)

There is many cases in which faulty or bad HTML will hide the report. This could be controled with "report html_compliance" (which is not yet implemented).

to log

  Cookie / ENV: CGI-Debug-to=log

  Import: to => 'log'
          to => [ 'log', ... ]
          to => { 'log' => 1, ... }

Send report to the standard error log.

This will easily result in a huge log.

to file

  Cookie / ENV: CGI-Debug-to=file

  Import: to => 'file'
          to => [ 'file', ... ]
          to => { 'file' => 'filename', ... }
          to => { 'file' => [ 'filename1', 'filename2', ... ] ... }

Save report to filename.

Default filename is "/tmp/CGI-Debug-error.txt". The file will be overwritten by the next report. This solution is to be used for debugging with an external program. (To be used with cookies.)

This will not work well with framesets that generates multipple reports at a time. The action of this control may change in future versions.

to mail

  Cookie / ENV: CGI-Debug-to=mail

  Import: to => 'mail'
          to => [ 'mail', ... ]
          to => { 'mail' => 'mailaddress', ... }
          to => { 'mail' => [ 'mailaddress1', 'mailaddress2', ... ] ... }

Send report with email.

The default mailaddress is the owner of the cgi program. This function requires the Mail::Send module. If there is any problem with the default behaviour of Mail::Send, set the enviroment variables, as described in the POD for Mail::Mailer, either for the HTTP server, or before "use CGI::Debug" in a BEGIN block.

The idea is to specify an email address that will be used if anybody besides yourself is getting an error. You will not get your own errors as email if you overide that action with a control cookie.

header control

  Cookie / ENV: CGI-Debug-header=control

  Import: header => 'control'

Controls that the HTTP-header is correct.

This control will follow the HTTP RFC to the point. It reports if the header is ok, if the content-type is text/html, and the length of the HTTP-body. That information will be used by other parts of CGI::Debug. This is done by redirecting STDOUT to a temporary file. This is the only control that must be set in the beginning of the program. All other controls can be changed during before the end of the program.

header ignore

  Cookie / ENV: CGI-Debug-header=ignore

  Import: header => 'ignore'

Assume that the HTTP-header is correct and specifies text/html.

This will tell CGI::Debug to ignore the STDOUT. A server generated error response will result if the program compile ok but does not produce a valid HTTP-header.

header minimal

  Cookie / ENV: CGI-Debug-header=minimal

  Import: header => 'minimal'

Generates a simple text/html HTTP-header for you.

This is the only action that CHANGES THE BEHAVIOUR of your program. You will have to insert your own header if you remove the CGI::Debug row. But this action will guarantee that you have a valid header, without the need to save STDOUT to a temporary file.

set param_length

  Cookie / ENV: CGI-Debug-set-param_length=value

  Import: set => { param_length => 'value', ... }

Set the max length of the parameter values.

The default length is 60 chars. This is used for query parameters, cookies and environment. The purpose is to give you a table that looks good.

set error_document

  Cookie / ENV: CGI-Debug-set-error_document=value

  Import: set => { error_document => 'value', ... }

Set what page to redirect to if there was an error report, not sent to browser.

This will show up in the browser if the error is going somewhere else. If no page is specified, a short generic CGI error response will show up. But if the CGI program succeeded in printing a valid http header and something in the body, that will be showed instead, even if the program later crashed.


CGI::Debug will not function under mod_perl. The only solution to get similar functionality is to develop a replacement for Apache::Registry, with integrated debugging features. The configuration interface can not include the "use CGI::Debug ( report => ... )" style, in a mod_perl version.

If you run CGI::Debug under mod_perl, it will do nothing, except sending a warning to STDERR.


These are things that could be done to make CGI::Debug even better. I have no plan to add new features myself. Feel free to contribute.

  • Clean up and generalize configuration

  • Test on non-*nix platforms

  • Implement HTML_compliance controls (using HTML::validate)

  • Implement function for debugging in a separate window


Copyright (c) 1999-2000 Jonas Liljegren. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Jonas Liljegren <jonas@paranormal.se>


CGI, Mail::Send, Time::Hires, Data::Dumper, perl