in reply to CGI::Carp reports line numbers incorrectly

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

Replies are listed 'Best First'.
Re: Re: CGI::Carp peculiarity
by shenme (Priest) on Dec 27, 2003 at 18:14 UTC
    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.