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

  • Thread starter Bennett Haselton
  • Start date
B

Bennett Haselton

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?
 
X

xhoster

Bennett Haselton said:
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.
 
G

Gunnar Hjalmarsson

Bennett said:
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: $!";
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top