develooper Front page | perl.perl5.porters | Postings from August 2001

[ID 20010826.005] Carp fails for overloaded object

Thread Next
From:
mjd
Date:
August 26, 2001 12:09
Subject:
[ID 20010826.005] Carp fails for overloaded object
Message ID:
20010826190942.26507.qmail@plover.com
Path: newshog.newsread.com!bad-news.newsread.com!netaxs.com!newsread.com!news.uchicago.edu!newsfeed.stanford.edu!postnews1.google.com!not-for-mail
From: demerphq@hotmail.com (Yves Orton)
Newsgroups: comp.lang.perl.misc,comp.lang.perl.modules
Subject: Bug and Fix in Carp::Heavy.pm
Date: 26 Aug 2001 10:16:58 -0700
Organization: http://groups.google.com/
Lines: 101
Message-ID: <74f348f7.0108260916.569497a9@posting.google.com>
NNTP-Posting-Host: 194.203.212.8
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 998846218 7039 127.0.0.1 (26 Aug 2001 17:16:58 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: 26 Aug 2001 17:16:58 GMT
Xref: bad-news.newsread.com comp.lang.perl.misc:414065 comp.lang.perl.modules:54543

Hello.

I have identified a minor bug in Carp::Heavy.pm.

If a class has overloaded the "" operator and it also uses Carp then
when calling carp/croak the object reference will be stringified by
carp.  This also can lead to deep recursion if the stringify routine
also calls cluck/confess.

Example:
#!perl
package TestCarp;
use Carp qw(carp cluck);
use strict;
use warnings;
use overload qw("" stringify);

sub stringify {
	carp "! In stringify !";
	#cluck "! In stringify !";
	${$_[0]};
}

sub new { my $c=shift; my $s=join("",@_); return bless \$s,$c};
1;
print "Testing Carp...\n";
my $t=__PACKAGE__->new("Testing!");
print $t;

__END__

Outputs when run:
D:\Development\Perl\Scratch>testcarp.pl
Testing Carp...
! In stringify ! at E:/Perl/lib/Carp/Heavy.pm line 90
! In stringify ! at D:\Development\Perl\Scratch\TestCarp.pl line 9
        TestCarp::stringify('Testing!', undef, '') called at
D:\Development\Perl\Scratch\TestCarp.pl line 20
Testing!
D:\Development\Perl\Scratch>

And if the carp line is commented out and the cluck line uncommented
you get:
D:\Development\Perl\Scratch>testcarp.pl
Testing Carp...
Deep recursion on subroutine "Carp::cluck" at
D:\Development\Perl\Scratch\TestCarp.pl line 10.
^C
D:\Development\Perl\Scratch>

Obviously the problem is in line 90 of Carp\Heavy.pm (Neither Carp.pm
or Carp\Heavy.pm have version numbers so thats all I can say) The code
and fix are as follows (line numbers added at left of | symbol):

16|# This package is heavily used. Be small. Be fast. Be good.
17|

89|			# force reference to string representation
90|			$_.="";

Should be changed to 

16|# This package is heavily used. Be small. Be fast. Be good.
17|use overload; # sorry, no choice 

89|			# force reference to string representation
90|			$_=overload::StrVal($_);

After these changes we get the predictable outputs:
D:\Development\Perl\Scratch>testcarp.pl
Testing Carp...
! In stringify ! at D:\Development\Perl\Scratch\TestCarp.pl line 9
        TestCarp::stringify('TestCarp=SCALAR(0x1abaeec)', undef, '')
called at D:\Development\Perl\Scratch\TestCarp.pl line 20
Testing!
D:\Development\Perl\Scratch>

and 
D:\Development\Perl\Scratch>testcarp.pl
Testing Carp...
! In stringify ! at D:\Development\Perl\Scratch\TestCarp.pl line 10
        TestCarp::stringify('TestCarp=SCALAR(0x1abaeec)', undef, '')
called at D:\Development\Perl\Scratch\TestCarp.pl line 20
Testing!
D:\Development\Perl\Scratch>


I understand the intention is to make Carp as light as possible but it
would seem overload::StrVal is essential to avoid this bug.  I dont
know if the perl community would agree but maybe the change should be
made to the standard distro.

As a second point, I have often wondered why there arent published
interfaces into Carp.pm so that we dont have to die/warn their
results, but perhaps use them in some other way. I suppose that
Carp::shortmess and Carp::longmess is ok, but I thought that perhaps
'chirp' and 'confide' might fit in with the 'c' word motif :-)
chirp/carp/croak confide/cluck/confess better.

Cheers all,
Yves


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About