Click here to get back home

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

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
use CGI::Carp qw(fatalsToBrowser) not sending file handle errors to browser Bennett Haselton 03-01-2008
Posted by Bennett Haselton on March 1, 2008, 6:17 pm
Please log in for more thread options
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?

Posted by xhoster on March 1, 2008, 9:57 pm
Please log in for more thread options
> 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.

Posted by Gunnar Hjalmarsson on March 2, 2008, 5:17 am
Please log in for more thread options
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

Similar ThreadsPosted
sending file handle as function argument June 8, 2005, 6:40 am
CGI::Carp::croak's behavior differs from Carp::croak's? July 24, 2007, 10:09 am
fatalsToBrowser October 21, 2004, 6:10 pm
syswrite "Bad file descriptor" after successfully writing to that file handle January 7, 2008, 10:31 am
Fastest way to send file to browser March 11, 2008, 3:48 am
Sending an incomplete file? March 10, 2007, 1:05 am
Sending a pdf file with perl August 29, 2007, 10:42 pm
How to delete temporary file after displaying in browser ? October 2, 2006, 11:21 am
General way to run an exe with errors written to a file September 19, 2007, 8:04 pm
sending output to STDOUT or a file April 22, 2008, 2:26 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap