[Bioperl-l] why string overload is bad

Hilmar Lapp hlapp at gmx.net
Mon Jun 27 22:06:47 EDT 2005


I've been debugging bioperl-db now for two days in a row to find all 
places that are affected by the string overload for the 
Bio::Annotation::* modules. There are many cases that break because a 
boolean evaluation of $obj is used as a shorthand for defined($obj) && 
ref($obj). One may argue that those cases deserve to be fixed anyway. 
BTW I'm also finding instances of this in the core bioperl library.

However, it gets uglier than this. Consider the following innocuous 
looking code.

use Bio::Annotation::SimpleValue;
my $a = Bio::Annotation::SimpleValue->new(-value => "");
%args = (-blah,$a);
my $ann = $args{-blah} || $args{-BLAH}; # ignore case
print "type of annotation: ", ref($ann),"\n";

This will print 'type of annotation:' and nothing else, because the || 
operator triggers the string overload, so all of a sudden $ann becomes 
a (empty) string instead of an object. So the correct way to express 
this is

my $ann = $args{-blah};
$ann = $args{-BLAH} unless defined($ann); # ignore case

I would argue that string-overloading core library classes requires a 
degree of discipline when programming against it that the library 
becomes pretty unforgiving.

Not really the spirit of how and for whom Bioperl should be useful. A 
beginner could have a pretty hard time tracking down the mistake above, 
let alone the frustration this can generate in the course of such an 
exercise.

Do people really want to go the route of string-overloading the 
annotation classes? To me it's really over the top and is a step 
backwards for ease of using the toolkit.

Comments, opinions anybody?

	-hilmar

-- 
-------------------------------------------------------------
Hilmar Lapp                            email: lapp at gnf.org
GNF, San Diego, Ca. 92121              phone: +1-858-812-1757
-------------------------------------------------------------



More information about the Bioperl-l mailing list