Display or view the perl cgi errors in a web browser

Q. I’m currently learning Perl cgi programming and coded a small web site in a Perl. Usually errors are logged in a log file. How can I view the perl cgi errors in a we browser like firefox?

ADVERTISEMENTS

A. CGI scripts have a nasty habit of leaving warning messages in the error logs that are neither time stamped nor fully identified. Tracking down the script that caused the error is a pain.

You need to install and use CGI::Carp module.

With this module the standard warn(), die (), croak(), confess() and carp() calls will automatically be replaced with functions that write out nicely time-stamped messages to the HTTP server error log.

You can also log message to a browser. Now add following two lines before sending any headers to a browser:

use CGI; 
use CGI::Carp qw(warningsToBrowser fatalsToBrowser); 

With above lines it is possible to make non-fatal errors appear as HTML comments embedded in the output of your program. To enable this feature, export the new “warningsToBrowser” subroutine. Since sending warnings to the browser before the HTTP headers have been sent would cause an error, any warnings are stored in an internal buffer until you call the warningsToBrowser() subroutine with a true argument.

See official CGI::Carp man page for more information.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
6 comments… add one
  • Luke Jun 16, 2007 @ 20:40

    Hi Vivek,
    Just pointing out a typo at the end of your post – isn’t it called CGI::Carp, and not CGI::Crap? You seem to have mispelled your link to the man page(although the link still works).

  • 🐧 nixCraft Jun 17, 2007 @ 9:13

    Luke,

    Thanks for the heads up! FAQ has been updated.

  • Mimi Nov 24, 2011 @ 3:39

    Thanks! Useful!

  • Craig Jun 13, 2012 @ 9:00

    This article is no longer valid with the new versions of perl.

    I have moved to IIS7 and ActivePerl 5.14 and fataltobrowsers no longer works.

    all you get is a 500 INTERNAL SERVER ERROR!

  • Sam Jun 27, 2012 @ 3:18

    Thanks! Very useful.

  • JoePerl Apr 22, 2017 @ 21:53

    Oh my god, that’s the greatest productivity booster of any library in perl.

    Thank the perl gods I found this!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.