[Bioperl-l] why string overload is bad

Stefan Kirov skirov at utk.edu
Tue Jun 28 09:44:07 EDT 2005


Hilmar Lapp wrote:

> 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.

Hilmar definitely has a point here. Overloading also causes trouble with 
respect to other code- take as an example Devel::ptkdb. Try to inspect a 
variable, that contains or is an object of type Bio::Annotation::*. Of 
course, one can always dump the data, but it is slower and this can be 
rather annoying if you just want to take a peek into something. And I 
have no doubt there would be other packages that fail because of the 
overloading.

>
> Comments, opinions anybody?
>
>     -hilmar
>



More information about the Bioperl-l mailing list