use CGI::Carp qw(fatalsToBrowser) not sending file handle errors tobrowser

Discussion in 'Perl Misc' started by Bennett Haselton, Mar 1, 2008.

  1. I wrote the following script to test whether, if I try writing to an
    invalid filehandle, the error message would get sent to the browser:

    >>>

    #!/usr/bin/perl

    use strict;
    use IO::File;
    use CGI::Carp qw(fatalsToBrowser);

    print "Content-type: text/html\n\n";

    # This will not open a valid filehandle since the script runs as
    nobody
    my $fh = IO::File->new("> doesnotexist.txt");
    print $fh "foo";

    die "Die here, if you get this far";
    >>>


    The line 'print $fh "foo";' does generate an error, however that error
    gets written to /var/log/httpd/error_log where it says "Can't use an
    undefined value as a symbol reference at /var/www/html/carptest/open-
    wrong-file-with-carp-fatalstobrowser.cgi line 11". I want that sent
    to the *browser*, not written to the log file. The line 'die "Die
    here, if you get this far";' never gets executed.

    The CGI::Carp page says that "Nonfatal errors will still be directed
    to the log file only". But isn't the file handle error a *fatal*
    error, since it caused the script to exit before getting to the 'die
    "Die here, if you get this far";' line? And if it's a fatal error,
    why didn't it get sent to the browser?
     
    Bennett Haselton, Mar 1, 2008
    #1
    1. Advertising

  2. Bennett Haselton

    Guest

    Re: use CGI::Carp qw(fatalsToBrowser) not sending file handle errors to browser

    Bennett Haselton <> wrote:
    > I wrote the following script to test whether, if I try writing to an
    > invalid filehandle, the error message would get sent to the browser:
    >
    > >>>

    > #!/usr/bin/perl
    >
    > use strict;
    > use IO::File;
    > use CGI::Carp qw(fatalsToBrowser);
    >
    > print "Content-type: text/html\n\n";
    >
    > # This will not open a valid filehandle since the script runs as
    > nobody
    > my $fh = IO::File->new("> doesnotexist.txt");
    > print $fh "foo";
    >
    > die "Die here, if you get this far";
    > >>>

    >
    > The line 'print $fh "foo";' does generate an error, however that error
    > gets written to /var/log/httpd/error_log where it says "Can't use an
    > undefined value as a symbol reference at /var/www/html/carptest/open-
    > wrong-file-with-carp-fatalstobrowser.cgi line 11". I want that sent
    > to the *browser*, not written to the log file. The line 'die "Die
    > here, if you get this far";' never gets executed.


    I can't replicate your problem.

    perl -le 'use CGI::Carp; print $CGI::Carp::VERSION'
    1.29

    perl -le 'use CGI::Carp qw(fatalsToBrowser); use IO::File;
    my $fh=IO::File->new("/fooasdf"); print $fh "foo"'


    Content-type: text/html


    <h1>Software error:</h1>
    <pre>Can't use an undefined value as a symbol reference at -e line 1.
    ....

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
     
    , Mar 2, 2008
    #2
    1. Advertising

  3. Re: use CGI::Carp qw(fatalsToBrowser) not sending file handle errorsto browser

    Bennett Haselton wrote:
    > I wrote the following script to test whether, if I try writing to an
    > invalid filehandle, the error message would get sent to the browser:
    >
    > #!/usr/bin/perl
    >
    > use strict;
    > use IO::File;
    > use CGI::Carp qw(fatalsToBrowser);
    >
    > print "Content-type: text/html\n\n";
    >
    > # This will not open a valid filehandle since the script runs as
    > nobody
    > my $fh = IO::File->new("> doesnotexist.txt");
    > print $fh "foo";
    >
    > die "Die here, if you get this far";
    >
    > The line 'print $fh "foo";' does generate an error, however that error
    > gets written to /var/log/httpd/error_log where it says "Can't use an
    > undefined value as a symbol reference at /var/www/html/carptest/open-
    > wrong-file-with-carp-fatalstobrowser.cgi line 11". I want that sent
    > to the *browser*, not written to the log file. The line 'die "Die
    > here, if you get this far";' never gets executed.


    As Xho indicated, you may be using a buggy version of CGI::Carp, so it
    could be worth trying to upgrade. The usual check of the return value,
    when doing IO operations, may be another solution:

    my $fh = IO::File->new("> doesnotexist.txt")
    or die "Filehandle creation failed: $!";

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 2, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Nogradi
    Replies:
    0
    Views:
    413
    Daniel Nogradi
    May 4, 2006
  2. Louis Erickson
    Replies:
    2
    Views:
    214
    James Willmore
    Sep 3, 2003
  3. Jo Oberman
    Replies:
    5
    Views:
    223
    Ilya Zakharevich
    Sep 14, 2003
  4. Xho Jingleheimerschmidt

    CGI::Carp fatalsToBrowser alternative

    Xho Jingleheimerschmidt, Mar 24, 2013, in forum: Perl Misc
    Replies:
    0
    Views:
    185
    Xho Jingleheimerschmidt
    Mar 24, 2013
  5. George Mpouras

    CGI::Carp and File::Temp

    George Mpouras, Mar 26, 2014, in forum: Perl Misc
    Replies:
    7
    Views:
    171
    Rainer Weikusat
    Mar 27, 2014
Loading...

Share This Page