CGI::Carp and "useless quotes"

  • Thread starter Gunnar Hjalmarsson
  • Start date
G

Gunnar Hjalmarsson

I have made a weird observation when using CGI::Carp, version 1.26.

This code:

use CGI::Carp 'fatalsToBrowser';
open FH, 'nonexistingfile' or die $!;

results in the message " at ...test.pl line 10." being sent to STDERR,
i.e. the error string is not printed.

If I change the code to:

use CGI::Carp 'fatalsToBrowser';
open FH, 'nonexistingfile' or die "$!";
--------------------------------------^--^

the resulting error message instead reads "No such file or directory
at ...test.pl line 10.", i.e. as expected.

(Version 1.20 of CGI::Carp behaves as expected whether the $! variable
is quoted or not.)

Is the above a bug or a feature in CGI::Carp?
 
K

ko

Gunnar Hjalmarsson said:
I have made a weird observation when using CGI::Carp, version 1.26.

This code:

use CGI::Carp 'fatalsToBrowser';
open FH, 'nonexistingfile' or die $!;

results in the message " at ...test.pl line 10." being sent to STDERR,
i.e. the error string is not printed.

If I change the code to:

use CGI::Carp 'fatalsToBrowser';
open FH, 'nonexistingfile' or die "$!";
--------------------------------------^--^

the resulting error message instead reads "No such file or directory
at ...test.pl line 10.", i.e. as expected.

(Version 1.20 of CGI::Carp behaves as expected whether the $! variable
is quoted or not.)

Is the above a bug or a feature in CGI::Carp?

Sorry, this isn't an answer to your question, but wouldn't it be
better to include more debugging information rather than less? Even
something as simple as:

my $nonexistingfile = 'nonexistingfile';
open FH, $nonexistingfile or die "Couldn't open $nonexistingfile: $!";

For example, it would make a difference when you're munging data to
create the filename(s) because you'll be able to see the *exact*
filename. I make my share of errors/typos :( and try to keep the
number of bare '$!'s to a minimum.

HTH - keith
 
G

Gunnar Hjalmarsson

ko said:
wouldn't it be better to include more debugging information rather
than less? Even something as simple as:

my $nonexistingfile = 'nonexistingfile';
open FH, $nonexistingfile or die "Couldn't open $nonexistingfile:
$!";

I typically do something like that, too. But sometimes I find it
sufficient to just say "... or die $!", and it has worked fine up to
now. Btw, I have made several posts to this group using "... or die
$!", and nobody has objected. ;-)

If the behaviour of CGI::Carp 1.26 *is* a bug, which has been/will be
taken care of in later versions, it's one thing. But if the behaviour
is something that you should expect, I'd better revise some code of
mine...

So if somebody could help me understand what's happening, it would be
much appreciated.
 
M

Matt Garrish

Gunnar Hjalmarsson said:
I have made a weird observation when using CGI::Carp, version 1.26.

This code:

use CGI::Carp 'fatalsToBrowser';
open FH, 'nonexistingfile' or die $!;

results in the message " at ...test.pl line 10." being sent to STDERR,
i.e. the error string is not printed.

If I change the code to:

use CGI::Carp 'fatalsToBrowser';
open FH, 'nonexistingfile' or die "$!";
--------------------------------------^--^

the resulting error message instead reads "No such file or directory
at ...test.pl line 10.", i.e. as expected.

I tried 1.26 and all I got in the browser was '</p>' without quoting $!.
Must have been a bug in that release, because it's gone again in 1.27...

Matt
 
G

Gunnar Hjalmarsson

Matt said:
I tried 1.26 and all I got in the browser was '</p>' without
quoting $!. Must have been a bug in that release, because it's gone
again in 1.27...

Hmm.. For me, both 1.26 and 1.27 fails to print the error string when
$! is passed unquoted to die().
 
M

Matt Garrish

Gunnar Hjalmarsson said:
Hmm.. For me, both 1.26 and 1.27 fails to print the error string when
$! is passed unquoted to die().

Here's the culprit (in the die subroutine):

realdie @_ if ineval;

Sorry, I'm too tired to try and track the bug down to the source, but it
seems @_ is reset if $! is not quoted (try dumping @_ to STDERR before and
after this line ($arg is "join"ed with @_ in the following block, and the
'at line...' is appended, hence your stunted output)).

Matt
 
G

Gunnar Hjalmarsson

Matt said:
Here's the culprit (in the die subroutine):

realdie @_ if ineval;

it seems @_ is reset if $! is not quoted (try dumping @_ to STDERR
before and after this line ($arg is "join"ed with @_ in the
following block, and the 'at line...' is appended, hence your
stunted output)).

Yes, you are right. The underlying 'culprit' seems to be this line in
the _longmess() function:

my $message = Carp::longmess();

i.e. the explanation is to be found in another module... (I haven't
checked out that).

Anyway, this is what the beginning of the die() function in CGI::Carp
currently looks like:

sub die {
my ($arg) = @_;
realdie @_ if ineval;
if (!ref($arg)) {
$arg = join("", @_);

I suppose it would be a good idea to change those lines to:

sub die {
my ($arg) = @_;
my @args = @_;
realdie @args if ineval;
if (!ref($arg)) {
$arg = join("", @args);

That may make a difference when run under mod_perl as well.

What do you think? Should I pass the suggestion to Mr. Stein?
 
M

Michele Dondi

now. Btw, I have made several posts to this group using "... or die
$!", and nobody has objected. ;-)

Also, when posting to this group one often wants to show minimal
examples and just at the same time avoid unchecked open()s, for which
purpose pseudo code like 'or die horribly' is sometimes used too, but
then 'die $!' not only is perfectly fine, but also correctly working.

As a side note I tend to use the "\n"-die()s, which raised some
reactions in the past, however IMHO they are *generally* much more
clear/useful to the "end user" whereas the "other form" is mostly
useful at development stages...


Michele
 
G

Gunnar Hjalmarsson

Matt said:
That would have been my advice had I had more time to get back to
you. Not knowing the inner workings of the module I wouldn't want
to speculate...

Agreed.

FYI, I noticed that Lincoln already has changed the status of the bug
report to "resolved". That was quick. :)
 
M

Matt Garrish

Gunnar Hjalmarsson said:
Agreed.

FYI, I noticed that Lincoln already has changed the status of the bug
report to "resolved". That was quick. :)

It will be interesting to see if it is mentioned in the next release, and
whether he considered it a bug or not...

Matt
 
G

Gunnar Hjalmarsson

Matt said:
It will be interesting to see if it is mentioned in the next
release, and whether he considered it a bug or not...

From the Revision History of CGI version 3.05:

"3. Fixed CGI::Carp::die() so as to avoid problems from _longmess()
clobbering @_."

So yes, he obviously considered it a bug worth fixing (within a few
minutes!). Our discussion bore fruit. :)
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top