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

When using CGI::Carp, the application of die to a string that is not terminated with a newline doesn't output the proper file name and line number. Try running the following code.
#!/usr/bin/perl use strict; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); eval { die "Ouch"; }; if ($@) { print $@, "\n"; } eval { my_die("Ouch"); }; if ($@) { print $@, "\n"; } sub my_die { my ($arg) = @_; if (CGI::Carp::ineval) { $arg = join("", @_); my($file,$line,$id) = CGI::Carp::id(1); $arg .= " at $file line $line." unless $arg=~/\n$/; if ($arg !~ /\n$/) { $arg .= "\n"; } CGI::Carp::realdie $arg; } if (!ref($arg)) { $arg = join("", @_); my($file,$line,$id) = CGI::Carp::id(1); $arg .= " at $file line $line." unless $arg=~/\n$/; &fatalsToBrowser($arg) if $CGI::Carp::WRAP; if (($arg =~ /\n$/) || !exists($ENV{MOD_PERL})) { my $stamp = CGI::Carp::stamp; $arg=~s/^/$stamp/gm; } if ($arg !~ /\n$/) { $arg .= "\n"; } } realdie $arg; }

Title edit by tye

Replies are listed 'Best First'.
Re: CGI::Carp peculiarity
by davido (Cardinal) on Dec 27, 2003 at 07:11 UTC
    I do think you're onto something. It looks to me like it might be something worth reporting to Lincholn Stein (CGI::Carp's author)... or to the p5p people, since CGI::Carp is a core module. Not sure which is more appropriate.

    Here are a few tests that I found interesting, and which give results that are in some ways inconsistant with documentation.

    use strict; use warnings; use CGI::Carp; eval { die "Ouch. 1" }; print $@, "\n" if $@; eval { die "Ouch. 2\n" }; print $@, "\n" if $@; eval { CORE::die "Ouch. 3" }; print $@, "\n" if $@; eval { CORE::die "Ouch 4.\n" }; print $@, "\n" if $@; __Output__ Ouch. 1 at C:/Perl/lib/CGI/Carp.pm line 312. Ouch. 2 Ouch. 3 at C:\Perl\scripts\mytest.pl line 14. Ouch. 4

    And now the same tests but with Carp.pm (not CGI::Carp)...

    use strict; use warnings; use Carp; eval { die "Ouch. 1" }; print $@, "\n" if $@; eval { die "Ouch. 2\n" }; print $@, "\n" if $@; eval { CORE::die "Ouch. 3" }; print $@, "\n" if $@; eval { CORE::die "Ouch 4.\n" }; print $@, "\n" if $@; __OUTPUT__ Ouch. 1 at C:\Perl\scripts\mytest.pl line 8. Ouch. 2 Ouch. 3 at C:\Perl\scripts\mytest.pl line 14. Ouch. 4

    Clearly Carp.pm's output is what we want. And CGI::Carp's output is improper. This bug appears only in scripts that are evaled. mod_perl scripts are eval'ed too, so the problem shows up there too. The POD for CGI::Carp says that CGI::Carp is a CGI-friendly equivilant to Carp. But it turns out that CGI::Carp is not as good as Carp at reporting the correct line numbers. I would consider this a bug.

    As for why a "\n" character in the die string suppresses the printing of line numbers, that's a mystery for which I can't find any documentation anywhere.


    Dave

      that's a mystery for which I can't find any documentation anywhere.
      In perlfunc under die:   If the last element of LIST does not end in a newline, the current script line number and input line number (if any) are also printed, and a newline is supplied.

      Separately, I had such problems with CGI::Carp running within a CGI::Application environment I just gave up and 'borrowed' parts of Carp/CGI::Carp to create my own exception trap output routine.   CGI::Carp has problems with eval because it is trying very hard to be compatible with mod_perl and apparently there just isn't enough context to do the right thing.