in reply to Gettin ALL errors with carp module

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

Replies are listed 'Best First'.
Re: Re: Gettin ALL errors with carp module
by nysus (Parson) on May 20, 2001 at 16:27 UTC
    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
Re: Re: Gettin ALL errors with carp module
by nysus (Parson) on May 20, 2001 at 18:59 UTC
    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;

        The reason you see only the "Execution of ... aborted due to compilation errors" message is that this is the only fatal error being produced. The "String found..." compilation 'error' is actually a severe warning, which is not caught by fatalsToBrowser.

        This is a quirk of how many of Perl's compilation errors are handled. If "... found where ... expected" were a fatal error, then compilation would abort immediately. Making it a severe warning allows compilation to continue, possibly reporting other errors and warnings, before finally failing with the generic compilation errors message. Here's an example:

        if ($x) { prnt "Hello"; } elseif { print "Goodbye" $x = 1; }
        If all compilation errors were fatal, you'd just get: String found where operator expected at tmp.pl line 4, near "prnt "Hello"" But perl actually provides more:
        String found where operator expected at tmp.pl line 4, near "prnt "Hel +lo"" (Do you need to predeclare prnt?) syntax error at tmp.pl line 4, near "prnt "Hello"" elseif should be elsif at tmp.pl line 5. Scalar found where operator expected at tmp.pl line 7, at end of line (Missing semicolon on previous line?) syntax error at tmp.pl line 7, near "$x " tmp.pl had compilation errors.
        This does have its drawbacks, as you've found. However, you should make sure your scripts compile before running them from the browser, which would avoid the problem you are having.
        I'm also suffering with CGI::Carp, so any info is welcome about it.