Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How do I get errors printing out to the browser?

by chriso (Sexton)
on Aug 05, 2003 at 15:20 UTC ( #281020=perlquestion: print w/replies, xml ) Need Help??

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

My students are putting the following in the first portion of my scripts:

#!/usr/bin/perl -w
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);

However, they are not getting any errors to the browser. For example. The browser will tell me internal error. However, when I run perl -c at the console, it tells me, "syntax error at quizfive.cgi line 23, near "; ||"

What do we need to do to get this error out to the browser?

Thanks.

  • Comment on How do I get errors printing out to the browser?

Replies are listed 'Best First'.
Re: How do I get errors printing out to the browser?
by talexb (Canon) on Aug 05, 2003 at 16:14 UTC

    Or, to rephrase the previous answer .. fatalsToBrowser works fine for run-time errors. However, if there are compile errors, the script (or CGI) never runs. So you need to check for syntax errors by running

    perl -c quizfive.cgi
    so you can remove the extra ';' that you have in front of the double or bar. Just a guess. :)

    --t. alex
    Life is short: get busy!
Re: How do I get errors printing out to the browser?
by jsprat (Curate) on Aug 05, 2003 at 16:59 UTC
Re: How do I get errors printing out to the browser?
by blue_cowdawg (Monsignor) on Aug 05, 2003 at 15:51 UTC

    First off, you need to understand that if a Perl script fails perl -c then the server is only going to issue an internal error to your browser. Sorry! That's it! End of story!


    Peter @ Berghold . Net

    Sieze the cow! Bite the day!

    Test the code? We don't need to test no stinkin' code! All code posted here is as is where is unless otherwise stated.

    Brewer of Belgian style Ales

      if a Perl script fails perl -c then the server is only going to issue an internal error to your browser.
      Why do you say that? fatalsToBrowser will report errors to the browser whether the script compiles or not. It even reports line numbers and paths, which is a good reason to remove fatalsToBrowser in production code.

        Just to verify what jsprat is saying:

        #!/usr/bin/perl -w use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); $i = 6; || $j = 7;
        produces:
        Content-type: text/html <H1>Software error:</H1> <PRE>syntax error at /tmp/t63 line 6, near &quot;; ||&quot; Execution of /tmp/t63 aborted due to compilation errors. </PRE> <P> For help, please send mail to this site's webmaster, giving this error + message and the time and date of the error. [Tue Aug 5 12:58:07 2003] t63: syntax error at /tmp/t63 line 6, near +"; ||" [Tue Aug 5 12:58:07 2003] t63: Execution of /tmp/t63 aborted due to c +ompilation errors.

        chriso: Do you see the same problems with this simple script, or does it display the error in the browser as you expect?

        OK.... maybe it is a case of YMMV and depends on the server and its configuration. I was able to verify what you are saying as true on my test apache server at home.

        However, in my professional experience what I said in my reply is also true with a lot of servers and server configurations.


        Peter @ Berghold . Net

        Sieze the cow! Bite the day!

        Test the code? We don't need to test no stinkin' code! All code posted here is as is where is unless otherwise stated.

        Brewer of Belgian style Ales

Re: How do I get errors printing out to the browser?
by sgifford (Prior) on Aug 05, 2003 at 16:53 UTC
    What if you run your script without -c from the command line? Does it appear to output a proper HTML page with headers (Content-type: text/html)?
Re: How do I get errors printing out to the browser? (bug)
by tye (Sage) on Aug 05, 2003 at 21:12 UTC
Re: How do I get errors printing out to the browser?
by Hagbone (Monk) on Aug 06, 2003 at 01:36 UTC
    Have you tried:

    #Put the following code at the top of the script,
    #just below #!/usr/bin/perl.
    #Run the program from your browser,
    #and read what is printed there.

    BEGIN { local($|) = 1; # Temporarily turn off bufferi +ng print "Content-type: text/plain\n\n"; my $date = localtime; print "Script $0\nrunning on $date (Perl version $])\n\n"; unless (open STDERR, ">&STDOUT") { print "Can't redirect STDERR: $!"; exit; } print "\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://281020]
Approved by tcf22
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2020-07-11 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?