nysus has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

The following code:

use CGI; use CGI::Carp qw(fatalsToBrowser); $query = new CGI; print $query->header; prnt "hello";
...produces an Internal Server Error for the obvious reason that "prnt" should be "print". But why can't I get this compile time error to appear in the browser window? How come "fatalsToBrowser" doesn't work in this case?

I've read the Carp documentation but that didn't help me much.

Thanks!

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar";
$nysus = $PM . $MCF;

Replies are listed 'Best First'.
Re: Gettin ALL errors with carp module
by Masem (Monsignor) on May 20, 2001 at 16:16 UTC
    The problem here is that the print typo is a compile-time error, which will occur before any part of your perl script is run; therefore Carp has no possibility of catching these types of errors. Instead, Carp can only catch run-time errors generated by die, warn, and the various flavors of those; these only occur after the program has been successfully compiled.

    The way to work with Carp and perl scripts then is to first do edits of the script, then run the script from the command line. Any errors, including those caught by -w and use strict; should appear then, which you can then fix. You can then use the browser to find other error detections that Carp will pick up.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Well, part of what is confusing me was that in the Carp Documentation, I found this bit of code:

      BEGIN { use CGI::Carp qw(carpout); open(LOG, ">>/usr/local/cgi-logs/mycgi-log") or die("Unable to open mycgi-log: $!\n"); carpout(LOG); }
      Which, according to the documentation, catches compile time errors and writes them to a filehandle. So I figured the same could be done and use a filehandle to write to the browser. Am I wrong?

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar";
      $nysus = $PM . $MCF;

        You need to include that specific block of code (including the BEGIN statement), for compile time errors to be caught. Just using use CGI::Carp will not do the compile time catching.
        Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      So I tried the following:
      use CGI; BEGIN { use CGI::Carp qw(carpout fatalsToBrowser); open STDOUT; carpout STDOUT; } $query = new CGI; print $query->header; prnt "hello";
      I obviously have no clue because it doesn't work. Is it possible to feed compile time errors to the browser? I'm assuming no.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar";
      $nysus = $PM . $MCF;

        Sorry to beat a dead horse. But the following code works...sorta:
        use CGI; BEGIN { use CGI::Carp qw(carpout fatalsToBrowser); open(LOG, ">test") or die("Unable to open test: $!\n"); carpout(LOG); } $query = new CGI; print $query->header; prnt "hello";
        OK, so now I'm getting an error in the browser but it's very generic:
        Execution of /home/xxxx/xxxx/web/cgi-bin/xx.cgi aborted due to compila +tion errors.
        That's it...no line number, no details. Compare this to the "test" log file which is more what I'm looking for:
        [Sun May 20 12:04:58 2001] carp.cgi: Unquoted string "prnt" may clash +with future reserved word at /home/xxxxx/xxxx/web/cgi-bin/xx.cgi line + 15. [Sun May 20 12:04:58 2001] xx.cgi: String found where operator expecte +d at /home/xxxxx/xxxx/web/cgi-bin/xx.cgi line 15, near "prnt "hello"" [Sun May 20 12:04:58 2001] carp.cgi: (Do you need to predeclare pr +nt?) syntax error at /home/xxxxx/xxxx/web/cgi-bin/xx.cgi line 15, near "prn +t "hello"" Unmatched right bracket at /home/xxxx/xxxxx/web/cgi-bin/xx.cgi line 16 +, at end of line [Sun May 20 12:04:58 2001] xx.cgi: Execution of /home/xxxxx/xxxx/web/c +gi-bin/xx.cgi aborted due to compilation errors.
        How do I get my browser message to look like my log?

        $PM = "Perl Monk's";
        $MCF = "Most Clueless Friar";
        $nysus = $PM . $MCF;